The information in this article applies to:
- Microsoft Access versions 7.0, 97
SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article describes a technique you can use to turn off the "Break on
All Errors" option in Visual Basic for Applications code to prevent users
from interrupting your error handling routines.
This article assumes that you are familiar with Visual Basic for
Applications and with creating Microsoft Access applications using the
programming tools provided with Microsoft Access. For more information
about Visual Basic for Applications, please refer to your version of the
"Building Applications with Microsoft Access" manual.
MORE INFORMATION
Even when your code contains error handling routines, if you share the
database with a user who has the "Break on All Errors" option turned on in
Microsoft Access, a run-time error causes your code to halt and open the
module containing the error. If you do not want users to handle your run-
time errors, you can create procedures that suspend the "Break on All
Errors" option while your code executes.
Sample Procedures to Suspend and Resume the "Break on All Errors" Option
You can use the following sample procedures in your own database to
temporarily suspend the "Break on All Errors" option. Note that these
procedures work whether or not the "Break on All Errors" option is set; you
can include them in your code as added protection against exposing run-time
errors:
- Create a module and type the following line in the Declarations section:
Dim varOldBOAEOptions As Variant
- Type the following procedures:
'-----------------------------------------------------------------
' Save the current setting for the "Break on All Errors" option
' Turn off the "Break on All Errors" option.
'-----------------------------------------------------------------
Public Sub SuspendBreaks()
Select Case Application.SysCmd(acSysCmdAccessVer)
Case "7.0"
varOldBOAEOptions = GetOption("Break On All Errors")
SetOption "Break On All Errors", False
Case "8.0"
varOldBOAEOptions = GetOption("Error Trapping")
SetOption "Error Trapping", 2
End Select
End Sub
'-----------------------------------------------------------------
' Restore the "Break on All Errors" settings that were temporarily
' suspended by the SuspendBreaks procedure.
'-----------------------------------------------------------------
Public Sub ResumeBreaks()
Select Case Application.SysCmd(acSysCmdAccessVer)
Case "7.0"
If Not IsEmpty(varOldBOAEOptions) Then _
SetOption "Break On All Errors", varOldBOAEOptions
Case "8.0"
If Not IsEmpty(varOldBOAEOptions) Then _
SetOption "Error Trapping", varOldBOAEOptions
End Select
End Sub
- Save the module as basErrHandling.
- When you want to ensure that the "Break on All Errors" option does not
interrupt the execution of your code, call the SuspendBreaks procedure
at the beginning of your code, and call the ResumeBreaks procedure at
the end. For example:
Function MyCodeModule()
SuspendBreaks
On Error GoTo MyCodeModule_Err
' Add your code here.
MyCodeModule_Exit:
ResumeBreaks
Exit Function
MyCodeModule_Err:
' Add your error handling routine here.
Resume MyCodeModule_Exit
End Function
Example Showing Results of Different "Break on All Errors" Settings
The following example demonstrates what happens when your code containing
error handling routines runs with the Break on All Errors option turned on.
- Start Microsoft Access and create a new blank database called
MyError.mdb.
- Perform steps 1-3 in the previous section to create the procedures that
suspend and resume the "Break on All Errors" option.
- Create a new form not based on any table or query in Design view:
Form: frmTestErrors
----------------------------------------------------
Caption: Test Error Handling
Text box:
Name: txtUName
Text box:
Name: txtPwd
Command button:
Name: cmdOK
Caption: Without Turning Off Break On All Errors
OnClick: [Event Procedure]
Command button:
Name: cmdOKBreakOff
Caption: Turning Off Break On All Errors
OnClick: [Event Procedure]
Command button:
Name: cmdCancel
Caption: Cancel
OnClick: [Event Procedure]
- On the View menu, click Code, and then type the following procedures:
'---------------------------------------------------------------
' Test UserName and Password.
' Returns:
' True if UserName and Password are valid.
' False if UserName and Password are invalid.
' Displays corresponding error message.
'-------------------------------------------------------------
Public Function ChkPwd(uid As String, strPwd As String)
On Error GoTo badPwd
Dim ws As Workspace
Set ws = DBEngine.CreateWorkspace("TestPWD", uid, strPwd)
MsgBox "Your password is correct, " & uid
ChkPwd = True
exitChkPwd:
Exit Function
badPwd:
MsgBox "Not the right UserName or Password, " & uid & _
", if that is your real name!"
ChkPwd = False
Resume exitChkPwd
End Function
Private Sub cmdOK_Click() ' Without "Break on All Errors" turned off.
Call ChkPwd(Me![txtUName] & "", Me![txtPwd] & "")
End Sub
Private Sub cmdOKBreakOff_Click()
SuspendBreaks ' Turn off "Break on All Errors."
Call ChkPwd(Me![txtUName] & "", Me![txtPwd] & "")
ResumeBreaks ' Reset "Break on All Errors."
End Sub
Private Sub cmdCancel_Click()
DoCmd.Close
End Sub
- Save the frmTestErrors form and close it. On the Tools menu, click
Options.
- In the Options dialog box, click the Advanced tab (or the Module tab in
version 7.0), and then click "Break on All Errors." Click OK.
- Open the frmTestErrors form in Form view.
- Type "User1" (without the quotation marks) in the txtUName box, and
type "MyPassword" (without the quotation marks) in the txtPwd box.
Click "Without Turning Off Break On All Errors," and note that you
receive the following run-time error message, even though your code
handles errors:
Run-time error '3029':
Not a valid account name or password.
Click End in response to the error message.
- Click "Turning Off Break On All Errors." Note that your error
handling routine produces the following message:
Not the right UserName or Password, User1, if that is your real
name!
REFERENCES
For more information about error handling, search the Help Index for "error
handling," or refer to your Microsoft Access manual "Building Applications
with Microsoft Access 97," Chapter 8, "Handling Run-Time Errors," pages 235-
254.
For more information about Break On All Errors, search the Help Index for
"GetOption method" or "SetOption method."