Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you give me an example of try/catch in Access VBA? 1

Status
Not open for further replies.

OhioSteve

MIS
Mar 12, 2002
1,352
US
I have this...

try
[something]
catch e as exception
finally
end try


I do not want to use e, I just want to keep the function from dying if it cannot perform [something].
 
Catch ... Try" is a VB.Net construct and Access VB is essentially VB6 coding. You will need to use

On Error GoTo SomeLabel
On Error Resume
On Error Resume Next

in Access as in
Code:
On Error Resume Next
X = 10 / 0
If Err.Number <> 0 Then
   X = 0
   Err.Clear
End If

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Good point. I cannot find the "try..." syntax because it does not exist in this language.
 
Here's some typical error handling in VBA:
Code:
Public Sub MySub()
   On Error Goto ErrHandler

   [Code for the subroutine]

   Exit Sub

ErrHandler:
   'Check for any errors you might be expecting
   If Err.Number = 999 Then
      [Code that resolves this error - retry the line that errord]
      Resume
   Else
      'Give the user some info on this error
      MsgBox "Unfortunately the following error occurred " & _
      "in the MySub routine: Error # " & Err.Number & _
      "  " & Err.Description
   End If
End Sub

Note the Exit Sub line is important to put above the ErrHandler: label, otherwise the code falls through to the error handling code, whether or not there was an error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top