-
I am making a little program that requires a variable from one form to end up on another. I use that Public statement but when I compile or run the program, I get an erro message saying the variable is not defined. any inference into what the problem is would be greatly appreciated.
Thank You Much
-
what kind of variable is it, because some kinds can't be declared as Public variables
-
If you need the variable from one form to appear on the other, you need to access the object followed by the variable.
For instance
Form1:
Public Happy As String
Form2:
Dim MyNews
Private Sub Form_Load()
MyNews = Form1.Happy
Msgbox MyNews
End Sub
Hope this helps
-
Actually, let me put up some working code so you get a better idea.
Form1 (put a command button on form1):
Code:
Public Happy As String
Private Sub Command1_Click()
Form2.Show
End Sub
Private Sub Form_Load()
Happy = "Happy"
End Sub
Form2:
Code:
Private Sub Form_Load()
MsgBox Form1.Happy
End Sub
Hope that will clear things up. There are some things which cannot be declared public on a form, but at the moment, I'm reletively unsure what they are. The above example does, however, work ;)
-
Dim it.
Code:
INTEGER
Dim myINT As Integer
Private Sub Form_Load()
myINT = 0
End Sub
Private Sub Command1_Click()
myINT = myINT + 1
End Sub
Private Sub Form_Unload(Cancel As Integer)
Msgbox myINT
End Sub
Code:
STRING
Dim myString As String
Private Sub Form_Load()
myString = "Hello"
Msgbox myString
myString = "Good bye"
End Sub
Private Sub Form_Unload(Cancel As Integer)
Msgbox myString
End Sub
You should also remove Option Explicit, it's not needed.
Pretty easy to understand :rolleyes:.
-
I may be wrong, but using dim won't allow another form to grab the information from that variable, will it?
>I am making a little program that requires a variable from
>one form to end up on another.
That requires the use of Public and accessing that variable through:
object.XXX (XXX can be an object, variable, sub, function, control, or property as long as
it's been declared as public.)
I could be wrong about the Dim statement, but I do know that I was able to access the variable from one form on another using public and the above syntax.