views:

634

answers:

3

So how can I pass a value from one form to another? For example: The user select's an organization from a list and this opens up a trip form that allows a user to enter various information regarding the trip. At one place I would like to add another little pop up form where they can enter contact information (just a name and phone for POC) of the organization they are visiting.

So when that initial form opened from the selection screen it has two IDs that are simply hidden in text boxes (one being the tripID, and the other being the OrgID), so how do I pass these to the second little pop up form so that the contact information has the relative IDs with it.

Thanks.

+1  A: 

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

Forms!frmInitialForm!tripID
Forms!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

Robert Harvey
thanks! thanks for the resource as well!
Justin
+1  A: 

This one could help

MS Access: passing parameters from one access form to another

Philippe Grondier
thanks as always Philippe!
Justin
+3  A: 

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 Database
Option 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 pubic 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 pubic 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.

Albert D. Kallal
thanks very much! i especially appreciate the explaination; helps me learn! thanks albert!
Justin
The approach is very interesting. I personnally use something quite similar (at least in the spirit) with a specific object that holds the collection of all open windows, and where windows are declared as instances of forms. This allow me to have a serie of specific 'window' methods and properties in addition to the standard forms methods and properties.
Philippe Grondier
I weep, almost, with gratitude. +1
Smandoli