How to check variable is empty

D

default105

I know there are different variables: string, long, integer, variant, double,
etc... but is there an easy way to see if one has been set. The reason I ask
is I have a public variable that I use and until yesterday when updating the
front end I front and error that could occur if the user did something in a
particular order. This would cause an error and empty my variable string. I
found the error and it was my own fault that I did not have error coding in
to start. I would not like to have to call the procedure everytime before I
use the variable so I was wondering if there is an easy way to check a
variable before I used a procedure to call it. Thanks.
 
A

Allen Browne

In VBA, a string variable is initialized to a zero-length string (zls),
numbers to zero, booleans to False, and variants to Empty (a special type
that equates to zero and also to a zls.)

Declare a boolean to track whether the variables have been set. Set this to
True in the code that initializes your variables. If it's False, things have
been reset.

The issue you describe is one of the problems with public variables in VBA,
especially while developing/maintaining when you can reset quite often. My
recommendation would be to avoid the use of public variables if possible.
Create variables with as narrow a scope and as short a lifetime as
practical. Explicitly pass them from routine to routine where applicable.

In a typical Access app, you really don't need many public variables. Where
I do have to use them, I don't rely on them for very long (e.g. setting a
filter string immediately before an OutputTo or Printout, and then using and
clearing it almost immediately in Report_Open.)
 
D

default105

Thanks for the prompt reply Allen. I have read through your access tips
many times and do appreciate the time you take to help people like me who are
learning as we go. I have many books but must say I have learned more from
your code and tips posted than I have from hardback references.
The situation I have is where I do an ADO call to get the current user name
based on the logon username. This code checks to see if the user is new to
the database and opens a form appropriately to get all the relevent
information about them or if the user is known it opens another form to allow
the user to proceed with the task at hand. It sets the user identity and
saves it as a public string variable for the next line in the code. I am a
little hesitate to call it repeatedly because it uses an ado recordset even
though I do have error coding. So would it be a problem to make repeated
calls of a procedure that uses an ado recordset? I do not want to cause any
problems, either front end or backend related. If you would like to see the
code I would gladly post it.

Thanks,

Pete B.
 
A

Allen Browne

If you want to go ahead and use it, the public boolean is easy enough to
code

Assuming you call the Init() function to run your initialization code, it
looks like this:
Public bstrUser As String
Public gbInit As Boolean
Public Function Init()
gstrUser = "whatever"
gbInit = True
End Function

Then when you want to use the string you add one line before you call it,
e.g.:
If Not gbInit Then Call Init()
Debug.Print bstrUser

As I say, I prefer to declare a local variable in a routine, and assign the
value to it before executing any loop where you need it repeatedly.

Another possible approach is to open a hidden form when the application
starts, and store the user name there. Some developers use a hidden form to
simulate an Application_Close event (since Access doesn't have one), so the
hidden form can serve a couple of purposes. The point here is that the form
controls don't lose their values when you reset.

Thanks for your kind words.
 
Top