• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

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   :)
0
pin_plunder
Asked:
pin_plunder
1 Solution
 
polaughlinCommented:
Placing the 'Senf' declaration in a module works for me.

Public Senf as String

polaughlin
0
 
mcriderCommented:
If Form1 is unloaded, then the public variable Senf will go away...

Add a MODULE to your project and declare Senf this way:

   Global Senf As String


That way, you don't have to worry about the form being unloaded...


Cheers!
0
 
mcriderCommented:
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!
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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

0
 
pin_plunderAuthor Commented:
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?

0
 
pin_plunderAuthor Commented:
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?

0
 
mcriderCommented:
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!
0
 
pin_plunderAuthor Commented:
thanks.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now