Why doesn't this work inside a subform

  • Thread starter Thread starter Jared Evans
  • Start date Start date
J

Jared Evans

Hey all.

I have this code inside a form (subfrmFind) which searches out records with
letters from the last name. It works perfectly (though i would like some
tips on how i could make the coder nicer and more powerful). Anyway i build
a new form and i add a subform, the subform is this subfrmFind. however i
can't get it do it's searches anymore. Whats going on, whats wrong with the
code?

Private Sub cmdFind_Click()
txtfindLast = InputBox("Please enter a Letter from the lastname of the
Visitor")
txtfindLast = txtfindLast & "*"
DoCmd.ApplyFilter , "[Lastname] LIKE Forms!subfrmFind!txtfindLast"
End Sub

Jared Evans
 
You're trying to refer to a form field in your Filter, but you really want
to refer to the variable in your routine:

DoCmd.ApplyFilter , "[Lastname] LIKE '" & txtfindLast & "'"

Exagerated for clarity:

DoCmd.ApplyFilter , "[Lastname] LIKE ' " & txtfindLast & " ' "

Now, you'll run into problems if the string stored in txtfindLast contains a
single quote (apostrophe). If that may be the case, try

DoCmd.ApplyFilter , "[Lastname] LIKE '" & Replace(txtfindLast, "'", "''") &
"'"

Again exagerated:

DoCmd.ApplyFilter , "[Lastname] LIKE ' " & Replace(txtfindLast, " ' ", " ' '
") & " ' "
 
Back
Top