Search on Access Form

lousy

New member
Local time
Today, 10:03
Joined
Jun 11, 2011
Messages
3
Hi

I am trying to put a simple search facility on my database without using a combo box/filter. I have got this far below, but this will only find 1 record and then won't search any futher.

Can anyone help me please??

Thanks

Louise

Private Sub Command156_Click()
Dim strnhsRef As String
Dim strSearch As String

'Check txtSearch for Null value or Nill Entry first.
If IsNull(Me![txtsearch1]) Or (Me![txtsearch1]) = "" Then
MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criterion!"
Me![txtsearch1].SetFocus
Exit Sub
End If
'Performs the search using value entered into txtSearch
'and evaluates this against values in strsearch (dob)

DoCmd.ShowAllRecords
DoCmd.GoToControl ("cmdnhsno")
DoCmd.FindRecord Me!txtsearch1

cmdnhsno.SetFocus
strnhsRef = cmdnhsno.Text
txtsearch1.SetFocus
strSearch = txtsearch1.Text

'If matching record found sets focus in strsearch (dob) and shows msgbox
'and clears search control
If strnhsRef = strSearch Then
MsgBox "Match Found For: " & strSearch, , "Congratulations!"
cmdnhsno.SetFocus
txtsearch1 = ""

Else
If strnhsRef = strSearch Then
MsgBox "Match Found For: " & strSearch, , "Congratulations!"
cmdnhsno.SetFocus
txtsearch1 = ""

'If value not found sets focus back to txtSearch and shows msgbox
Else
MsgBox "Match Not Found For: " & strSearch & " - Please Try Date of Birth Search or Create a New Record.", _
, "Invalid Search Criterion!"
txtsearch1.SetFocus
End If
End If


End Sub
 
Have you tried the binoculars. click in a field, then let the binoculars search.

I use a similar thing in code, to make it easier for users. Have a text box to entrer a search string

then have buttons for "first" and "next" searches

code in each is something like this - note the true/false to determine whether search starts from the top, or from the current location


Code:
FIND FIRST

Private Sub btnFirst_Click()
    If Nz(txtSearch, vbNullString) = vbNullString Then Exit Sub
    
    searchcontrol.SetFocus
    DoCmd.FindRecord txtSearch, acAnywhere, False, acSearchAll, False, acCurrent, [COLOR="red"]True[/COLOR]
End Sub
    
FIND NEXT

Private Sub btnNext_Click()
    If Nz(txtSearch, vbNullString) = vbNullString Then Exit Sub
    
    searchcontrol.SetFocus
    DoCmd.FindRecord txtSearch, acAnywhere, False, acDown, False, acCurrent, [COLOR="Red"]False[/COLOR]

End Sub
 

Users who are viewing this thread

Back
Top Bottom