Does VBA manage the reference (shape) of global variables?
Ok, I was from Access programming for multiple versions, but I can swear I was able to specify controls in global form variables. Sample code as follows:
Option Compare Database
Option Explicit
Dim Testvar As String
Private Sub Form_Load()
Testvar = "Load"
End Sub
Private Sub Form_Open(Cancel As Integer)
Testvar = "open"
End Sub
Private Sub Text0_Click()
Testvar = "settest"
End Sub
I should be able to put a textbox in a control that can see the TestVar variable, but the controls don't. Also, I used to do this with the form's record source.
So the questions - I'm crazy - was that impossible? Or have I forgotten how to access the form?
And then the most important question is what is the best way to get around this?
The most common way to use this method is to pass OpenArgs (in this case, write the keys), which is then handled by global vars, and then multiple controls display the public arguments and / or display values ββto be displayed using the keys.
I am very sorry to have to create routines that restore and load record sources for controls. Hope someone knows a better approach
source to share
You have to actually set the value of the textbox. There is no way (as far as I know) to bind a textbox to a variable.
Option Compare Database
Option Explicit
Private Sub Form_Load()
Text0.Value = "Load"
End Sub
Private Sub Form_Open(Cancel As Integer)
Text0.Value = "open"
End Sub
Private Sub Text0_Click()
Text0.Value = "settest"
End Sub
Of course, you can store the value in a variable and use it to set the value instead, but in this simple example that doesn't make sense.
source to share
TempVars is a function introduced in Access 2007. So, if your version is Access> = 2007, you can use TempVar to hold the string value. Then you can use TempVar as control source for your textbox.
With the =[TempVars]![Testvar]
control source for Text0, the following event routines do what you requested.
Private Sub Form_Open(Cancel As Integer) TempVars.Add "Testvar", "Open" End Sub Private Sub Form_Load() TempVars("Testvar") = "Load" End Sub Private Sub Text0_Click() TempVars("Testvar") = "settest" Me.Text0.Requery End Sub
Note: [TempVars]![Testvar]
will be available throughout the application throughout the entire session. If this is the problem in your situation, you can remove the TempVar in the Close form:TempVars.Remove "Testvar"
source to share
Wanted: show app user login id for all forms in app.
This is how I did it:
Create a module: module_init_globals
with the following code:
Option Compare Database
'Define global variables
Global GBL_LogonID as string
Option Explicit
Public Sub init_globals ()
'Initialize the global variables
'Get_Logon_Name is a function defined in another VBA module that determines the logon ID of the user
GBL_LogonID = Get_Logon_Name()
End Sub
On the main/first form - we need to call the module that will initialize the global variables:
In the code for "on open" event I have:
Private Sub Form_Open (Cancel as Integer)
call init_globals
End Sub
then on each of the forms in the app, I have a text control - txtUserID to display the logon id of the user
and I can set it value in the "on open" event of the form.
txtUserID.value = GBL_LogonID
source to share