Dlookup seems to work in a form but not a subform?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Access2002
I am using the code below to store the value from a table in a text box
based on the results from a previous combo box.

The code below when used in a Form works fine, but when I try it in a
subform, it won't work.
Is there a problem with using Dlookup in a subform?

Private Sub varDutyID_AfterUpdate()
Me.TextDuty = DLookup("[LookupDuty]", "[Lookup1020Duty]",
"[LookupDutyID]=[Forms]![1000Model]![varDutyID]")
End Sub
 
It works just fine in a subform, but, if you're using the full reference to
that form as if it were a form and not a subform, then it won't work the
same way.

A subform is not "open" on its own as a form; it's a child of the main form,
so the reference to it must include the full path through the main form.

However, in your case, assuming that the code is using the value of
varDutyID from the form/subform itself, simplify and genericize the code by
using the Me object instead to get to the control's value:

Private Sub varDutyID_AfterUpdate()
Me.TextDuty = DLookup("[LookupDuty]", "[Lookup1020Duty]",
"[LookupDutyID]=" & Me.[varDutyID])
End Sub

This will work regardless of whether the form is on its own or is a subform.
 
Back
Top