Question

I am following instructions on creating a new VBA procedure in Access. One of the specifications is to use standard ExitProc-Error handling. I was a little confused on what that ment but I looked at examples online and previously completed examples supplied as a template, I came up with this:

On Error GoTo HandleError
'code that may cause errors

ExitProc:
Exit Function
HandleError:
If FindFolders = False Then
    MsgBox ("Required folders for " & InstrumentName & " not found")
ElseIf FindFolderPaths = False Then
    MsgBox ("Required folder paths for " & InstrumentName & " not found")
End If

MsgBox Err.Number & " " & Err.Description & " in FindFolderPaths"
Resume ExitProc
End Function

findfolders is a boolean variable I marked as false in the procedure to indicate an error and FindFolderPaths is the name of the procedure i am using as a indication of whether the paths where found. When I run this procedure the following msg boxes appear. I created an error in the folder name to show how the error would be handled :

-Required folders for ICPMS not found

-0 in FindFolderPaths

-Required folders for ICPMS not found

-20 Resume without Error in FindFolderPaths

When I stepped through the procedure step by step it looks like its looping through HandleError twice before going to ExitProc and I can't figure out why. Also the error message changes during the second loop in HandleError which is strange.

Was it helpful?

Solution

Your problem seems to be that you're "mis-using" the error handler by calling it directly in your code using Goto HandleError instead of having it called automatically when an error occurs.

This would explain exactly what you report was output when your code ran:

-Required folders for ICPMS not found  'shows regardless of what triggers the handler
-0 in FindFolderPaths                  'err number of zero means there's no error

Resume Exit Proc then triggers an actual error, calling the code a second time

-Required folders for ICPMS not found        'not relevant here...
-20 Resume without Error in FindFolderPaths  'an actual error number and message

Typically if there's some condition you can test for (such as a missing folder) then you don't need to handle that by calling an error handler - you can show a message from your "regular" code and then just Exit function. Alternatively, raise a custom error using Err.Raise, giving it a specific error number and description which you can test for and handle appropriately in your Error Handler.

http://msdn.microsoft.com/en-us/library/office/aa164019(v=office.10).aspx#odc_tentipsvba_topic3

OTHER TIPS

Remove Resume ExitProc. The code will leave the function when it hits End Function. Also, you need to remove the parentheses when you are calling MsgBox. Only use parentheses when you are setting something equal to MsgBox.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top