Error in Access VBA on Combo Box

Maxine Nietz 1 Reputation point
2025-10-09T18:25:39.23+00:00

On a form I have a combo box, Combo61, with the following code for AfterUpdate:

Dim rs As Object

Set rs = Me.Recordset.Clone

rs.FindFirst "[SONo] = '" & Me![Combo11] & "'"

If rs.NoMatch Then

    MsgBox "That SONo does not exist. Try again.", vbOKOnly + vbInformation, "US Welding"

    Me!Combo11 = Null

    Me!Combo11.SetFocus

Else

    Me.Bookmark = rs.Bookmark

End If

I am not getting the NoMatch MsgBox. I am getting the following error message:

NoMatchError

I cannot set the LimitToList to NO. How can I get my MsgBox, not this error??

Thanks in advance.

Microsoft 365 and Office | Access | For home | Windows
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Ken Sheridan 3,291 Reputation points
    2025-10-10T12:06:58.0533333+00:00

    The fact that you are calling the FindFirst method rather than filtering the form suggests that the SONO column is a candidate key of the form's underlying table. If so the combo box's RowSource should return rows which are guaranteed to exist in the table, so the user will not be able to select an unknown value. To cater for the user typing in an unknown value the control's NotInList event procedure can give the user the option of inserting a new row into the table. The following example from one of my demo files is for navigating to a row in a form bound to a Contacts table. Firstly the control's RowSource is:

    SELECT
        ContactID,
        FirstName & " " & LastName,
        1 As SortColumn,
        LastName,
        FirstName
    FROM
        Contacts
    UNION
    SELECT
        0,
        "<New Contact>",
        0,
        "",
        ""
    FROM
        Contacts
    ORDER BY
        SortColumn,
        LastName,
        FirstName;
    

    The control's AfterUpdate event procedure is:

    Private Sub cboGotoContact_AfterUpdate()
        Const MESSAGETEXT = "No matching record"
        Dim ctrl As Control
        
        Set ctrl = Me.ActiveControl
        
        If Not IsNull(ctrl) Then
            If ctrl = 0 Then
                ' go to new record and move focus to FirstName control
                DoCmd.GoToRecord acForm, Me.Name, acNewRec
                Me.FirstName.SetFocus
            Else
                With Me.RecordsetClone
                    .FindFirst "ContactID = " & ctrl
                    If Not .NoMatch Then
                        ' go to record by synchronizing bookmarks
                        Me.Bookmark = .Bookmark
                    Else
                        MsgBox MESSAGETEXT, vbInformation, "Warning"
                    End If
                End With
            End If
        End If
        
    End Sub
    
    

    and its NotInList event procedure is:

    Private Sub cboGotoContact_NotInList(NewData As String, Response As Integer)
        Dim ctrl As Control
        Dim strMessage As String
        Dim strFirstName As String, strLastName As String
        
        Set ctrl = Me.ActiveControl
        strMessage = "Add " & NewData & " to list?"
        
        strFirstName = Left(NewData, InStr(NewData, " ") - 1)
        strLastName = Mid(NewData, InStr(NewData, " ") + 1)
        
        'if user confirms insert move form to new record
        ' and insert values into first and last name controls
        If MsgBox(strMessage, vbYesNo + vbQuestion) = vbYes Then
            Response = acDataErrContinue
            ctrl.Undo
            DoCmd.GoToRecord acForm, Me.Name, acNewRec
            Me.FirstName = strFirstName
            Me.LastName = strLastName
        Else
            Response = acDataErrContinue
            ctrl.Undo
        End If
        
    End Sub
    
    

    Where the column whose values are listed in the combo box is not a candidate key, the following example is again for a contacts form, but in this case to return rows where the LastName column's value is that selected in the combo box. The control's RowSource in this case is:

    SELECT
        LastName,    
    FROM
        Contacts
    UNION
    SELECT
        "All Names",    
    FROM
        Contacts
    ORDER BY
        SortColum,
        LastName;
    

    Its AfterUpdate event procedure filters the form to those rows matching the selected value. The code calls the DLookup function to determine if any such rows exist, and if this returns a value filters the form, or if a Null is returned informs the user of the failure to match the match the value entered:

    Private Sub cboLastname_AfterUpdate()
    
        Const MESSAGETEXT = "No matching records found."
        Dim ctrl As Control
        Dim strFilter As String
        
        Set ctrl = Me.ActiveControl
        
        strFilter = "LastName = """ & ctrl & """"
        
        If ctrl = "All names" Then
            ' turn off filter
            Me.FilterOn = False
        Else
            If Not IsNull(DLookup("ContactID", "Contacts", strFilter)) Then
                ' flter form to name selected in combo box
                Me.Filter = strFilter
                Me.FilterOn = True
            Else
                ' inform user if no matching records found and show all records
                MsgBox MESSAGETEXT, vbInformation, "Warning"
                Me.FilterOn = False
                Me.Requery
            End If
        End If
           
    End Sub
    
    

    In this example no opportunity is given for the user to insert a new row, but an alternative to the above approach would, subject to user confirmation, be to use the control's NotInList event procedure to move the form to an empty new record, and assign the value of the NewData argument to a LastName control.

    You'll find the above examples in FindRecord.zip in my Dropbox public databases folder at: 

    https://www.dropbox.com/scl/fo/0scigd3r48hx5xrev2jrf/AB0-GMdTgMAO5O1cGdr3QW0?rlkey=ib6bs6g9jqcrywwzivur3265t&dl=0

    2 people found this answer helpful.
    0 comments No comments

  2. Marcin Policht 63,645 Reputation points MVP Volunteer Moderator
    2025-10-09T18:28:52.0266667+00:00

    When you enter a value that doesn't exist, Access triggers the combo box's NotInList event before the AfterUpdate event runs — and since your value isn't valid, AfterUpdate never actually finishes properly. That's why you're getting a system “NoMatch” error instead of your custom MsgBox.

    Since you can't set LimitToList = No, you need to handle this in the combo box's NotInList event — not AfterUpdate.

    Private Sub Combo11_NotInList(NewData As String, Response As Integer)
        MsgBox "That SONo does not exist. Try again.", vbOKOnly + vbInformation, "US Welding"
        
        Response = acDataErrContinue  ' Prevent Access default error message
        Me!Combo11.Undo               ' Clears the invalid entry
    End Sub
    

    This will catch any value that's not in the list, show your custom message, and stop Access from displaying its default “NoMatch” error.

    Your original AfterUpdate code can stay as-is — it will only run for valid values in the combo box:

    Private Sub Combo11_AfterUpdate()
        Dim rs As Object
        Set rs = Me.Recordset.Clone
        rs.FindFirst "[SONo] = '" & Me![Combo11] & "'"
        If Not rs.NoMatch Then
            Me.Bookmark = rs.Bookmark
        End If
    End Sub
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    1 person found this answer helpful.
    0 comments No comments

  3. Maxine Nietz 1 Reputation point
    2025-10-14T18:25:21.5266667+00:00

    Thank you both. The NotInList event did the trick!


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.