Solved

Access 2010 vba error handling

Posted on 2014-02-04
3
972 Views
Last Modified: 2014-02-04
Hi,

Just a quick question regarding error handling in vba.

Should you implement error handling for each and every vba procedure? For example:

If opening a form to a specific record:
=======================================================
If Not Nz(Me.txtSubsIFAID, "") = "" Then
        Dim strLinkCriteria As String
        strLinkCriteria = "[SubsIFA_ID]=" & Me.txtSubsIFAID & " And [Prop_ID]=" & Me.txtPropID & ""
        DoCmd.OpenForm "frmAddSubsIFA", acNormal, , strLinkCriteria
    End If
=======================================================

or:
DoCmd.OpenForm "frmPolicyPII_AddPolicyNote", acNormal, , acFormAdd

or:
If Me.Dirty Then
        If MsgBox("You have made changes to the 'Client Data' and not saved them." & vbCrLf & vbCrLf & "Are you sure you wish to Exit without saving your changes?", vbYesNo + vbExclamation, "Client Details: Close Screen Alert") = vbYes Then
            Me.Undo
            DoCmd.Close acForm, "frmClientDetails"
        Else
            Exit Sub ' stay on form do nothing
        End If
    Else
        Forms![frmNavMenuForm].[NavMenuSubform].SourceObject = ""
    End If

Many thanks
0
Comment
Question by:andrewpiconnect
  • 2
3 Comments
 
LVL 4

Expert Comment

by:Jack Leach
ID: 39832037
If a person has to ask, then yes, they should.  It's safer,and it's one of those "learn all the rules before you learn where to break them" type of things.

What is sure is that every code that runs absolutely should have an error handler somewhere in the call stack, thus preventing any unhandled errors from ever being raised.

If an error is raised and is not handled in the current procedure, it will look to the procedure that called it, and use the error handler there.  If there's none, it will go the procedure before that one, so on and so forth, until it finds a handler or runs to the bottom of the call stack.  Under no circumstances should it hit the bottom of the call stack without being handled.

Error propagation (as the above is called) can be a helpful tool when understood and used correctly.  As a general rule of thumb though, you're probably better off including basic error handling on every procedure unless you have some specific reason NOT to.

Tools like mz-tools can help greatly with that, in case you're not already aware (google the word, you'll find it).

Cheers,
0
 
LVL 4

Accepted Solution

by:
Jack Leach earned 500 total points
ID: 39832046
As a side note, you can probably get away without it for a small handful of procedures where you can be sure that no error would ever be raised.  Many do, but this begs the question: if I knew an error might be raised, I would have handled it in the code logic already, right?

Anything to do with the form's Dirty property or any DoCmd method, I don't really trust... in the examples you gave, I would definitely use an error handler, as they tend to be prone to non-obvious errors.

So again, once a person can recognize code that is error prone to code that isn't, they can make more informed decisions on when to use error handling and when it's "safe enough" to omit it.  It's probably a better practice to use a handler everywhere, otherwise.
0
 
LVL 75
ID: 39833209
Hey Jack ... welcome to Experts Exchange ...

Joe
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vba sql wild card passing in code 3 24
Normalization of a table 19 74
Object Variable or With Block Variable not set - Microsoft Access vba 3 34
Create macro from runcode 30 26
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question