How do you declare a variable and use a variable which is availabe in any form.
Thankyou
Printable View
How do you declare a variable and use a variable which is availabe in any form.
Thankyou
But it has to be in a Module, not in a form. A public variable in a form is only accessable to that form's functions and subs.Code:Public MyVar As Integer ' Or Long, Variant, String etc
Public Arrays must be declared in modules too.
You can also use the Global Keyword.
That's not strictly true because public variables in a form are accesable to other parts of the program while the form is loadedQuote:
But it has to be in a Module, not in a form. A public variable in a form is only accessable to that form's functions and subs.
Public Arrays must be declared in modules too.
form1:
form2:Code:
Option Explicit
Public intNum As Integer
Private Sub Command1_Click()
Form2.Show
End Sub
Private Sub Form_Load()
intNum = 5
Form1.intNum = 8
MsgBox Form1.intNum
End Sub
but yes, public variables would normally go in a module.Code:Option Explicit
Private Sub Command1_Click()
MsgBox Form1.intNum
End Sub
Public is ususlly rather than Globalthese days.
Global is a hanger-on from older versions of (Visual) Basic