Code problems

whatdoidonext

New member
Local time
Today, 23:13
Joined
Jul 5, 2005
Messages
8
Hi
I did post this last week but think I confused everyone!
Form 1 has defined a public variable gflag as in integer. The code starts with the on click event of a button on form1
The initial value of gflag is then set to 1.

Various tests are performed then on line 25 a second form is opened with
DoCmd.OpenForm"form2",acNormal,,,,acDialog
Information is then passed from form1 to form2 to populate it.
As form 2 is opened modally the user has to select one of 2 buttons on the form to exit.

For button1
button1_Click()
gflag =2
DoCmd.Close
End Sub

For button2
button_Click()
gflag=1
DoCmd.Close
End Sub

What I was hoping to happen was that as soon as the user clicked a button operation would be return to line 26 in form1 to test the value of gflag e.g.

if gflag=2 Then
do x
End if

Unfortunately, after putting in a test to show the value of gflag in form 1 it appears the code opens form 2 ok but then immediately processes line 26 as if gflag is always =1, before the user has actually pressed a button on form2.

This is driving me potty, please explain what I am doing wrong and help me find a solution.

Thanks in anticipation.
 
whatdoidonext said:
Form 1 has defined a public variable gflag as in integer

That is the problem with your code. I'll try to explain it:
the variable gflag is part of your Form 1. When you open Form 2 you create a second variable gflag which has nothing to do with the gflag from Form 1.

So access has two gflags:
Form 1.gflag = 1
Form 2.gflag = whatever value you want.

When you close Form 2 the content from gflag is lost and you can not transfer the contents between the two gflags.

SOLUTION
1/ create a module with the public variable gflag.
2/ when you load Form 1 you set the value of gflag to 1; since gflag is part of the module creating your Form 1 you can edit the variable
3/ Load Form 2 and change gflag accordingly, since Form 2 is created by Form 1 which is created by the module, the variable can be changed.
4/ when you close Form 2 you can test for the value of gflag, it should have taken the new value as given in Form 2.

I hope I am clear enough :D
 
TomAllins
Many many thanks for the explanation. I have managed to get the test working now.

Thanks again.
 

Users who are viewing this thread

Back
Top Bottom