Adding a Message Box (1 Viewer)

tarcona

Registered User.
Local time
Today, 14:46
Joined
Jun 3, 2008
Messages
165
I made a search form. I want to add a message box when there are no results. What would the code be to add a message when the search returned no results?

I know you have to put:
MsgBox "Your search returned no results. Please check the spelling and try again.", vbInformation, "Invalid Search Criterion"

But where would I put this? What would be the statement?
This is my code:

Code:
Private Sub btnSearchAccounts_Click()
    Dim strWhere As String   ' The Criteria String
    Dim lngLen As Long       ' Length of the criteria string to append to
 
' Adds LIKE Company field match to the string
    If Not IsNull(Me.txtSearchCompany) Then
        strWhere = strWhere & " ([Company] Like '" & Me.txtSearchCompany & "*') AND "
    End If
 
lngLen = Len(strWhere) - 5
    If lngLen <= 0 Then
        MsgBox "Please try again.", vbInformation, "Invalid Search Criterion"
 
    Else
        strWhere = Left$(strWhere, lngLen)
        'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
        Debug.Print strWhere
 
        ' Apply the string as the form's Filter.
        Me.Filter = strWhere
        Me.FilterOn = True
    End If
 
End Sub
 

KenHigg

Registered User
Local time
Today, 15:46
Joined
Jun 9, 2004
Messages
13,327
My suggestion is to plug in a dcount() to see if there are any records before you applied the filter...
 

tarcona

Registered User.
Local time
Today, 14:46
Joined
Jun 3, 2008
Messages
165
I just looked at some DCount() information and I dont seem to quite get it. Could you help me out a little more...
 

KenHigg

Registered User
Local time
Today, 15:46
Joined
Jun 9, 2004
Messages
13,327
You can put your criteria in the dcount() first. And if the result = 0 then you know to tell the user such -
 

KenHigg

Registered User
Local time
Today, 15:46
Joined
Jun 9, 2004
Messages
13,327
Sorry Tarcona, I have to bug out, maybe someone else can jump in...
 

boblarson

Smeghead
Local time
Today, 12:46
Joined
Jan 12, 2001
Messages
32,059
Something like this - be sure to put in the applicable names of YOUR real items:

Code:
If DCount("YourFieldNameInTheTable", "YourTableName", "[Company] Like '" & Me.txtSearchCompany & "*') > 0 Then
   ...Run your code here
Else
   Msgbox "No Records Found"
   Exit Sub
End If
 

tarcona

Registered User.
Local time
Today, 14:46
Joined
Jun 3, 2008
Messages
165
Awesome. One more question...sorry.
I have multiple fields that I search from. In the example code I supplied, I only listed one field. I have 4, I believe. How do you DCount all of them at the same time?
 

boblarson

Smeghead
Local time
Today, 12:46
Joined
Jan 12, 2001
Messages
32,059
It would use the same final filter string:

"[Company] Like '" & Me.txtSearchCompany & "*' And [AnotherField]='" & Me.txtWhatever & "*' AND [SomeDateField]>#" & Me.DateFieldName & "#"

etc.
 

tarcona

Registered User.
Local time
Today, 14:46
Joined
Jun 3, 2008
Messages
165
I tried it and it didnt work. I am probably not doing it right.
I am still confused. . . .
 

boblarson

Smeghead
Local time
Today, 12:46
Joined
Jan 12, 2001
Messages
32,059
What are ALL of the actual names of your fields you are looking to filter by and what is the form name and control names of the filtering form?
 

tarcona

Registered User.
Local time
Today, 14:46
Joined
Jun 3, 2008
Messages
165
This is my actual code:

Code:
Private Sub btnSearchAccounts_Click()
 
    Dim strWhere As String   ' The Criteria String
    Dim lngLen As Long       ' Length of the criteria string to append to
 
    ' Adds LIKE Company field match to the string
    If Not IsNull(Me.txtSearchCompany) Then
        strWhere = strWhere & " ([Company] Like '" & Me.txtSearchCompany & "*') AND "
    End If
 
    ' Adds EXACT State field match to the string
    If Not IsNull(Me.txtSearchState) Then
        strWhere = strWhere & "([State] = """ & Me.txtSearchState & """) AND "
    End If
 
    ' Adds LIKE Zip field match to the string
    If Not IsNull(Me.txtSearchCity) Then
        strWhere = strWhere & "([City] LIKE ""*" & Me.txtSearchCity & "*"") AND "
    End If
 
    ' Adds EXACT Account ID match to the string
    If Not IsNull(Me.txtSearchAccountID) Then
        strWhere = strWhere & " ([Account ID] Like """ & Me.txtSearchAccountID & """) AND "
    End If
 
lngLen = Len(strWhere) - 5
    If lngLen <= 0 Then
        MsgBox "Please try again.", vbInformation, "Invalid Search Criterion"
 
    Else
        strWhere = Left$(strWhere, lngLen)
        'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
        Debug.Print strWhere
 
        ' Apply the string as the form's Filter.
        Me.Filter = strWhere
        Me.FilterOn = True
    End If
 
End Sub

So I am filtering by Company, State, City, and Account ID.

My form name is "AllRecordsSearch"
 

boblarson

Smeghead
Local time
Today, 12:46
Joined
Jan 12, 2001
Messages
32,059
Try adding a DCOUNT here (see red and replace the applicable parts in the code with the actual table and field name because I don't know what your form is base off of, I couldn't include them):
Code:
Private Sub btnSearchAccounts_Click()
 
    Dim strWhere As String   ' The Criteria String
    Dim lngLen As Long       ' Length of the criteria string to append to
 
    ' Adds LIKE Company field match to the string
    If Not IsNull(Me.txtSearchCompany) Then
        strWhere = strWhere & " ([Company] Like '" & Me.txtSearchCompany & "*') AND "
    End If
 
    ' Adds EXACT State field match to the string
    If Not IsNull(Me.txtSearchState) Then
        strWhere = strWhere & "([State] = """ & Me.txtSearchState & """) AND "
    End If
 
    ' Adds LIKE Zip field match to the string
    If Not IsNull(Me.txtSearchCity) Then
        strWhere = strWhere & "([City] LIKE ""*" & Me.txtSearchCity & "*"") AND "
    End If
 
    ' Adds EXACT Account ID match to the string
    If Not IsNull(Me.txtSearchAccountID) Then
        strWhere = strWhere & " ([Account ID] Like """ & Me.txtSearchAccountID & """) AND "
    End If
 
lngLen = Len(strWhere) - 5
    If lngLen <= 0 Then
        MsgBox "Please try again.", vbInformation, "Invalid Search Criterion"
 
    Else
        strWhere = Left$(strWhere, lngLen)
        'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
        Debug.Print strWhere
[color=red]If DCount("YourFieldNameHere","YourTableNameHere", strWhere) > 0
        ' Apply the string as the form's Filter.
        Me.Filter = strWhere
        Me.FilterOn = True
Else
      Msgbox "No Records Match", vbInformation, "No Records"
End If[/color]
    End If
 
End Sub
 

tarcona

Registered User.
Local time
Today, 14:46
Joined
Jun 3, 2008
Messages
165
Ok. I have no clue what the field name should be because I have those four fields that I am searching in....do I put all field names or what?

I am searching in my 'Accounts' table.
 

tarcona

Registered User.
Local time
Today, 14:46
Joined
Jun 3, 2008
Messages
165
Actually I think I got it to work here. Thanks boblarson for your help.
 

Users who are viewing this thread

Top Bottom