Global Variables in VB

bella

Registered User.
Local time
Today, 12:58
Joined
Jul 31, 2003
Messages
38
Hi,

does anyone know how to declare and use global variables in VB?

Eg, I have an integer variable called temp1

which i need to use in two different subs.
- in sub1 when i use the variable, I assign an integer value to it
- in sub 2 when i use the var iable i use its value in an SQL string

im not sure why, but its not storing the value for the sub2 ??

whats the right way to do this?

Bella
 
Using a global variable just to pass something form 1 sub to another is not a good thing to do.

rahter you should pass it on

sub Sub2 (Temp1 as Integer)

then call your sub2 from 1:

Call Sub2(Temp1)

Read the help on subs for more info

Regards

The Mailman
 
If these two subs are on the same form's module you can declare a form level variable instead.
 
Mile-O-Phile

how do u declare a form level variable??

Bella
 
By declaring it as you would any variable OUTSIDE a sub. Right at the top of your module:

Option Compare Database
Option Explicit

Dim myFormVariable As Integer

Regards
 
thats what I had done originally...i put it right up the top in the Option Compare Database bit

but its not holding the values i assign to it

?:confused:

Bella
 
do i have to declare the variable in each subj I use it in as well as the initial declaration?
 
No

Code:
Option Explicit
Option Compare Database

    Dim myString as String


Private Sub Form_Load()

    myString = "ABC"
    One

End Sub


Private Sub One()

    MsgBox myString
    myString = "123"
    Two

End Sub

Private Sub Two()

    MsgBox myString

End Sub
 

Users who are viewing this thread

Back
Top Bottom