Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global Variables 1

Status
Not open for further replies.

mettodog

Vendor
Jul 11, 2000
94
US
in variables.bas, in the declarations section i have global mytext as string

i have 2 forms, form1 and form2

on form 1 i have a text box, text1.text, and on form2 i have text1.text

form1-> (in form load)mytext = text1.text
(in text1 change) mytext = text1.text
form2-> (in form load)text1.text = mytext

this doesnt work, why? what can i do to make it work?

 
I don't understand your problem - I coded up your example, entered 'test' in the text box on the first form and 'test' appear in the text box on the second form. How are you defining your 'global' variable. I used both Public (preferred) and Global. I did all this in VB 6.
 
i did public mytext as string

also

global mytext as string
 
Are you getting an error message? - if so, you need to post it for us. Step through your code using breakpoints or use a watch - alot of people don't seem to use these or are unaware of their existance. Place your variables in your startup bas module. All things to try... Rob Marriott
rob@career-connections.net
 
If the goal of the exercise is to transfer a text value from one textbox on Form1 to a textbox on Form2, then there is no need to go through a shared public variable. Here's how to do it:

The Main BAS module loads and shows the forms. Focus is on Form1:

Public Sub Main()
Form1.Show
Form2.Show
Form1.SetFocus
End Sub


The Text1_Change event for Form1 contains:

Private Sub Text1_Change()
Form2.Text1.Text = Me.Text1.Text
End Sub


As soon as you start typing the text in the textbox on form1, it will appear on form2.

HOWEVER, This might not work in the next version of VB called VB.NET.

Some preliminary information for that version shows that direct referencing of a control on another form will fail. See: "10 Ways to prepare for VB.NET" VB Prog Journal, December 2000, pp62-65.

So, What's the alternative? Yes, you can still used shared public variables. But as said before (thread222-37333 and thread222-42216) this is NOT the way to go. Use public Properties where you can. Here's how: (code for the Main BAS module is not changed)

Add following code to Form1:

Private m_strTextValue As String

Public Property Get TextValue() As String
TextValue = m_strTextValue
End Property

Private Sub Text1_Change()
m_strTextValue = Me.Text1.Text
End Sub


As soon as you start entering the text, the internal variable m_strTextValue will be updated. This value can not be changed from the outside (as it is private).
If one likes to fetch that value he/she has to do it through the Property TextValue.

For instance, Form2 might do so upon activation:
(add this code to Form2)

Private Sub Form_Activate()
Me.Text1.Text = Form1.TextValue
End Sub


You'll observe that as soon as you click Form2, the text will (magically) appear.

This presumably will still work in VB.VET


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top