So I'm currently making program for my year 12 IPT assignment. It's a maths game for Year 4's and 5's.
I need to carry a variable throughout the entire project (across forms) so I can easily specify what level the user is playing and how many questions they want to answer etc..

The only way I would ever know how to do this would be to put different captions on labels and then read off the labels and I really don't want to have to do that. There's probably an advanced way that I've never heard of, but becuase my teacher doesn't know what he's doing my entire class is left to learn how to use VB by themselves and through books....so it would be great if I could get some help.

Thanks. Hopefully I've provided enough information.

It's really just one word that I think I need though...like " public level as string" or "static level as string" something like that...

Recommended Answers

All 4 Replies

Define it outside (before) any functions or subroutines.
(these are called "global variables.")
"public level as string"
looks correct.

If you need persistence between executions, set up a routine to write the global variables' values to a file at termination and another to read them from a file at initialization.

Good luck.

Also, you have to put actual global variables in a Module, not in a Form. Your best bet is to alter your project to use Startup Object "Sub Main" rather than a form. To do that, create a module, put in a subroutine named "main" that loads your initial form, then put your globals in front of that. In the Project Explorer, right click on your project (top level of the project tree) and select "Project1 Properties...". Then select the module from the Startup Object drop-down in the General tab.

Just to get you going, here's some sample code (assumes a Module named Module1 and a form named Form1):

' This is in Module1
Public myString As String
Sub main()
myString = "Joe"
Dim myForm As Form
Set myForm = New Form1
myForm.Show
End Sub

' This is in Form1
Private Sub Form_Load()
Me.Caption = myString
End Sub

Good luck!

How about using Property GET/Let?

Put this code in your form (General Declaration Section)

' Variable to hold 'VarValue' property value
Private refVarValue As String

Public Property Get VarValue() As String
   VarValue = refVarValue
End Property

Public Property Let VarValue(ByVal strValue As String)
   refVarValue = EnuValue
End Property

Then, when you call the form where you put the code above (ex, Form1)

Form1.VarValue = "Your String goes here"
Form1.Show

'Then on form1 load event, put this code
Private Sub Form_Load()
   Debug.Print refVarValue
End Sub

Try to run it and see what happens.

Thanks so much for the help everyone, the problem is solved! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.