Global variables

wim1981

Registered User.
Local time
Tomorrow, 00:29
Joined
May 20, 2015
Messages
40
Hi everyone,

i try to use global variables, but when i want to set a value into those variables, and print it out with MsgBox, these variables stay empty??

I declare the variable in a module like this:

Code:
Global GLB_taal As String
in the first form that give a user a language choice i open the next form and set the variable GLB_taal as follows:

Code:
DoCmd.OpenForm="BeginFrm"
GLB_taal = "NL"

In the BeginFrm i have the following code to set the text of labels and buttons:

Code:
Private Sub Form_Load()
Dim language As String
language = GBL_taal
MsgBox language
If (language = "NL") Then
    
    Me.LblTitle.Caption = "Landmeter"
    Me.LblID.Caption = "Geef ID in: "
    Me.BtnGaVerder.Caption = "Ga Verder"
    Me.LblSearchLan.Caption = "Zoek een Landmeter: "
    Me.BtnZoeken.Caption = "Zoeken"
    Me.LblReport.Caption = "Rapportage"
    Me.BtnRapportage.Caption = "Rapportage"
    Me.LblSetHoursCompulsory.Caption = "Geef de verplichte uren per jaar in: "
    Me.BtnVerplichteUren.Caption = "Uren Ingeven"
    Me.FillupBtn.Caption = "Vul vrijstellingen en Max uren op" 'Na afwerking weg doen en in batch plaatsen
    
Else
If (language = "FR") Then
     Me.LblTitle.Caption = "Arpenteur"
    Me.LblID.Caption = "Entrer ID: "
    Me.BtnGaVerder.Caption = "Allez"
    Me.LblSearchLan.Caption = "Trouver un Arpenteur: "
    Me.BtnZoeken.Caption = "Recherche"
    Me.LblReport.Caption = "Compte-rendu: "
    Me.BtnRapportage.Caption = "Compte-rendu"
    Me.LblSetHoursCompulsory.Caption = "Entrez les heures requises par an: "
    Me.BtnVerplichteUren.Caption = "Saisie heures"
    Me.FillupBtn.Caption = "Entrez exemptions et Max. heures Automatique" 'Na afwerking weg doen en in batch plaatsen
End If
End If
End Sub

I think i am forgetting something in my code tho call or set the variable?
Anyone knows the details of working with global variables?

Wim
 
You should insert

Option Explicit

at the top of all your modules. That would reveal the mistake.
 
You should insert

Option Explicit

at the top of all your modules. That would reveal the mistake.

Hi thanks for answering.

It seems that the error is that i have to, in my Button where i want to set a value in the variable, declare the variable again?
 
No it isn't. Did you insert Option Explicit? Do that. Now in the code window, Run-> Compile and sort out all the compilation errors.
 
You have to spell the name the same where you declare it and where you use it or they will be treated as different variables.

Option Explicit makes VBA complain if a variable is referred to without already being declared.
 
No it isn't. Did you insert Option Explicit? Do that. Now in the code window, Run-> Compile and sort out all the compilation errors.

I have set Option Explicit in all the modules and form code. But no errors when i use the application. The value isn't put into the global variable.
 
You have to spell the name the same where you declare it and where you use it or they will be treated as different variables.

Option Explicit makes VBA complain if a variable is referred to without already being declared.

Indeed i fixed the name of the variable. Now he doesn't give an error, just the value i want to set into the variable isn't set into the variable when i click the button.
 
Indeed i fixed the name of the variable.
What does this mean? Did you declare it again? Misspelled as it was originally?
 
Maybe i can ask a simpler question?

Can you use global variables in and between forms?
Or
Is the declaring and initializing of global variables in a module compulsory?

You then create functions in the module, where you initialize the variables, that you call in the Load function of the Forms?
 
public publicVar as integer ' in form

The form would need to stay loaded and you'd reference the global variable with form_formname.publicVar
 
Paste your actual code not what you thought you wrote.

This code from your original post is not valid:
DoCmd.OpenForm="BeginFrm"

Even if you had that line right, your code sequence is incorrect.

You are assigning a value to the variable after you open the form. The Load event is running before you assign the value.
 

Users who are viewing this thread

Back
Top Bottom