Public Variable HELP!

  • Thread starter Alexcamp via AccessMonster.com
  • Start date
A

Alexcamp via AccessMonster.com

I just don't understand what's not working.
Given the following:

Module "publicVariables":
Public NewContract As Boolean


Command button onclick event on form "frmReception":

Private Sub cmdModifContract_Click()
NewContract = False
End Sub

Command Button onclick event on another form:
Private Sub Commande59_Click()
message = MsgBox(NewContract, vbOKOnly)
End Sub

I thought this would be simple enough to work. Declare a public variable,
assign a value to it in a form, extract the value from it from another form.
It's not working !
I am getting a error like Function or variable required or something
("Fonction ou variable attendue" in french..).

Does anyone know what the problem might be ? Is it because I am assigning a
value from a Private sub?

Any hint will be appreciated.
Thank a lot
 
S

Stefan Hoffmann

hi Alex,
Module "publicVariables":
Public NewContract As Boolean
This must be a standard module, not a class or form module.
Private Sub cmdModifContract_Click()
NewContract = False
End Sub
This should work.
Command Button onclick event on another form:
Private Sub Commande59_Click()
message = MsgBox(NewContract, vbOKOnly)
End Sub
Have you an Option Explicit in your first line of your forms module?

Then you need to declare message:

Dim message As Long

message = MsgBox()

Try instead of this a simple

MsgBox NewContract
I am getting a error like Function or variable required or something
("Fonction ou variable attendue" in french..).
At which code line does the debugger stop?


mfG
--> stefan <--
 
K

Klatuu

The first rule of Global variable is "Don't Use Them"
They are not dependable. Any unhandled error that occurs resets all Global
variable values, for example.

Usually, there is a better way to design your application then using Global
variables, but if it is an absolulte necessity, then a better approach is a
Public Function with a static varialbe.

Here is an example:

Public Function saveANumber(Optional NewValue As Variant) As Variant
Static varOldValue
If Not IsMissing(NewValue) Then
varOldValue = NewValue
End If
If IsEmpty(varOldValue) Then
saveANumber = Null
Else
saveANumber = varOldValue
End If
End Function

To assign it a value
SaveANumber(345)
To return the value
X = SaveANumber

If a value has not been assigned, it will return Null.
 
A

Alexcamp via AccessMonster.com

This is a good idea to use a public function. I will definitely give it a try.

Thanks for the insights
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top