简体   繁体   中英

MS Access Nested Subform reference error

I'm having trouble referencing a subform within a subform on a form.

Private Sub Command37_Click()
Dim UMsge As String

Me.Filter = "ID=" & Me.ID
Me.FilterOn = True

UMsge = "Swimmer's Name: " & Forms![Parents]![Swimmers Subform].Form![Memb First Name] & " " & Forms![Parents]![Swimmers Subform].Form![Memb Last Name] & vbCrLf & vbCrLf & "Roster Group: " & Forms![Parents]![Swimmers Subform].Form![Roster Group] & vbCrLf & vbCrLf & "Monthly Fee: " & Format(Forms![Parents]![Swimmers Subform].Form![Parents - Payment subform].Form![GroupMonthlyPrice], "Currency") & vbCrLf & vbCrLf & "Thank You!"

DoCmd.SendObject acSendForm, "Parents", acFormatPDF, Me.Email, , , "Monthly Fees Owed As Of " & DateSerial(Year(Date), Month(Date), Day(Date)), UMsge, True

End Sub

It's only this last piece:

Format(Forms![Parents]![Swimmers Subform].Form![Parents - Payment subform].Form![GroupMonthlyPrice], "Currency")

FormView: 在此处输入图片说明

First, it looks like you using a sub datasheet. They are somewhat different then sub forms (in that you cannot name them, they are named automatic for you).

Further, since your button is in the same form, for the top most form, we can use “me” keyword. This will simple your code quite a bit.

Assuming we had 3 nested sub forms, we could use this syntax:

strName = Me.Swimmers_SubForm.Form![memb First Name] & " " & _
          Me.Swimmers_SubForm.Form![memb Last Name]

The above will get you the name in the first nested sub form.

However, you next sub form looks VERY much like a Subdatasheet.

Two things:

If this is a sub data sheet (as opposed to sub form, then if this form has never been expanded, then the reference will break (the child form does not exist). This is often the same for if the sub datasheet has no child records.

So you need (should) to trap this error.

Also, if this is a sub data sheet, then you cannot give the sub form control a name – it starts at child0 and that likely is your issue.

Note that the record return will be the highlighted row, and in your example screen shot, I see two rows, and I DO NOT see the column called [GroupMonthlyPrice]. However I will assume that this is the column you wish to reference? Or is do you want the column called [Monthly Fee] ?

So to reference that 3rd nested form, the syntax would be this:

Me.Swimmers_SubForm.Form!Child0.Form![GroupMonthlyPrice]

So to break all this down, I would use this code:

Dim f       As Form     ' ref to sub form
Dim UMsge   As String

Set f = Me.Swimmers_Subform.Form

UMsge = "Swimmer's Name: " & f![Memb First Name] & " " & f![Memb Last Name] & vbCrLf & _
        "Roster Group: " & f![Roster Group] & vbCrLf & vbCrLf & _
        "Monthly Fee: " & Format(f!Child0.Form![GroupMontlyPrice], "Currency")

The referenced "f" in above is not required, but certainly makes code much more readable.

Note that you might have to replace child0 with the name of the sub form control, but as I stated, this looks like a sub datasheet, and quite sure the first sub datasheet is called Child0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM