Link to home
Start Free TrialLog in
Avatar of johnhyde
johnhyde

asked on

Need to get the value of a field in another form without loading it

Hi.

I have a situation where a checkbox (say chkField) is set in form1.  This passes control to form2, which goes on to form3.  Forms 1 and 2 are still displayed under form3 and as the later forms are closed control is returned to the previous one.

Form3 now needs to know the value of the checkbox in form1.  I can simply reference form1.chkField, and this gets the value OK, but it invokes the Load event in form1 and it blats over the top of form3 on the screen.  Similarly if I try saving the checkbox value in form1's Tag property.

I don't want to set a global variable as there are dozens of forms in this program and this only affects two.  It's frowned upon where I'm working anyway.  

I'm sure there's a simple and elegant solution to this, and it's worth 500 points to me if I can get a quick and simple answer...
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi johnhyde,

It sounds to me as if you are assigning a new reference to form1 when you instantiate it:

Dim frm1 As New Form1

For example, this is why using Form1.chkField is creating a new instance, not actually getting the instance you already have.

You can either preserve this reference or iterate the forms collection and locate the appropriate member:

Dim frm As Form
For each frm In Forms
    If frm is Form1 Then
        msgbox frm.chkField
        Exit For ' we found it so we don't need to check any more
    End If
Next

Tim Cottee
Avatar of johnhyde
johnhyde

ASKER

Many thanks for the swift answer, Tim.

I'm not doing any sort of new Dim statement for the form - I need to pass the checkbox value to a stored procedure and I just use the statement

pParameterValue:=form1.chkField

I'll try iterating through the forms, though, in case that makes a difference.  I'll get back to you shortly on that one.
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gotcha.

And you're quite right, finding the right member in the forms collection works fine (although I had to change it to say If frm.name = "form1" for some reason).

Many thanks for the excellent answer and clear explanation, Tim - getting this sorted so quickly is a great help.