473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Access an object on one form from another?

Hello,
I have a (hopefully simple) problem.

I have a form, lets call it frmMain, and this has a database connection
object on it (sqlconnection) declared as public. All works well.

One of the operations I want to do will occur on a second form (call it
frmChild).

I can open up this quite happily with

frmChild NewChildForm = new frmChild();
frmChild.showdialog();

but on the child form I want to do a Database operation. frmMain.dbconn
(name of my connection) doesn't work, and from what I have read this is
because I need an object reference to frmMain rather than just frmMain. I
get that, but how do I reference something that already exists rather than
creating a new one?
Hope that makes sense

James.
Feb 27 '06 #1
2 5601

"JamesB" <james.> wrote in message
news:44***********************@news.zen.co.uk...
Hello,
I have a (hopefully simple) problem.

I have a form, lets call it frmMain, and this has a database connection
object on it (sqlconnection) declared as public. All works well.

One of the operations I want to do will occur on a second form (call it
frmChild).

I can open up this quite happily with

frmChild NewChildForm = new frmChild();
frmChild.showdialog();

but on the child form I want to do a Database operation. frmMain.dbconn
(name of my connection) doesn't work, and from what I have read this is
because I need an object reference to frmMain rather than just frmMain. I
get that, but how do I reference something that already exists rather than
creating a new one?
Hope that makes sense

James.

Actually - I think I worked it out?

Added a property to frmChild:

public frmMain MyParentForm;

then in the code in frmMain that opens the child I added:

frmChild.MyParentForm = this;

I can then see MyParentForm.DBConn from frmChild...
not run it yet, but is this the correct method to use?
James.
Feb 27 '06 #2
sure you does. It will work perfect.
acturally, you can use the following code:

in frmMain replace
frmChild NewChildForm = new frmChild();
frmChild.showdialog();
with:
frmChild NewChildForm = new frmChild();
NewChildForm.Owner = this;
frmChild.showdialog();
and in formChild when you would use frmMain,insert the following code:
frmMain fm = this.Owner as frmMain;
and now ,fm is a reference of frmMain

Feb 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ryan Park | last post by:
Hi, //SITUATION I got a panel control that hold a certain position on a form. Every controls or UIs are on this panel. At certain situation, I called dispose() method of this panel control...
2
by: Galina | last post by:
Hello I have a very complex database application, which have been working fine for several years in Access 97. Now I had to convert it into Access 2000. The main form (course) has got 2 subforms....
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
3
by: Michael Ramey | last post by:
How can controls on a Windows Form be accessed (or referenced) from another Class? I know how to do it from another Form. The following doesn't work even though the Control Modifiers property is...
5
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a revolution in programming forces you to abandon all...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
0
by: C-Note | last post by:
This is a simple MS Access 2003 Project file (*.adp). It accesses a SQL Server 2005 database on the network. It has 10 tables. Everything updates, deletes, inserts, adds without a hitch, except...
10
by: Les Desser | last post by:
In article <fcebdacd-2bd8-4d07-93a8-8b69d3452f3e@s50g2000hsb.googlegroups.com>, The Frog <Mr.Frog.to.you@googlemail.comMon, 14 Apr 2008 00:45:10 writes Not sure if I quite follow that. 1....
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.