Search code examples
vbams-accessruntime-error

Trying to avoid runtime error 2115 on BeforeUpdate


I have the below code on a data entry form that also includes an unbound Search box. The form errors out to 2115 and crashes when the user searches something that has no matches (i.e, when recordset.Recordcount = 0).

    Private Sub Form_BeforeUpdate(Cancel As Integer)
         ' This procedure checks to see if the data on the form has
         ' changed. If the data has changed, the procedure prompts the
         ' user to continue with the save operation or to cancel it. Then
         ' the action that triggered the BeforeUpdate event is completed.
    
         Dim ctl As Control
    
         On Error GoTo Err_BeforeUpdate
    
         ' The Dirty property is True if the record has been changed.
         If Me.Dirty Then
           ' Prompt to confirm the save operation.
           If MsgBox("Do you want to save?", vbYesNo + vbQuestion, _
               "Save Record") = vbNo Then
            Me.Undo
           End If
         End If
    
        Exit_BeforeUpdate:
         Exit Sub
    
        Err_BeforeUpdate:
         If DataErr = 2115 Then
          Response = acDataErrContinue
          Me.cboSearch.Text = ""
          Cancel = True
          Me.cboSearch.Undo
          ExitSub
         Else
        MsgBox Err.Number & " " & Err.Description
        Resume Exit_BeforeUpdate
         End If
     
    End Sub

Below is the code for the unbound Search box:

    Private Sub cboSearch_AfterUpdate()
            Dim strFilter As String
            Dim sSearch As String
            On Error Resume Next
    
            If Me.cboSearch.Text <> "" Then
                sSearch = "'*" & Replace(Me.cboSearch.Text, "'", "''") & "*'"
                strFilter = "SalesOrder Like " & sSearch & " OR PO Like " & sSearch &_
                " OR PONotes Like " &      sSearch & " OR ProjectPlan Like " & sSearch
            Me.Filter = strFilter
            Me.FilterOn = True
            DoCmd.SetWarnings False
        Else
            Me.Filter = ""
            Me.FilterOn = False
        End If
            DoCmd.SetWarnings True
        If Me.Recordset.RecordCount = 0 Then 'new line of code
            MsgBox "There are no records for this search."
            Me.Filter = "" 'new line of code
            Me.FilterOn = False 'new line of code
            Me.cboSearch.SetFocus 'new line of code
            Me.cboSearch.Text = "" 'new line of code
        Exit Sub 'new line of code
        End If 'new line of code
    
        With Me.cboSearch
            .SetFocus
            .SelStart = Len(Me.cboSearch.Text)
        End With
    End Sub

Tried a couple different suggestions from some posts I found but nothing is working. Trying to understand the logic of the BeforeUpdate event in relation to an unbound control.


Solution

  • You have a typo in your error handling routine. Your code shows ExitSub as one word instead of Exit Sub as two words.

    You would also do well to code your error handler as follows:

        Err_BeforeUpdate:
         If DataErr = 2115 Then
          Response = acDataErrContinue
          Me.cboSearch.Text = ""
          Cancel = True
          Me.cboSearch.Undo
         Else
          MsgBox Err.Number & " " & Err.Description
         End If
        Resume Exit_BeforeUpdate