Public Variable Scope

S

scott

I've got a public variable called "userID" declared in a module. When a user
logs into my app through a log in form, I'm setting the "userID" variable
equal to his userID field in a users table after he supplies a
user/password.

For some reason, the "userID" variable is null within my app.

Why would a public variable loose it's value between forms?


CODE*********
Public userID As Integer
 
L

Larry Linson

Public variables can lose their values if you encounter an error and do not
handle it. That may or may not be what you are experiencing. Have you put a
stop in the code just after you think you are setting the variable? Then,
use the immediate window to examine the value of the variable.

A public Variable in a standard Module, in the absence of unhandled errors,
should retain the value, if it was set.

Larry Linson
Microsoft Access MVP
 
S

scott

I changed the "userID" public variable to a string type and it works. Is
there a restriction on creating Public variables as Integer type?

My public declaration now reads:

Public userID As String
 
R

Rick Brandt

scott said:
I changed the "userID" public variable to a string type and it works. Is there
a restriction on creating Public variables as Integer type?

Unless you declared you variable before as a Variant it could not possibly have
been Null. Variant is the only variable type that can contain a Null value. As
soon as you declare a variable as type Integer it will already have the value of
zero. If you even tried to set it to Null you would get an error.
 

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