Programming for Search Form

S

Steve

Good Morning,
I am working on creating a search form and I got the
following code from someone. It looks like it should work
but when it opens the results form it pulls up all of the
records. Any help would be greatly appreciated.

Private Sub cmdSearch_Click()

Dim sFilter As String

On Error GoTo cmdSearch_ClickERR

If txtfirstname = vbNullString Or IsNull(txtfirstname)
Then
Call MsgBox("Please enter a First Name..", vbOKOnly
+ vbInformation, "Search")
Exit Sub
End If

DoCmd.OpenForm "SearchResults"

sFilter = "First Name Like'" & txtfirstname

Form_SearchResults.Filter = sFilter
Form_SearchResults.FilterOn = True

If Form_SearchResults.RecordsetClone.RecordCount < 1
Then
Call MsgBox("No records found...", vbOKOnly +
vbInformation, "Search")
DoCmd.Close acForm, "SearchResults"
Exit Sub
End If

cmdSearch_ClickEXIT:
'Do any clean up here..
sFilter = vbNullString
Exit Sub

cmdSearch_ClickERR:
'Place your custom error handling code here..
Resume cmdSearch_ClickEXIT

End Sub


Thanks,
Steve
 
K

Kevin

Try Me.Refresh after the filter is applied and FilterOn is
set to True. I think the problem is your opening the form
unfiltered which would bring up all records. If you
refresh the form after the filter is applied, this should
resolve the problem. Another method would be to open the
form filtered. Look in help for more information on this.

Hope this helps!

Kevin
 

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