Search code examples
vbams-accessms-access-2016

The expression On Click you entered as the event property setting produced the following error: Sorry, an unexpected error occurred


The error message continues with:

"*The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure]." "*There may have been an error evaluating the function, event, or macro."

This database was written by an employee who retired years ago and unfortunately I am now stuck with it. It was working last week and of course when it stopped working today. I am not sure where to even start looking or how to debug in VBA. When a user enter data in the fields and click on Print button, I believe the data gets populated in the data source and then it will print a report/receipt. The print button call the cmdPrintReceipt_Click() procedure. Can someone give me some pointers or how to debug in VBA? Much appreciated and happy holidays!

Pasting the code here:

Option Compare Database
Option Explicit

Dim OkPass As Integer, message As String, Response As Integer
Dim nAmount As Integer
Dim Conn As New ADODB.Connection
Dim RS2 As ADODB.Recordset
Dim Total As Long 'Hold receiptNumber for ReceiptLine

Private Sub cmdNoAdd_Click()  'close window without adding a new reacord
'Use the msgbox function to see if user wants to exit this form w/o adding new receipt

Dim Msg, Style, Title, respons
Msg = "Close this window without saving this receipt?"
Style = vbYesNo + vbQuestion + vbDefaultButton2 'Define buttons of message box
Title = "Exit without adding a receipt?" 'Title of the message box
respons = MsgBox(Msg, Style, Title)

    If respons = vbYes Then 'User chose Yes DO NOT ADD the Record
       DoCmd.Close
    Else 'User chose No close the message box and leave frmReceipt Open
    End If
End Sub
Private Sub Form_Load()
    'Clear Total
    Total = 0
End Sub
'****************************************************************************************
'   Main procedure for the form, check first all required fields filled in
'   then Add new records and print receipts.
'****************************************************************************************
Private Sub cmdPrintReceipt_Click()
'Check if all filled in, then
    'Just call the AddReceipt Function
    DoNotClose          'function
      If OkPass <> 9 Then
        Response = MsgBox(message, vbOKOnly, "Information Missing")
        'return focus to the frmReceipt
        txtFirstName.SetFocus
        Exit Sub
      Else
       ' Add the new Receipt record
            Me.lblNoAdd.Visible = False
            Me.cmdNoAdd.Visible = False   'you can't escape after printing receipt.
            
            AddAReceipt ' call AddReceipt function
                   
      
      MsgBox "Please confirm the information you entered is correct", vbOKOnly
      ShowPrint3Button
            'cmdPrint3Button print 3 copies of receipt
      End If
      Exit Sub
 End Sub

Solution

  • If it worked for years without errors before, I doubt debugging will help.
    The db might be corrupted. Try the following steps:

    • see if VBA compiles
    • compact and repair the db
    • if not successfull, create a new blank db, and imort all objects from the old one.

    Also, when you get the error message, you can press Ctrl+Break and arrive on the exact line that causes the error. Good luck ;-)