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

MartyMcFly
MartyMcFly used Ask the Experts™
on
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 ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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.

Commented:
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).
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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...

Author

Commented:
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.

Commented:
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



Author

Commented:
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.
Commented:
if u declare it in Bas module ONLY, it should be ok.

One question, when did you load your FORM1 ?? since you put
'if lang = 3 then....
in the Form1's form_Load procedure, you gotta make sure thatthe form2 button is pressed b4 u load the Form1. It would be better if u put
'if lang = 3 then....
in form1's form_Activate.
what i suspected is your FORM1 already loaded b4 the user press any button in form2.

Author

Commented:
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 !!

Commented:
glad that i can help. :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial