Difference Between Global Const and Public Variable

R

R Tanner

Hi,

I have an application where a user must sign in before continuing.
Would it be better to assign the user's sign in status to a public
variable or a global constant or are these virtually the same...
 
A

Albert D. Kallal

R Tanner said:
Hi,

I have an application where a user must sign in before continuing.
Would it be better to assign the user's sign in status to a public
variable or a global constant or are these virtually the same...

You can't change a global constant. The very term/word "constant" means
exactly that...it is a constant, and is defined at compile time...not at
runtime.

You cannot modify the value(s) of constants in your code...it isnot
allowed...

So, if in code you go:

Const Pi As Double = 3.14159265

If you in code go:

Pi = 123

You not even be able to compile the code, you get the error
 
S

Stuart McCall

R Tanner said:
Hi,

I have an application where a user must sign in before continuing.
Would it be better to assign the user's sign in status to a public
variable or a global constant or are these virtually the same...

Well you can't assign anything to a constant at runtime. It means what it
says: 'the value will remain constant throughout the life of the app'.

Yes you could use a public variable, but be aware that on the 1st unhandled
error will clear out the values of all public vars (unless your app is an
mde).

Far better to set up a table for the purpose, and write the value into that.
 
D

Dirk Goldgar

R Tanner said:
Hi,

I have an application where a user must sign in before continuing.
Would it be better to assign the user's sign in status to a public
variable or a global constant or are these virtually the same...


A constant ("Const x") is a constant, and its value can't be changed at run
time. So you can't use a constant for what you are doing. A Public
variable in a standard module is the same as a Global variable, and is
probably what you want. However, be aware that an unhandled error will rest
all variables, and so you must implement error-handling in your code to
ensure that there are no unhandled errors, if you want to be sure that the
user's sign-in status is not lost.

That may not be a problem, if all you need to do is ask the user to sign in
again in the (hopefully) rare event of an unhandled error. Alternatives to
public variables are (a) saving the value in a text box on a hidden form,
and (b) saving the status in a local table. Each has its drawbacks, of
course, though having a hidden form is convenient for lots of things.
 
R

R Tanner

A constant ("Const x") is a constant, and its value can't be changed at run
time. So you can't use a constant for what you are doing. A Public
variable in a standard module is the same as a Global variable, and is
probably what you want. However, be aware that an unhandled error will rest
all variables, and so you must implement error-handling in your code to
ensure that there are no unhandled errors, if you want to be sure that the
user's sign-in status is not lost.

That may not be a problem, if all you need to do is ask the user to sign in
again in the (hopefully) rare event of an unhandled error. Alternatives to
public variables are (a) saving the value in a text box on a hidden form,
and (b) saving the status in a local table. Each has its drawbacks, of
course, though having a hidden form is convenient for lots of things.

When I first created this app, I did something a bit different with
the user sign in status.. I based it off of a locked text box on my
main form. I have a module that, when the main form is loaded, checks
to see if the text is null and if so, the only thing the user can see
is the sign in fields. I just wanted to get some opinions on what the
difference between public variables and constants are...and I really
appreciate your input..I will keep your responses in mind in my
coding, as I am sure they will come in handy...:)
 
D

Dale Fye

I like to use functions to store many of my variables. This works similar to
having textboxes on a hidden form, except I just find it easier. It
overcomes the problem that Stuart mentions (dropping values on unhandled
errors), and avoids me having to maintain a separate form. It also allows
for some error checking and other options that using a textbox doesn't do for
you. You can also use a function call in a query (which you cannot do with a
variable), and I find that since my functions are all typed (declared as a
particular data type), I don't need to declare them as parameters in the
query.

I generally create a code module (mod_Global_Variables) and put all the code
in that module. I take advantage of the use of static variables, so that
each time I call the function, it retains the previously set value, if I
don't overwrite it. An example of this might be:

Public Function fnEmpID(Optional SomeValue As Variant = Null, Optional Reset
As Boolean = False) As Long

Static EmpID As Long
Dim varInput As Variant

If Reset = True Then EmpID = 0

If Not IsNull(SomeValue) Then
EmpID = SomeValue
ElseIf EmpID = 0 Then
Do
varInput = InputBox("Enter an employeeID")
If IsNumeric(varInput) Then
EmpID = CLng(varInput)
Exit Do
End If
MsgBox "Input must be numeric"
Loop
End If

fnEmpID = EmpID

End Function

I define the parameters to be passed as optional, so that if none are
passed, the function returns the value previously assigned to the static
variable. I generally declare the main parameter "SomeValue" as a variant
and set the default value to NULL, so that I can test use that to determine
whether a value was passed. I also frequently include a "Reset" variable so
that I can clear out the static variable for testing purposes.

To set the value of fnEmpID, you just add a line of code to your application:

fnEmpID 123
or
call fnEmpID(123)

To use this in a query, you might write:

SELECT * FROM tbl_Employees WHERE [EmpID] = fnEmpID()

The neat thing about this is that since the default value of a numeric
variable is zero (0), I can test in my function to determine whether the
value has been set. If not, I can open a form or use an inputbox to allow
the user to input the value, or could lookup a default value in another table.

Another advantage is that when you are debugging your code, you don't have
to have the "hidden form" that Dirk mentioned open and populated. You can
just call the function from the immediate window and pass it a value, or
check its value.

Hope this all makes sense.
--
HTH
Dale

email address is invalid
Please reply to newsgroup only.
 

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