I found some code for text-searching in forms (using an unbound text box and a command button), and adapted it to look like this:
The problem I now have is that the results are now sorted by the primary key of the table this form applies to, instead of alphabetically by surname, which I would like. This is also how I've set it to sort when the form opens. How can I make this bit of code re-sort the results?
Code:
Private Sub Command83_Click()
Dim strPersonRef As String
Dim strSearch As String
'Check txtSearch for Null value or Nill Entry first.
If IsNull(Me![txtsearch]) Or (Me![txtsearch]) = "" Or (Me![txtsearch]) = "Surname" Then
MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criterion!"
Me![txtsearch].SetFocus
Exit Sub
End If
'Performs the search using value entered into txtSearch
'and evaluates this against values in Surname
DoCmd.ShowAllRecords
DoCmd.GoToControl ("Surname")
DoCmd.FindRecord Me!txtsearch
Surname.SetFocus
strPersonRef = Surname.Text
txtsearch.SetFocus
strSearch = txtsearch.Text
'If matching record found sets focus in Surname and shows msgbox
'and clears search control
If Surname = strSearch Then
MsgBox "Matches Found For: " & strSearch, , "Congratulations!"
Surname.SetFocus
txtsearch = ""
'If value not found sets focus back to txtSearch and shows msgbox
Else
MsgBox "Match Not Found For: " & strSearch & " - Please Try Again.", _
, "Invalid Search Criterion!"
txtsearch.SetFocus
End If
End Sub
Comment