
-----------------------------------
Matt_872000
Mon May 23, 2005 2:15 pm

New to VB and need some help.
-----------------------------------
I've been fooling around with VB because I am learning it in class. I have just one question, it may sound kind of goofy, and it may be really easy, but how do you  use variables throughout all or some of the "forms" or windows? It would be a great help.

-----------------------------------
GlobeTrotter
Mon May 23, 2005 3:40 pm


-----------------------------------
One way is to create a module and inside the module declare the variables as such:


Global stStringVariable as String
Global iIntegerVariable As Integer


or whatever variables you are using.

They should then become usable over multiple forms.[/code]

-----------------------------------
Matt_872000
Tue May 24, 2005 12:28 pm


-----------------------------------
Call me a loser or noob or whatever you like, but I'm not quite sure what a module is....

-----------------------------------
Maverick
Tue May 24, 2005 3:43 pm


-----------------------------------
Right click on form i believe, on the right hand side where it says Form1, Form2 etc. and click on Create module then enter those variables. Im not positive thats it because im at home and not able to make sure thats correct

-----------------------------------
GlobeTrotter
Tue May 24, 2005 4:12 pm


-----------------------------------
Yes, that is how you create a module.

-----------------------------------
Matt_872000
Tue May 24, 2005 6:50 pm


-----------------------------------
Thanx, I've made a module and it's declaring the variables. The only problem I have is when I assign a value to the variable and go to another form the variable doesn't have anything assigned to it... Is there someway were I can share the variables completely,  throughout all of the forms?

-----------------------------------
GlobeTrotter
Tue May 24, 2005 8:15 pm


-----------------------------------
If you declared the variables in the module as "Global VarName As whatever" it should work.  The global makes it work over multiple forms.  Are you sure you didn't declare it with a "Dim"?

-----------------------------------
Matt_872000
Wed May 25, 2005 12:08 pm


-----------------------------------
Thanx alot. I tested it out individually and it works fine. It seems to be a problem with the program trying the get the variable from a text box. Here is my code involving the text box problem.


In the module I have:

     Option Explicit
     Global ShipName As String

Then in the form called "OnePlayer":

     Option Explicit

     Private Sub Continue_Click()
          ShipName = ShipName.Text
          OnePlayer.Hide
          PlayerOneTravel.Show
     End Sub

Then in the form called "PlayerOneTravel":
     Option Explicit

     Private Sub Form_Load()
          WindowState = 2
          MsgBox (ShipName)
     End Sub


-----------------------------------
GlobeTrotter
Wed May 25, 2005 4:26 pm


-----------------------------------
That code looks fine.  The only thing I can think of is this:  Make sure you didn't declare ShipName anywhere else other than the module.  If there are no other declarations of shipname, I'm not sure.

-----------------------------------
Matt_872000
Wed May 25, 2005 8:13 pm


-----------------------------------
Hmmm... wierd. Well now I've run into another problem :P. What is the list seperator i need to use if i want to use more than one variable in a command like "MsgBox(Variable)"? I know in Turing it is just "," but i have no idea what it is in VB.

-----------------------------------
GlobeTrotter
Wed May 25, 2005 8:18 pm


-----------------------------------
Use an "&"

For example:


Call MsgBox("Variable Name: " & VarName & " Variable two name: " & Var2Name)


-----------------------------------
betaflye
Mon May 30, 2005 8:51 am


-----------------------------------
As for a shared variable, declare them using the Public statement rather than  Dim, I believe.

-----------------------------------
Matt_872000
Wed Jun 01, 2005 12:26 pm


-----------------------------------
I've solved my problem!! :D I think it was because I didn't realise textbox names were considered variables... I'm not exactly sure, but it seemed when  I changed the names of the textboxes and/or variables so that they were different it worked.

-----------------------------------
betaflye
Fri Jun 03, 2005 1:43 pm


-----------------------------------
Sounds like you should review the scope of variables and other identifiers :)
