Author |
Message |
Matt_872000
|
Posted: Mon May 23, 2005 2:15 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
GlobeTrotter
|
Posted: Mon May 23, 2005 3:40 pm Post subject: (No subject) |
|
|
One way is to create a module and inside the module declare the variables as such:
code: |
Global stStringVariable as String
Global iIntegerVariable As Integer
|
or whatever variables you are using.
They should then become usable over multiple forms.[/code] |
|
|
|
|
![](images/spacer.gif) |
Matt_872000
|
Posted: Tue May 24, 2005 12:28 pm Post subject: (No subject) |
|
|
Call me a loser or noob or whatever you like, but I'm not quite sure what a module is.... |
|
|
|
|
![](images/spacer.gif) |
Maverick
![](http://server2.uploadit.org/files/gregdilts-bleh2.jpg)
|
Posted: Tue May 24, 2005 3:43 pm Post subject: (No subject) |
|
|
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 |
|
|
|
|
![](images/spacer.gif) |
GlobeTrotter
|
Posted: Tue May 24, 2005 4:12 pm Post subject: (No subject) |
|
|
Yes, that is how you create a module. |
|
|
|
|
![](images/spacer.gif) |
Matt_872000
|
Posted: Tue May 24, 2005 6:50 pm Post subject: (No subject) |
|
|
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? |
|
|
|
|
![](images/spacer.gif) |
GlobeTrotter
|
Posted: Tue May 24, 2005 8:15 pm Post subject: (No subject) |
|
|
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"? |
|
|
|
|
![](images/spacer.gif) |
Matt_872000
|
Posted: Wed May 25, 2005 12:08 pm Post subject: (No subject) |
|
|
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.
code: |
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
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
GlobeTrotter
|
Posted: Wed May 25, 2005 4:26 pm Post subject: (No subject) |
|
|
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. |
|
|
|
|
![](images/spacer.gif) |
Matt_872000
|
Posted: Wed May 25, 2005 8:13 pm Post subject: (No subject) |
|
|
Hmmm... wierd. Well now I've run into another problem . 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. |
|
|
|
|
![](images/spacer.gif) |
GlobeTrotter
|
Posted: Wed May 25, 2005 8:18 pm Post subject: (No subject) |
|
|
Use an "&"
For example:
VisualBASIC: |
Call MsgBox("Variable Name: " & VarName & " Variable two name: " & Var2Name)
|
|
|
|
|
|
![](images/spacer.gif) |
betaflye
|
Posted: Mon May 30, 2005 8:51 am Post subject: (No subject) |
|
|
As for a shared variable, declare them using the Public statement rather than Dim, I believe. |
|
|
|
|
![](images/spacer.gif) |
Matt_872000
|
Posted: Wed Jun 01, 2005 12:26 pm Post subject: (No subject) |
|
|
I've solved my problem!! 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. |
|
|
|
|
![](images/spacer.gif) |
betaflye
|
Posted: Fri Jun 03, 2005 1:43 pm Post subject: (No subject) |
|
|
Sounds like you should review the scope of variables and other identifiers ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
|