Passing information to a form
We very often want to pass information into an Access form to change the behaviour
of that form - perhaps it's the ID code of the client's order so that the form can
display information about that order. Whatever the reason, there are three ways of
doing this in Microsoft Access. One way is easy but wrong, one way is clever but clumsy,
the third way is the best.
Global variables
The easiest way to get information into a form is to use a global variable. This will
be available to all the program code in the database so you can store some information
from one form and then retrieve it from another. It's very quick and easy but it does
have it's dangers and it does make maintenance more difficult.
A global variable is convenient because it can be used from anywhere in the database
but this convenience is also its greatest drawback. If you declare a global variable:
Public
gstrOrderID
as String
then you'll be able to let the user enter an order number before the form opens and
still be able to read that order number when the form is running. That's solved your
immediate problem but what will happen if another programmer wants to do the same thing
in another part of the system some months later? If they choose to use the same technique
and they accidentally use the same variable name then your original form might stop
working. A global variable is just a bug that's waiting to happen.
Referring to an invisible textbox on another form
A clever programmer will know that it's possible to read the value of a textbox on another
form. Something like:
Forms("PickOrder").txtOrderid.Value
will pick up the text from the textbox named 'txtOrderID' on the form named 'PickOrder'.
The textbox doesn't need to be visible to the user so you can hide it behind something
else or just set its Visible property false.
This is better than using a global variable but it does mean that these two forms are now
tied together. The second form can only be called from the PickOrder form and if PickOrder
isn't on screen then the second form will crash. It's a safe technique but it does
restrict your flexibility.
Using the OpenArgs property
Every form has an OpenArgs property which holds the value
of the parameter that was passed to the form. If you're wanting to pass "ABC123" in as
the order id then just specify this as the value of the OpenArgs parameter when you
open the form:
DoCmd.OpenForm
FormName:="ViewOrder",
OpenArgs:="ABC123"
Pick the value up in the form's Load event:
strOrderID = Me.OpenArgs
and you've got a safe, easy way of getting a parameter into a form.
You can only pass a single parameter with OpenArgs but it is possible to pack
multiple parameters
into a single string argument.
|