Open (sub)form with command button - link master/child fields

B

ben.biddle

I have a main form bound to a table on customers with a subform bound
to a table on service pricing. I've added a third table that really
only has a memo field for capturing unstructured data on the pricing,
but I've made it a separate table because this sort of unstructured
data is the exception rather than the rule. I would like to add a
button to my subform that will open a simple form for entering the
unstructured data into a memo box. The code I am using follows:

Option Compare Database

Private Sub cmdNotes_Click()
On Error GoTo cmdNotes_Click_Err

If ChildFormIsOpen() Then
FilterChildForm
Forms![sub_frmNotes].SetFocus
Else
OpenChildForm
FilterChildForm
End If

Exit Sub

cmdNotes_Click_Err:
MsgBox Error$

End Sub

Sub Form_Current()
On Error GoTo Form_Current_Err

If ChildFormIsOpen() Then FilterChildForm

Exit Sub

Form_Current_Err:
MsgBox Error$

End Sub

Private Sub FilterChildForm()

If Me.NewRecord Then
Forms![sub_frmNotes].DataEntry = True
Else
Forms![sub_frmNotes].Filter = "[RateID] = " & Me![ID]
Forms![sub_frmNotes].FilterOn = True
End If

End Sub
Private Sub OpenChildForm()

DoCmd.OpenForm "sub_frmNotes"

End Sub
Private Sub CloseChildForm()

DoCmd.Close acForm, "sub_frmNotes"

End Sub
Private Function ChildFormIsOpen()

ChildFormIsOpen = (SysCmd(acSysCmdGetObjectState, acForm,
"sub_frmNotes") And acObjStateOpen) <> False

End Function

I harvested this code by creating a form/subform in the Access wizard.
It generally works fine except when I am adding a new memo record. The
foreign key field does not populate with the primary key field value
for the current record in my pricing subform. If I had a nested
subform, I know I would fix this with the Master/Child Field property,
but because I am using a command button to open the form, I can't set
those properties in design view. I have tried to do it with VBA (I am
more experienced with Excel VBA but I can manage with Access).
Unfortunately Help is decidedly unhelpful with the
LinkChildFields/LinkMasterFields properties. The code, in the open
event of the memo form, is simply:

Private Sub Form_Open(Cancel As Integer)

Forms![sub_frmRates].LinkMasterFields = "ID"
Forms![sub_frmNotes].LinkChildFields = "RateID"

End Sub

This is resulting in run-time error 2465 - Application-defined or
object-defined error. I am not set on using LinkChild/MasterFields. I
know there has to be a relatively straight forward way of making sure
the foreign key is populated with the primary key from the "master"
form, but I feel like I am trying to stumble across it in the dark
right now. Help please!

Thanks in advance for sharing your time and insight.
 

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