Link to home
Start Free TrialLog in
Avatar of pin_plunder
pin_plunder

asked on

public variables

I've got this problem...

Form1:

Public Senf As String

Private Sub Command1_Click()
Senf = "es un largo relato"
Form2.Show
End Sub

Form2:
Private Sub Command1_Click()
If Senf = "es un largo relato" Then
Print "it works"
Else
Print "it doesn't"
End If
End Sub
------------------
display: it doesn't

why doesn't this code work?
aren't public variables available throughout the project, in all forms and modules?

please just answer THIS question and don't give me unuseful tips.
thanks for your patience   :)
Avatar of polaughlin
polaughlin

Placing the 'Senf' declaration in a module works for me.

Public Senf as String

polaughlin
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

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
One other thing...  Instead of doing this on Form2:

   If Senf = "es un largo relato" Then

You can try this:

   If Form1.Senf = "es un largo relato" Then

and set a break point in the Form1 Load Event.  If the load event fires when the Form2 "If" executes, Form1 was unloaded...


Cheers!
If you add a public variable inside a class/form/usercontrol the VB compiler will translate this into a public PROPERTY of the form.

If you add Option Explicit on all the forms you will get a 'variable not defined' error when you try to access the variable from the Form2.

Only public variables in a module is global variables.

Avatar of pin_plunder

ASKER

thanks everybody


public senf as string
global senf as string

they both work

is there any difference between them?
may be memory usage or something?

thanks everybody


public senf as string
global senf as string

they both work

is there any difference between them?
may be memory usage or something?

Global and Public are the same, however, Global can only be used in modules...


For example:

   Global Senf As String

Will work in a MODULE, but will generate a compile error in a FORM.


Cheers!
thanks.