Global Variables

R

RG

I have an application that has global variables define and used in a number
of forms and modules. For instance, there is a variable called breakmode,
when set to true, would display debugging information. My guess is that you
could pass the value to this variable without having to change the code. If
so, where can I set it?

Thanks in advance
 
A

Albert D. Kallal

You can type in "scope" in the code help to get more info.

The simple issue is if you define the variable as public in a **standard**
code mode (not a forms, or report module) then that variable is then global
can be used by any code in a form or report.
My guess is that you could pass the value to this variable without having
to change the code. If so, where can I set it?

You can set the varible anywhere you please in code (say, you start up
form). However, keep in mind that un-handeled errors will re-set the value.
And, you can't use use variables in sql experssions, or as an expression in
a text box.
 
K

Klatuu

My favorite workaround for this is a static function in a standard module.
Has none of the limitations of a global variable. For example:

Static Function GetSSN(Optional ByVal varNewSSN As Variant) As String
Dim varSSN As Variant

If Not IsMissing(varNewSSN) Then
varSSN = varNewSSN
End If
GetSSN = varSSN
End Function
 
A

Albert D. Kallal

Klatuu said:
My favorite workaround for this is a static function in a standard module.
Has none of the limitations of a global variable. For example:

Static Function GetSSN(Optional ByVal varNewSSN As Variant) As String
Dim varSSN As Variant

If Not IsMissing(varNewSSN) Then
varSSN = varNewSSN
End If
GetSSN = varSSN
End Function

But, an unhandled error will still re-set the value...will it not?
 
K

Klatuu

I would have to test it for that. I have never lost a value using that
technique, but then I am a bit anal when it comes to error handling.
My main reason for using such functions rather than a global variable is
they can be used in SQL.
 

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