Computing.Net > Forums > Programming > Declaring global variables in VB

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Declaring global variables in VB

Reply to Message Icon

Name: catchxan
Date: January 28, 2004 at 09:00:05 Pacific
OS: VB 6.0
CPU/Ram: 2.6Ghz and 512MB
Comment:

I am fairly new to VB6.0. I am trying to write a code involving several seperate forms and I would like to know if its possiblt to access variable of one form in another.In other case how can I declare variables so that I can access in different forms.



Sponsored Link
Ads by Google

Response Number 1
Name: MarkM
Date: January 28, 2004 at 09:36:38 Pacific
Reply:

Instead of using Dim to delare variables, use Public. This has to be done at the top of the Form's code.

The best way to do it though, is to add a new Module and call 'Globals' or 'Declarations' or something. Put all the Public declare in there.


0

Response Number 2
Name: StuartS
Date: January 28, 2004 at 11:12:37 Pacific
Reply:

Jusr one word or caution.

If you declare a variable as public at the top of a form and use that variable from outside the form, the form contains it will be implicitly loaded. This may or may not be what you and can cause problems if its not what you want.

Use global variables sparingely. Sometimes there is no option but often there is a better way of using values created in one form and used in another.

Keeping track of global variables can get quite complicated as your programme gets more complex.

Stuart



0

Response Number 3
Name: Infinite Recursion
Date: January 28, 2004 at 11:13:59 Pacific
Reply:

If I am not mistaken, from Form1 you can populate a text box in Form 2...

...Form1_Load...
Form2.txtMyTextBox.text = "Hello"

In .NET you would have to do this...

...Form1_Load...
Dim frmForm2 As Form2 = New Form2()
frmForm2.Show()
frmForm2.txtSiteName.Text = yourValue

One of those options, or declaring the global using Public will work for you.

IR


0

Response Number 4
Name: catchxan
Date: January 30, 2004 at 06:01:33 Pacific
Reply:

I did try using th Public declaration as you guys suggested,but when I access the global variable in another form in the same project it becomes zero eventhough I assigned a value for that.Am I doing something here?Do I have to activate something or add something?

Xavier


0

Response Number 5
Name: Infinite Recursion
Date: January 30, 2004 at 06:22:31 Pacific
Reply:

Those above methods work for me... post a bit of code so we can attempt to see whats going on...


0

Related Posts

See More



Response Number 6
Name: catchxan
Date: January 30, 2004 at 06:41:53 Pacific
Reply:

Here's a sample of what I want to do.I have 2 forms Form1 and Form2.

Form1 has 2 command buttons (Display and Next)and 1 texbox(Text1).

Form2 has a Textbox(Text1) and 1 command button(Display).
I declare 2 variables (i,j) as public and I wrote a code to display i in text1 of Form1 and j in text1 of Form2.Here's how I tried.

Code in Form1

Public i, j As Integer
Option Explicit


Private Sub Next_Click()
Unload Me
Load Project1.Form2
Form2.Show
End Sub

Private Sub Display_Click()
Text1.Text = i

End Sub

Private Sub Form_Load()
i = 100
j = 10
End Sub

Code in Form2

Private Sub Display_Click()
Text1.Text = j
End Sub

the result I got was a blank screen in form2.text1. What am I doing wrong?


0

Response Number 7
Name: StuartS
Date: January 30, 2004 at 07:19:07 Pacific
Reply:

To make this work you should use the following code.

Private Sub Display_Click()
Text1.Text = form1.j
End Sub

Unloading form1 caases the variable to be reset. You need to keep form1 loaded but hidden.

Using a variable declared in another module you must prefixed with module name. This code will implicitly reload Form1.

This code should have produced an error. Check VB Options and make sure the Require Variable Declaration is ticked. Your code is expecting j to be a local variable. As it has not been declared it makes one.

Stuart



0

Response Number 8
Name: catchxan
Date: January 30, 2004 at 07:55:38 Pacific
Reply:

ok..I got that..This is the situation I have..I have 10 wav files.I have to choose 4 and then play them one by one (each of the 4 is related to a commandbutton) and it has option buttons under them and the user has to choose 1. Then click next button.Another form comes up with 3 command buttons each having a wav file excluding the 1 the user selected in the previous form and so on till I reach 1 button.This has to be repeated till I am done with 120 times of loading all forms.Inorder to do this I guess I need a counter that records each time the form gets loaded,hence I needed a global variable which can be accessed by all forms so that i can keep track.If declare the variable in the form each time the form is accessed it resets to 0.Do u have any suggestions regarding this?And is there anyway of delaring varibales so that I can access tem in all forms present in the project.



0

Response Number 9
Name: StuartS
Date: January 30, 2004 at 08:58:07 Pacific
Reply:

This may be a case where a global variable may be necessary. Create a .bas module and declare the variable there.

Stuart


0

Sponsored Link
Ads by Google
Reply to Message Icon

Question About Font Shell command in C



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Declaring global variables in VB

Declaring global variables contd www.computing.net/answers/programming/declaring-global-variables-contd/9368.html

System Environment Variables in VB www.computing.net/answers/programming/system-environment-variables-in-vb/12819.html

setting environment variables in vb www.computing.net/answers/programming/setting-environment-variables-in-vb/13145.html