MSAccess 2003 - VBA for passing a value from one form to another MSAccess 2003 - VBA for passing a value from one form to another vba vba

MSAccess 2003 - VBA for passing a value from one form to another


The best approach in these cases is not to attempted to pass a bunch of variables. It is too much code, and is inflexible. For example, if you need to pass two values, what happens over the years when that requirement grows to 5 values? Trying to maintain and pass a whole whack of values is too much coding work.

Keep in mind that each form in ms-access is really a class object that you can manipulate in code. So, use a object approach here and you find you not only write less code, but your code will be more clean, more modular, no need for global vars, and code you write can often be re-used between different forms.

Here is how:

In general when one form launches another form in the 2nd form in the forms on-open event (in fact, you can even use as late as the on-load event) you can pick up a reference to the PREVIOUS form object. In other words, you can use a object approach here.

At the forms module level, for form I declare a form object as:

Option Compare DatabaseOption Explicit dim frmPrevious       as form 

Then, in the forms on-load event, we go:

Set frmPrevious = Screen.ActiveForm

Now, any code in our form can FREELY use code, events, even varibles declared as public from that previous form in code.

So, if you want to force a disk write of the previous form, and re-load of data.

frmPrevious.Refresh

If you want to set the ID value, then go:

frmPrevious!ID = some value

And, note that you can even declare form previous as a PUBLIC variable for that form, and thus if you two forms deep, you could go:

frmPrevious.frmPrevious!ID = some value

So, simply declare a forms object in EACH forms code module (or at lest the ones where you need to use values in code). The above means any code has a ready made reference to the previous form object. Functions declared as public in a form will become a METHOD of the form, and can be run like:

frmPrevious.MyCustomRefresh

or even things like some option to force the previous form to generate and setup a invoice number:

frmPrevous.SetInvoice

or

frmPrevious.SetProjectStatusOn

So not only can you shuffle values and data back and forth, but you can easily execute features and functions that you build in code for the prevous form.

In fact as a coding standard, MOST of my forms have a public function called MyRefresh.

Note that the beauty of this approach is that you can thus read + use + set values from that previous form. This allows your code to not only receive values, but also set values in that previous form. So this approach is bi-directional. You can shuffle data and values back and forth between the forms. The other advantage here is you NOT restricted to just variables, but can use fields, control values (events, properties) etc.

This approach means that much of the previous form is now at your fingertips.

So don’t try to pass a whole whack of variables. Pass a reference to the form and you have a nice ready made object at your fingertips and it makes this type of coding problem a breeze.


The usual way would be to reference the textboxes in the initial form from the popup form, like this:

Forms!frmInitialForm!tripIDForms!frmInitialForm!OrgID

However, this tightly binds the popup form to the initial form, so that it cannot be used anywhere else in the application.

A better approach is to use OpenArgs:

DoCmd.OpenForm "frmPopup", OpenArgs:=Me.tripID & ", " & me.OrgID

This places your two values into a string, which is passed to the popup form. You can then parse the two values out of the OpenArgs using the Split function.

For more info about passing parameters using OpenArgs, see:http://www.fmsinc.com/free/NewTips/Access/accesstip13.asp