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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling - What error handling? 1

Status
Not open for further replies.

mobile2

Programmer
Dec 19, 2002
38
0
0
GB
I am a learner in VBA Access 97 and found a particular piece of code very useful thread705-441796. The code is working fine but a colleague has advised that I should include error handing in this code and all other VBA code I use. I am not too familiar with the error handling process and I'm not too sure how to get started. I have checked the FAQs and Help on Access 97 but getting no where fast. Apologies, if I am asking for help on too broad a subject. I would be grateful of any links you know of that gives useful advice on this subject.

Looking forward to a response. Thanks
 
Hi,

At the very least, error trapping keeps your application up and running (especially important if it has been compiled as an mde), without presenting your user with too many unfriendly system dialog boxes.

A very simple error trap would look like this:

Code:
public sub MyProc()

	dim strMsg as string

	On Error goto ErrHandler:

'	Your code here
	...
	...
	...


'	Code has executed without error, so stop processing
	Exit Sub

ErrHandler:
	strMsg = "The sytem has returned an error. " & _
		 "The error message was: " & _
    		 Err.Description, vbOKOnly + vbExclamation
	MsgBox strMsg
	Exit Sub
End Sub


This simple example allows the system to tell the user what has gone wrong without crashing out with all sorts of unpleasant error messages.

If you are anticipating specific error types, you can write slightly more sophisticated code. For instance, the following snippet responds to connection timeouts (only) by asking the user if they would like to retry the connection...


Code:
public sub AnExample()

	On Error goto ErrHandler:

'	Code here that sets up an ADO Command for execution
	...
	...
	...

'	Following lines of code attempt to execute the ADO command
errTimeoutRetry:
    	On Error GoTo errTimeout	
    	adoCMD.Execute
    	On Error GoTo ErrHandler:

'	Rest of the procedure here
	...
	...
	...

	Exit Sub

ErrHandler:
	strMsg = "The sytem has returned an error. " & _
		 "The error message was: " & _
    		 Err.Description, vbOKOnly + vbExclamation
	MsgBox strMsg
	Exit Sub

errTimeout:
    If Err.Description = "Timeout expired" Then
	strMsg = "A timeout error occured. Would you like to try again?")
	If msgbox(strMsg,vbYesNo) = vbYes Then
		goto errTimeoutRetry
	Else	
		Exit Sub
	End if
    Else
        GoTo ErrHandler:
    End If

End Sub


 
Thanks for the examples. Now I have something to get me started. Really appreciate it!
 
Hi nealvenables
Are you there?
I have a question on your comment.
Best regards
sanan
 
Hi Sanan--your best bet would be to begin a new post.
Regards-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top