finding items, if not found, message box

Lifeseeker

Registered User.
Local time
Today, 05:07
Joined
Mar 18, 2011
Messages
273
Hi,

I have a form-based search. The code is as follows:

Private Sub Command7_Click()
If Len(cbosearchfield) = 0 Or IsNull(cbosearchfield) = True Then
MsgBox "You must select a field to search."

ElseIf Len(txtsearchstring) = 0 Or IsNull(txtsearchstring) = True Then
MsgBox "You must enter a search string."

Else

'Generate search criteria
GCriteria = "[" & cbosearchfield.Value & "]" & " LIKE '*" & txtsearchstring & "*'"

'Filter frmCustomers based on search criteria
Form_frm_fee_history_browse.RecordSource = "select * from fee where " & GCriteria
Form_frm_fee_history_browse.Caption = "fee (" & cbosearchfield.Value & " contains '*" & txtsearchstring & "*')"

'Close frmSearch
'DoCmd.Close acForm, "frm_lab_Search"

MsgBox "Item Found!"
DoCmd.Close acForm, "frm_search"

End If


End Sub


If an item cannot be found, it will still say "Item Found!". How should the code look like that represents the situation where a search returns no result?

comments/thoughts much appreciated
 
Perhaps:

Code:
Private Sub Command7_Click()
    If Nz(cbosearchfield,"") = "" Then
        MsgBox "You must select a field to search."
    ElseIf Nz(txtsearchstring,"") = "" Then
        MsgBox "You must enter a search string."
    Else
        'Generate search criteria
        GCriteria = "[" & cbosearchfield.Value & "]" & " LIKE '*" & txtsearchstring & "*'"
        Dim rs As Recordset
        Set rs = CurrentDb.OpenRecordset("select * from fee where " & GCriteria, dbOpenSnapshot)
        If rs.RecordCount = 0 Then
            MsgBox "Item Not Found!"
        Else
            'Filter frmCustomers based on search criteria
            Form_frm_fee_history_browse.RecordSource = "select * from fee where " & GCriteria
            Form_frm_fee_history_browse.Caption = "fee (" & cbosearchfield.Value & " contains '*" & txtsearchstring & "*')" 
            'Close frmSearch
            'DoCmd.Close acForm, "frm_lab_Search"
            MsgBox "Item Found!"
            DoCmd.Close acForm, "frm_search" 
        End If
        rs.Close
    End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom