Group:  Microsoft Access ยป microsoft.public.access.formscoding
Thread: Difference Between Global Const and Public Variable

Geek News

Difference Between Global Const and Public Variable
R Tanner <nouveauricheinvestments[ at ]gmail.com> 12/19/2008 4:55:36 PM
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...
Re: Difference Between Global Const and Public Variable
"Albert D. Kallal" <PleaseNOOOsPAMmkallal[ at ]msn.com> 12/19/2008 5:38:01 PM
"R Tanner" <nouveauricheinvestments[ at ]gmail.com> wrote in message
news:83ea8ec8-5334-4d2e-953e-77629df7811d[ at ]s16g2000vbp.googlegroups.com...
[Quoted Text]
> 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

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Assignment to constant not permitted
---------------------------
OK Help
---------------------------

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKallal[ at ]msn.com


Re: Difference Between Global Const and Public Variable
"Stuart McCall" <smccall[ at ]myunrealbox.com> 12/19/2008 5:38:12 PM
"R Tanner" <nouveauricheinvestments[ at ]gmail.com> wrote in message
news:83ea8ec8-5334-4d2e-953e-77629df7811d[ at ]s16g2000vbp.googlegroups.com...
[Quoted Text]
> 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.


Re: Difference Between Global Const and Public Variable
"Dirk Goldgar" <dg[ at ]NOdataSPAMgnostics.com.invalid> 12/19/2008 5:42:43 PM
"R Tanner" <nouveauricheinvestments[ at ]gmail.com> wrote in message
news:83ea8ec8-5334-4d2e-953e-77629df7811d[ at ]s16g2000vbp.googlegroups.com...
[Quoted Text]
> 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.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Re: Difference Between Global Const and Public Variable
R Tanner <nouveauricheinvestments[ at ]gmail.com> 12/19/2008 6:08:32 PM
On Dec 19, 10:42 am, "Dirk Goldgar"
<d...[ at ]NOdataSPAMgnostics.com.invalid> wrote:
[Quoted Text]
> "R Tanner" <nouveauricheinvestme...[ at ]gmail.com> wrote in message
>
> news:83ea8ec8-5334-4d2e-953e-77629df7811d[ at ]s16g2000vbp.googlegroups.com...
>
> > 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.
>
> --
> Dirk Goldgar, MS Access MVPwww.datagnostics.com
>
> (please reply to the newsgroup)

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...:)
Re: Difference Between Global Const and Public Variable
Dale Fye 12/19/2008 7:22:03 PM
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.

"R Tanner" wrote:

[Quoted Text]
> On Dec 19, 10:42 am, "Dirk Goldgar"
> <d...[ at ]NOdataSPAMgnostics.com.invalid> wrote:
> > "R Tanner" <nouveauricheinvestme...[ at ]gmail.com> wrote in message
> >
> > news:83ea8ec8-5334-4d2e-953e-77629df7811d[ at ]s16g2000vbp.googlegroups.com...
> >
> > > 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.
> >
> > --
> > Dirk Goldgar, MS Access MVPwww.datagnostics.com
> >
> > (please reply to the newsgroup)
>
> 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...:)
>

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net