Global variable

Ben McCall

Registered User.
Local time
Today, 23:13
Joined
Jun 20, 2001
Messages
22
I have a database in access 97.

I have a main form with two subforms.

I want to use a global variable "pre" in the main form and one subform.

My understanding was that I could declare it in the main declarations of the main form as follows:

Option Compare Database
Option Explicit
Public pre As Integer

I would like to give it a value of 1 in the mainform(after declaring "dim pre as integer") Then I have declared "dim pre as integer" in the sub form and entered:

If pre = 1 then
pre = 0
else
DoCmd.GoToRecord , , acNewRec
end if

If I put a breakpoint on "If pre = 1 then" and check the value of "pre", it is "0".

It is obvious that "pre" is not a global variable or I am completely confused.

Can anyone help?

Thanks!

Ben McCall
 
The subform's open and load events occur before those events in the main form. So the subform has been opened before the variable was set.

I would move the declaration to a standard code module and set the value to one in the routine that opens the main form rather than in the main form itself.
 
I guess that i did not explain it accurately. Both the form and the subform are open. I then click on a "previous button" which has the code:
"Dim pre As Integer
pre = 1
DoCmd.GoToRecord , , acPrevious"

which sends me to the "if" code above.

Ben McCall
 
Try deleting the "Dim pre As Integer" in the onclick(?) event procedure - "pre" has already been declared in the module declarations and is available to every procedure in the module (the public keyword makes pre available to every open module).

By defining pre with a dim statement within a procedure in a module after declaring pre in the declarations of the module, what I think happens is a new "pre" variable is allocated to the procedure and is private to that procedure.

Doug.

[This message has been edited by DALeffler (edited 07-11-2001).]
 

Users who are viewing this thread

Back
Top Bottom