Help with filter command when no records found (1 Viewer)

gojets1721

Registered User.
Local time
Today, 13:00
Joined
Jun 11, 2019
Messages
430
I have the below command that allows the user to search a field in a form and filter to whatever the user inputs. It works great but I just realized that if no matching records are found, the form just goes completely blank. This requires the user to exit the form and get back in.

Ideally, if no records are found, it will stay on the current record and just populate a msg box stating nothing found. Any suggestions on how to code that?

Code:
Private Sub btnSearchCategory_Click()
On Error GoTo btnSearchCategory_Click_Err

    Dim S As String
    
    S = InputBox("Enter Category", "Category Search")
    If S = "" Then Exit Sub
    
    Me.Filter = "Category LIKE ""*" & S & "*"""
    Me.FilterOn = True

btnSearchCategory_Click_Exit:
    Exit Sub

btnSearchCategory_Click_Err:

    MsgBox "Error #" & Err.Number & " - " & Err.Description, , "Error"
    
    Resume btnSearchCategory_Click_Exit
 
End Sub
 

gojets1721

Registered User.
Local time
Today, 13:00
Joined
Jun 11, 2019
Messages
430
Could you provide an example using my code? I'm not really sure what you mean. I've never used DCount that way
 

plog

Banishment Pending
Local time
Today, 15:00
Joined
May 11, 2011
Messages
11,646
That's not an example, that's giving you code.

Post the name of the datasource the form is based on and I am sure someone with something to prove will come along and give you the specific code you need. Until then, give it a shot and post back what you try and how it is not working.
 

MarkK

bit cruncher
Local time
Today, 13:00
Joined
Mar 17, 2004
Messages
8,181
It might work to just check Me.Recordset.RecordCount after you apply the filter, and if zero, then remove the filter.
Code:
Me.FilterOn = Me.Recordset.RecordCount
 

gojets1721

Registered User.
Local time
Today, 13:00
Joined
Jun 11, 2019
Messages
430
That's not an example, that's giving you code.

Post the name of the datasource the form is based on and I am sure someone with something to prove will come along and give you the specific code you need. Until then, give it a shot and post back what you try and how it is not working.
So I tried the below and the search works, but it still behaves the same when I type gibberish into the input box. It blanks the form and doesn't display the msgbox

Code:
Private Sub btnSearchCategory_Click()
On Error GoTo btnSearchCategory_Click_Err

    Dim S As String
    
    S = InputBox("Enter the Category", "Category Search")
    If S = "" Then Exit Sub
    
    If DCount("Category", "tblComplaints") > 0 Then
        Me.Filter = "Category LIKE ""*" & S & "*"""
        Me.FilterOn = True
    Else
       MsgBox "There are no records to view."
    End If

btnSearchCategory_Click_Exit:
    Exit Sub

btnSearchCategory_Click_Err:

    MsgBox "Error #" & Err.Number & " - " & Err.Description, , "Error"
    
    Resume btnSearchCategory_Click_Exit
 
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:00
Joined
Oct 29, 2018
Messages
21,476
Hi. Could you give this one a try? Thanks.
Code:
Private Sub btnSearchCategory_Click()
On Error GoTo btnSearchCategory_Click_Err

    Dim S As String
    Dim var As Variant

    S = InputBox("Enter Category", "Category Search")
    If S = "" Then Exit Sub

    var = Me.Bookmark
   
    Me.Filter = "Category LIKE ""*" & S & "*"""
    Me.FilterOn = True

    If Me.Recordset.RecordCount = 0 Then
        Me.FilterOn = False
        Me.Bookmark = var
        MsgBox "No matching record for that category.", vbInformation, "Info"
    End If

btnSearchCategory_Click_Exit:
    Exit Sub

btnSearchCategory_Click_Err:

    MsgBox "Error #" & Err.Number & " - " & Err.Description, , "Error"
    
    Resume btnSearchCategory_Click_Exit
 
End Sub
 

plog

Banishment Pending
Local time
Today, 15:00
Joined
May 11, 2011
Messages
11,646
Right now your DCount is just looking at total records in tblComplaints. You want to know if the filter will have any results. So you need to add your criteria to the DCount.
 

plog

Banishment Pending
Local time
Today, 15:00
Joined
May 11, 2011
Messages
11,646
There we go, you found your guy with something to prove.
 

gojets1721

Registered User.
Local time
Today, 13:00
Joined
Jun 11, 2019
Messages
430
Hi. Could you give this one a try? Thanks.
Code:
Private Sub btnSearchCategory_Click()
On Error GoTo btnSearchCategory_Click_Err

    Dim S As String
    Dim var As Variant

    S = InputBox("Enter Category", "Category Search")
    If S = "" Then Exit Sub

    var = Me.Bookmark
  
    Me.Filter = "Category LIKE ""*" & S & "*"""
    Me.FilterOn = True

    If Me.Recordset.RecordCount = 0 Then
        Me.FilterOn = False
        Me.Bookmark = var
        MsgBox "No matching record for that category.", vbInformation, "Info"
    End If

btnSearchCategory_Click_Exit:
    Exit Sub

btnSearchCategory_Click_Err:

    MsgBox "Error #" & Err.Number & " - " & Err.Description, , "Error"
   
    Resume btnSearchCategory_Click_Exit

End Sub
When I use that, I'm getting a 3159 error on the 'Me.Bookmark = var' line
 

MarkK

bit cruncher
Local time
Today, 13:00
Joined
Mar 17, 2004
Messages
8,181
I think applying a filter opens a new recordset, so a bookmark from a previous recordset is no longer valid.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:00
Joined
Oct 29, 2018
Messages
21,476
I think applying a filter opens a new recordset, so a bookmark from a previous recordset is no longer valid.
Thanks. You're probably right.
 

Users who are viewing this thread

Top Bottom