In the first form, I have a field call "Registration Number" and some other
additional fields. The "Registration number" is the primary key. After
filling up the first form, there will be a button at the bottom of the form
that is link to the Second form.
When the button is clicked, the second form appears. In the second form,
there's also a field call "Registration number". This "Registration number"
field in the second form is linked to the one in the First form. What I need
to know is that whether it is possible to have the "Registration number"
field in the second form to be filled up according to the "Registration
number" stated in the first form when the user clicked on the button in the
first form.
Usually, what the user normally does is that he/she will copy the
"Registration number" in the first form and paste it in the "Registration
number" of the second form when it appears after the button is clicked.
Again:
There is a very easy, no code required, intuitive and simple for the user,
tool built in to Access to do this: put the second form onto the first form as
a Subform, using [Registration number] as the master and child link field. If
you're short of screen space, you can use a Tab Control with the Table1 fields
on the first page and put the subform on the second page.
You're done. No programming, no maintenance, no hassles, the user doesn't even
need to click a button.
If you are *consciously and intentionally* rejecting this user interface in
favor of the more difficult alternative, here's some sample code you can
adapt.
Private Sub cmdOpenForm2_Click()
Dim strOpenArgs As String
Dim strCriteria As String
Dim strFormName As String
On Error GoTo Proc_Error
strFormName = "frmMyForm2"
strCriteria = "[Registration Number] = " & Me![Registration Number]
' or if Registration Number is a Text field,
' strCriteria = "[Registration Number] = '" & Me![Registration Number] & "'"
strOpenArgs = Me![Registration Number]
DoCmd.OpenForm strFormName,WhereCondition:=strCriteria,OpenArgs:=strOpenArgs
Proc_Exit: Exit Sub
Proc_Error:
MsgBox "Error " & Err.Number & " in cmdOpenForm2_Click:" _
& vbCrLf & Err.Description
Resume Proc_Exit
End Sub
and in the Open event of the second form
Private Sub Form_Open(Cancel as Integer)
Const Quote as String = """"
If Me.OpenArgs & "" <> "" Then
Me![Registration Number].DefaultValue = Quote & Me.OpenArgs & Quote
End If
End Sub
John W. Vinson [MVP]