Cannot pass record id to child form

D

David F

If I have an existing child record then the child form opens to the proper
child record and all is well. If not, the child form opens to a blank record
and when I try to save it I get the message: You cannot add or change a
record becuase a related record is required in table 'order'.

The relation between the two tables "order" and "orderfile" is one-to-one.
The code behind the click event in the "order" (parent) form is:

Private Sub View_Script_Click()

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "f_order_file"

'save the current record to make sure ID exists
DoCmd.RunCommand acCmdSaveRecord

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

End Sub


The code behind the "orderfile" form (child) load event looks like this:

Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then
[txtName] = Me.OpenArgs
End If
End Sub

The always name is passed to the child form but if a "orderfile" (child)
record does not already exist, no orderid is passed and I cannot save the
new child record I try to create.

How do I reference the order ID passed to the form so I can create a new
child record?
 
A

Allan Bach

Try using the Default Value property of the child field in the subform. Set it to the value of the parent field. Or, try changing the OpenArgs parameter of the DoCmd method to Me.OrderID
 
M

MacDermott

Your current code says, in effect, "Look for a record in your second table
with orderid equal to the current orderid on your main form."
It gives no instructions for adding a new record if that one isn't found
(doesn't exist yet).
Here's one way to accomplish that task:

You could write your Form_Load like this:
Private Sub Form_Load()
If Me.NewRecord=true then
[orderid]=Forms!MainForm![orderid]
End If

If Not IsNull(Me.OpenArgs) Then
[txtName] = Me.OpenArgs
End If
End Sub

Of course you'll need to use your own calling form's name instead of
MainForm.
And that MainForm will need to stay open while this code is executing.

HTH
- Turtle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top