Msg if no record; (1 Viewer)

theinviter

Registered User.
Local time
Yesterday, 23:42
Joined
Aug 14, 2014
Messages
241
HI
i Have a form which filter after entering code number, as code below;
but i wannt to show msg box if no record found and cancel filter, how to modify the code below:


Private Sub filterThisForm1()
Dim strFilter As String
On Error GoTo err_handler
If Len(Trim$(Me!Combo79 & "")) Then
strFilter = "code = '" & Me!Combo79.Column(2) & "' Or [Other Code] = '" & Me!Combo79.Column(3) & "'"

Else
strFilter = "code like '" & Me!FindCode & "' Or [Other Code] like '" & Me!FindCode & "'"

End If
With Me



.Filter = strFilter
.FilterOn = True
![Table1 Subform].Form.FilterOn = False
Me.Table1_Subform.SetFocus
DoCmd.GoToControl ("[Qnty_IN]")
DoCmd.GoToRecord , , acNewRec
End With
exit_here:
Exit Sub
err_handler:
MsgBox Err.Number & ": " & Err.Description
Resume exit_here
End Sub
 

bob fitz

AWF VIP
Local time
Today, 07:42
Joined
May 23, 2011
Messages
4,727
Indenting your code makes in much easier to read.
Code:
Private Sub filterThisForm1()
    Dim strFilter As String
    On Error GoTo err_handler
    If Len(Trim$(Me!Combo79 & "")) Then
        strFilter = "code = '" & Me!Combo79.Column(2) & "' Or [Other Code] = '" & Me!Combo79.Column(3) & "'"

    Else
        strFilter = "code like '" & Me!FindCode & "' Or [Other Code] like '" & Me!FindCode & "'"

    End If
    With Me



        .Filter = strFilter
        .FilterOn = True
        ![Table1 Subform].Form.FilterOn = False
        Me.Table1_Subform.SetFocus
        DoCmd.GoToControl ("[Qnty_IN]")
        DoCmd.GoToRecord , , acNewRec
    End With
exit_here:
    Exit Sub
err_handler:
    MsgBox Err.Number & ": " & Err.Description
    Resume exit_here
End Sub
 

bob fitz

AWF VIP
Local time
Today, 07:42
Joined
May 23, 2011
Messages
4,727
You try something like:

Code:
If Me.Recordset.RecordCount = 0 Then
   Me.FilterOn = False
End If

immediately after you turn the filter on.
 

ebs17

Well-known member
Local time
Today, 08:42
Joined
Feb 7, 2020
Messages
1,950
but i wannt to show msg box if no record found
My users feel sufficiently informed when they see the blank form. But these users would go crazy if they had to constantly click away superfluous messages.
 

theinviter

Registered User.
Local time
Yesterday, 23:42
Joined
Aug 14, 2014
Messages
241
You try something like:

Code:
If Me.Recordset.RecordCount = 0 Then
   Me.FilterOn = False
End If

immediately after you turn the filter on.
but if i want the show msgbox " no record found" and set focus on "Findcode".
 

bob fitz

AWF VIP
Local time
Today, 07:42
Joined
May 23, 2011
Messages
4,727
but if i want the show msgbox " no record found" and set focus on "Findcode".
Put your Msgbox code on the line after Me.FilterOn = False and on the next line set the focus with something like:
Me.[NameOfControlToBeSet].SetFocus
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 02:42
Joined
Feb 19, 2002
Messages
43,368
I'm with @ebs17 . You don't want to train your users to ignore your messages. Save them for something important that you want them to pay attention to.
 

Users who are viewing this thread

Top Bottom