Link to home
Start Free TrialLog in
Avatar of MartyMcFly
MartyMcFly

asked on

Global variables in Visual Basic 6 (doesn't work !)

I'm aware this question has been asked a dozen times on this site... But I'm also aware that none of the answers I saw works on my computer !!!
I think I'm doing something wrong, but I can't find out what.

What I want to do is extremely simple. I have a variable called 'lang' that I must use as a global variable - the only on of the program.
So I used the Public Lang as Integer in the General/declarations of my first form.
Then, when I assign a number to this variable, it is only available in this form, not the others.

Anyone has an idea ?
Avatar of hes
hes
Flag of United States of America image

If you declare it in teh Declarations section of your first form, it is Global to that form. Available to any sub or Function on that form.
To make it global to the Project, add a module and declare it there. Then it will be available to all forms.
Avatar of jimbucci
jimbucci

Create a globals.bas file and put it in there.
Public lang as string
Jim
You need to put the variable in a BAS module, not a form for it to be accessible outside of the form if you ever want the form to be unloaded and still have the variable around!

Right-click in the project explorer and click on add, then select Module.  Put your declaration in there.  You can set it and reference it from anywhere in your program, even if the form is unloaded.

Alternatively, if you want to leave the form loaded, you can always reference it from anywhere in the code by using form1.Lang = 1234 (for setting) or msgbox form1.lang (for reading).
Avatar of MartyMcFly

ASKER

Thanks for your answers everyone.

I created a module, with the Public lang as integer in it. But still, it doesn't appear to work... Even when I call it with Msgbox form1.lang, it says that he can't find it...

I also tried the form1.lang = i, but with the same result...

Is there still something I'm doing wrong ? Is the BAS module loaded before the rest, or do I have to change something ? I tried to change the start order of the forms, but without any success...
Following another comment I received on a duplicate answer page, here is a little precision :

My problem is not a problem with the value of the variable. But with its declaration, as the two forms don't seem to share this Global variable declared in the BAS module.
i do not understand y u wanna do form1.lang

if you wants to assign value in the 'LANG', then put
lang=i
will do

if u wanna do something like "something.something", it seems like u wanna 'dot' the property call 'LANG' rite, then u cannot just declare
PUBLIC LANG as integer

wat you need to do is

Public Type MyProperty
    LANG As integer
End Type
Public AAA As MyProperty

then in your forms you can do something like
AAA.Lang=i



What I want to do is simpler than that.

In the Form called Form2, I have 6 OptionButtons (Options(x)) and an OK button.
When the user clicks on the OK button, I have this code :
    For i = 0 To 5
        If Options(i).Value = True Then
            lang = i: Exit For
    End If
    Next i
   
    Form1.Visible = True
    Unload Me

Then it closes this Form2 and opens the Form1.

In this Form1, in the _Load Declarations, I'd want to be able to retrieve the value of this 'lang' variable, by simply adding something like

if lang = 3 then....

So, for the moment, the 'lang' variable is only declared in the BAS module, nowhere else. The only reference to this variable is in this Form1, where it has to check its value.
ASKER CERTIFIED SOLUTION
Avatar of applenie
applenie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes it works Applenie !!

Indeed, I had to declare it in the Activate section of Form1 ! I'm so happy I'm giving you 50 points, as you solved my problem AND you learnt me something very valuable !!

Thanx again !!
glad that i can help. :)