Handling VBA errors

VBA errors display a VBA dialog with an error number and message text. VBA halts the execution of VBA code at the point the error occurred. You can handle Microsoft Dynamics GP VBA errors in the same manner as other errors that appear in VBA, using VBA’s On Error statement to respond to the error condition. The On Error statement has three clauses that allow you to handle errors:




On Error Go To line

Enables an error-handling routine that starts at line. The specified line must be in the same procedure as the On Error statement.

On Error Resume Next

Passes control to the statement that immediately follows the statement that generated the error.

On Error Go To 0

Disables any enabled error handler in the current procedure.

In the following example, the Resume Next clause of the On Error statement allows the procedure to skip the statement that generates an error. In this case, setting the Shipping Method field will generate an error (VBA error 1007) since “NEW” isn’t an existing shipping method. However, the procedure continues to run, and the final statement sets the Trade Discount field:

Private Sub CustomerID_AfterUserChanged()
	'Bypass any line that encounters an error
	On Error Resume Next
	'Set a window field that generates an error
	ShippingMethod = "NEW"
	'Set a window field that doesn't generate an error
	TradeDiscount = "10.00"
End Sub

Documentation Feedback