Solved

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

Posted on 2006-07-11
4
201 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now