Answered by:
Run-time error '287': Application-defined or object-defined error

-
Hello all,
I have an access application that allows for the user to Save the current form record and also generate an email. After clicking the command button they are asked to allow or deny the event.
If the user clicks Deny they receive the error message "Run-time error '287': Application-defined or object-defined error"
The user can then either click End or Debug. If they click End there isn't an issue. If they click Debug the VBA debugger is opened and the code is displayed. This could result in the users making changes to the application code and cause it to break further.
How can I fix the VBA code so it works without this error appearing?
The code is below and attached is the error message and the point the debugger picks up the error. Any assistance is appreciated.
Private Sub Command75_Click() Dim stId As Integer Dim db As Database Dim rs As DAO.Recordset Dim appOutLook As Outlook.Application Dim MailOutLook As Outlook.MailItem Dim stText As String Dim stDefect As String Dim stGkOrder As Long '==Save the current record If Me.Dirty Then Me.Dirty = False Set appOutLook = CreateObject("Outlook.Application") Set MailOutLook = appOutLook.CreateItem(olMailItem) '==Set db = CurrentDb() '==Set rs = db.OpenRecordset("dbo.NonConfData") stId = Me.Id stDefect = Me.Combo45.Column(1) stGkOrder = Me.GKOrderNum stText = "NonConformance IR #" & " " & "Defect Category:" & stDefect & " " & "Against Order#" & stGkOrder With MailOutLook .BodyFormat = olFormatHTML .To = "capateam@company.com" ''.cc = "" ''.bcc = "" .Subject = "NonConformance Generated IR #" & stId .HTMLBody = stText .Send End With '==rs.Close
Keith
- Edited by Keith732 Friday, December 27, 2013 9:13 PM
Question
Answers
-
You need error handlers to properly manage errors. Users should never see the End/Debug dialog.
Here's the basics:
Function YourFunction() On Error Goto Error_Proc ' all your usual code goes here' Exit_Proc: 'this is your exit portion' 'all exits will be directed here' 'use this to clean up any open objects' Exit Function Error_Proc: 'this is your error handler' 'with the On Error statement at the top,' 'any errors jump to the specified label' '' 'check errors:' Select Case Err.Number Case 287: Resume Exit_Proc 'ignore the error' Case Else: MsgBox "Error encountered: " & Err.Description Resume Exit_Proc 'display a message then exit' End Function
This should be standard for pretty much every procedure. Tools like the invaluable mz-tools (www.mztools.com) let you customize your handler and add it to a procedure with the click of a button.
Do a google search for VBA error handling for more info, but the above is fully sufficient. There's more complex global handlers that can log errors, notify developers, etc, but this is all you really need.
Cheers
Jack D. Leach (Access MVP)
UtterAccess Wiki: (Articles, Functions, Classes, VB7 API Declarations and more)
Dymeng: blog | Services
- Edited by Jack D Leach Saturday, December 28, 2013 3:04 PM changed error number from 291 to 287
- Marked as answer by Keith732 Friday, January 03, 2014 2:11 PM
-
Your Access instance probably has the option "Breal on All Errors" selected.
In the code window, use the Menu command Tools / Options... / General tab and chgane the error-trapping option to "Break on Unhandled Errors" and then try your code again.
Note: the code you posted WILL display the error message when an error occurs UNLIKE Jack's suggested code (which trap all errors and display the error message for all errors EXCEPT error 287).
Van Dinh
- Marked as answer by George HuaModerator Friday, January 03, 2014 9:48 AM
All replies
-
You need error handlers to properly manage errors. Users should never see the End/Debug dialog.
Here's the basics:
Function YourFunction() On Error Goto Error_Proc ' all your usual code goes here' Exit_Proc: 'this is your exit portion' 'all exits will be directed here' 'use this to clean up any open objects' Exit Function Error_Proc: 'this is your error handler' 'with the On Error statement at the top,' 'any errors jump to the specified label' '' 'check errors:' Select Case Err.Number Case 287: Resume Exit_Proc 'ignore the error' Case Else: MsgBox "Error encountered: " & Err.Description Resume Exit_Proc 'display a message then exit' End Function
This should be standard for pretty much every procedure. Tools like the invaluable mz-tools (www.mztools.com) let you customize your handler and add it to a procedure with the click of a button.
Do a google search for VBA error handling for more info, but the above is fully sufficient. There's more complex global handlers that can log errors, notify developers, etc, but this is all you really need.
Cheers
Jack D. Leach (Access MVP)
UtterAccess Wiki: (Articles, Functions, Classes, VB7 API Declarations and more)
Dymeng: blog | Services
- Edited by Jack D Leach Saturday, December 28, 2013 3:04 PM changed error number from 291 to 287
- Marked as answer by Keith732 Friday, January 03, 2014 2:11 PM
-
-
Jack,
Thanks for your reply. I implemented the error handler but it doesn't seem to be moving into the code block. I even simplified the error handler so it wasn't specific to the 287 runtime error message but the message shows up at the same line in the debugger. Below is the code with the error handler. Could you take a look and let me know what I might be missing?
Private Sub Command75_Click() On Error GoTo Error_Proc Dim stId As Integer Dim db As Database Dim rs As DAO.Recordset Dim appOutLook As Outlook.Application Dim MailOutLook As Outlook.MailItem Dim stText As String Dim stDefect As String Dim stGkOrder As Long '==Save the current record If Me.Dirty Then Me.Dirty = False Set appOutLook = CreateObject("Outlook.Application") Set MailOutLook = appOutLook.CreateItem(olMailItem) '==Set db = CurrentDb() '==Set rs = db.OpenRecordset("dbo.NonConfData") stId = Me.Id stDefect = Me.Combo45.Column(1) stGkOrder = Me.GKOrderNum stText = "NonConformance IR #" & " " & "Defect Category:" & stDefect & " " & "Against Order#" & stGkOrder With MailOutLook .BodyFormat = olFormatHTML .To = "capateam@company.com" ''.cc = "" ''.bcc = "" .Subject = "NonConformance Generated IR #" & stId .HTMLBody = stText .Send End With Exit_Proc: Exit Sub Error_Proc: MsgBox "Error Encountered: " & Err.Description Resume Exit_Proc '==rs.Close End Sub
Keith
-
Your Access instance probably has the option "Breal on All Errors" selected.
In the code window, use the Menu command Tools / Options... / General tab and chgane the error-trapping option to "Break on Unhandled Errors" and then try your code again.
Note: the code you posted WILL display the error message when an error occurs UNLIKE Jack's suggested code (which trap all errors and display the error message for all errors EXCEPT error 287).
Van Dinh
- Marked as answer by George HuaModerator Friday, January 03, 2014 9:48 AM
-
If you are releasing this to customers or anyone who is not supposed to see the code, password protect the code so it is not even viewable to them.
Depending on what version of Access you are using you can do the following to protect your code:
1. In VBA window, look for the project box (usually top-left)
2. Right click on project name (usually first item in the list)
3. Click project properties
4. Click on tab#2: "Protection"
EDIT:
p.s: If an error occurs when the code is protected, the debug option is not available but they can still click on end
- Edited by Cadefia Monday, December 30, 2013 11:38 PM
-
-
-
This is an old post but I have just had a similar instance as you can see on this link. http://stackoverflow.com/q/36328068/5043393. As others suggest error handling is required but doesnt solve the problem.... I thought I would share my solution to see if the fix worked for other users. If you don't like clicking on links then the solution is to force a delay by inserting a loop right before the send operation. Thanks.
- Edited by Liam_H Saturday, April 02, 2016 2:34 PM
-
Check outlook's send and receive. Its an error returned by the POP or SMTP of outlook.
Similarly at VBA Editor, Tools->References, choose the available version of outlook object library. For instance; Initially, the macro would be written and executed in a machine having outlook 13 but when it is copied and executed in another machine with outlook 10. The macro will be still looking for outlook object library 15 instead of 14 which is unavailable in the latter machine. The references will not switch deepening on the availability.
Kiran