Populating the Primary Key from one Form to another

Sweetnuff38

Registered User.
Local time
Today, 08:37
Joined
Dec 7, 2007
Messages
74
I have a form with a button that has an onclick procedure that will open another form and show the matching primary keys (when there is data relating to that key). I should mention that both forms are attached to different queries (tables).

My problem is when adding a new record in the first form, I would like the primary key to automatically populate from the form I am working on to the other form so I dont have to fill it in.

This is the code behind the button now, what would I need to add to accomplish the above? Any help would be appreciated.

Private Sub cmdassessmentform_Click()
On Error GoTo Err_cmdassessmentform_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmInspectionAssessment"

stLinkCriteria = "[IItemNumber]=" & Me![IItemNumber]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmdassessmentform_Click:
Exit Sub
Err_cmdassessmentform_Click:
MsgBox Err.Description
Resume Exit_cmdassessmentform_Click

End Sub
 
You can pass he Item you want to add through the OpenArgs property of the OpenForm method. For example:

If you want to pass a String to the Opening Form:

DoCmd.OpenForm stDocName, , , , acFormAdd, , "My String Value"

If you want to pass a Number (3210) to the Opening Form:

DoCmd.OpenForm stDocName, , , , acFormAdd, , 3210


If you want to pass a Date to the Opening Form:

DoCmd.OpenForm stDocName, , , , acFormAdd, , #01/01/2008#


If you want to pass a Variable to the Opening Form:

DoCmd.OpenForm stDocName, , , , acFormAdd, , MyVariablName


Regardless of whichever you choose you will need to apply he code below into the OnCurrent event of the Form that will be opened:

Code:
Private Sub Form_Current()
   If Not IsNull(Me.OpenArgs) And Me.NewRecord Then
      Me.[I][COLOR="Red"]MyFormFieldName[/COLOR][/I] = Me.OpenArgs
   End If
End Sub

Change the items in red italic to suit your needs.

.
 
After reading your post I am wondering if you are duplicating fields in two tables....
My problem is when adding a new record in the first form, I would like the primary key to automatically populate from the form I am working on to the other form so I dont have to fill it in.
If so, don't. :)
On your Onclick event to open the second form do something like this
Code:
DoCmd.OpenForm "form2", acViewNormal, , _
"YourID = Forms!form1!YourID"
 

Users who are viewing this thread

Back
Top Bottom