Solved

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

Posted on 2006-07-11
4
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

751 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