Originally Posted by
swenger
I went to the Form_Load() and added
Dim tro as Long
tro =1
I created a button which simply displays MsgBox "The results of tro is" & tro
But when I press the button, the message box only says
The results of tro is
but it doesn't display the 1
How well do you understand the "Scope of Variables"?
You have code that probably looks something like
Code:
Private Sub Form_Load()
Dim tro As Long
tro = 1
End Sub
What is happening is that you declared a variable, "tro", in the form load event. This is known as a "Procedure (local) Variable". The variable exists as long as the procedure is running. Once the procedure ends, the variable dies (along with the value).
So when you click the button (a different procedure), the 1 is not printed because there is nothing TO print - the form load procedure ended.....
Because you are using two different forms, you would need a PUBLIC (global) variable. This scope of variable is declared in the declarations section of a standard module.
If you had a standard module named "Module1", to declare a Public (global) variable, the top 3 line would be
Code:
Option Compare Database
Option Explicit
Public tro As Long
See
Variable Scope in VBA for MS Access
https://bytes.com/topic/access/insig...-vba-ms-access
and
DECLARING AND SCOPING VARIABLES
http://cis.poly.edu/~mleung/CS394/f0...declaring.html
(this one refers to Excel, but has good information.)
------------------------------------
If you want to use the form Load event, I would pass the value using the "OpenArgs" option of the "DoCmd.OpenForm" command.
To see usage for "OpenArgs":
- Open Help
- Set search to "Developer Reference" (click on the down arrow to the right of the search button)
- search on "OpenArgs" (no quotes).