Solved

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

Posted on 2006-07-11
4
208 Views
Last Modified: 2010-08-05
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...
0
Comment
Question by:johnhyde
  • 2
  • 2
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 17080307
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
0
 

Author Comment

by:johnhyde
ID: 17080358
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.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
ID: 17080365
johnhyde,

I don't expect that you are doing a new one, the point is that if you do so the first time you instantiate the form, you have to use this reference at all future points. VB will otherwise try to be clever and realise that form1 is the name of an object in the project and create a new instance of form1 for you. Using the forms collection means that you can get at that instantiated object even if your original variable is out of scope.

Tim
0
 

Author Comment

by:johnhyde
ID: 17080438
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question