Carrying a field value to another form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have created two forms containing the same data from the same table. On
one of the forms each field is locked to prevent any changes, and I want to
create a command button that the user can click to go to the other form where
he/she can update the data. However, I can't find a way to carry the key
forward into the second form. On the protected form, the user enters a last
name, and then when the update button is pushed, I want the key from that
record passed to the second form to select the same record for updating. I
can get the second form to come up blank, or to request the user to enter the
key field, but I can't get it to automatically carry forward. Any help is
appreciated.
 
When you open the form with the OpenForm method be sure to use either the
where argument (or if your tables are ODBC - base the form on a query that
references the first form to obtain its criteria). This will open the form
to the correct records if any exist.

When you add a record with the new form, you need to obtain the correct
foreign key value. There are several ways to do this. Two are:
1. Pass the first form's key in the OpenArgs argument of the OpenForm method.
2. Refer to a control on the first form to obtain the value.
In either case, you will need only a single line of code in the second
form's BeforeInsert event:
Me.MyKeyID = Me.OpenArgs
or
Me.MyKeyID = Forms!frmYourFirstForm!MyKeyID

I suggest the BeforeInsert event because it fires immediately after the
first character is typed in the form so the user will see the key value (if
it is visible) as soon as he starts typing. The benefit of this event over
the current event is that your code doesn't dirty the record. The user
dirties the record and then your code runs. This will prevent confusion
should the user elect to not save the record once he goes to this form.
 
Back
Top