Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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.

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

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 = yourValueOne of those options, or declaring the global using Public will work for you.
IR

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

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 SubPrivate Sub Display_Click()
Text1.Text = iEnd Sub
Private Sub Form_Load()
i = 100
j = 10
End SubCode in Form2
Private Sub Display_Click()
Text1.Text = j
End Subthe result I got was a blank screen in form2.text1. What am I doing wrong?

To make this work you should use the following code.
Private Sub Display_Click()
Text1.Text = form1.j
End SubUnloading 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

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.

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

![]() |
Question About Font
|
Shell command in C
|

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