Error handling code placement

G

Guest

I need to insert a very simple error handling procedure in my code to open a
form. I'm not getting where to place it in the original code sequence. Both
are very simple but they aren't working together so far. I have a form with a
text box for entering account number, and an OK button which opens a form
that's based on a query. It works fine until I try to put the error handling
in there. It either ignores it completely or gives me an error message
depending on where I put the error code.

Original:
Private Sub cmdOK_Click()
DoCmd.OpenForm "Account Detail Form", acViewNormal, acEdit
End Sub

Error handling code:
Public Function Verify_Input() As Boolean
On Error GoTo ErrorHandler

Exit Function

ErrorHandler:
MsgBox ("Account not found.")
Resume

End Function

Where does this error code go in relation to the original open form code?

Dino
 
G

Graham Mandeno

Hi Dino

Your placement of the error handler is fine. It's normal to place it after
the normal exit point of the function and before the end of the function.
Usually you would also have another label after the main function code and
before Exit Function. This allows you to "clean up" (close recordsets, etc)
irrespective of whether or not an error has occurred.

Also important is the Resume statement. It comes in three flavours:

Resume
retries the statement that caused the error (presumably your error
handler has done something to rectify the condition that causes the error,
otherwise you will have an infinite loop!)

Resume Next
ignores the statement that causes the error and resumes at the next
statement

Resume SomeLabel
resumes execution at a given label - most often used to "bail out" of
the function but execute the clean-up code before exiting.

So, your standard template for the body of a procedure or function might
look like this:

Function (or Sub) FunctionName()
<declare variables>
On Error GoTo ErrorHandler

<all your main procedure code goes here>

ExitAndCleanUp:
On Error Resume Next ' ignore errors from here on
<clean-up code>
Exit Function ' or Sub

ErrorHandler:
<examine error code (Err.Number)>
If <error can be fixed> Then
<fix error condition>
Resume ' try again
End If
If <error condition can be ignored> Then
<maybe display message>
Resume Next
End If
' otherwise
<maybe display message>
Resume ExitAndCleanUp

End Function ' or Sub
 
G

Guest

Thanks for the reply Graham. I chose to use the simplest form of the code for
now just to try to get it to work. It looks like below, and it works if a
correct acct number is typed in but gives a blank screen (no error message)
if an invalid acct is typed in. This is how it started, I need to have an
error message pop up with an invalid entry.

Public Function Verify_Input() As Boolean
On Error GoTo ErrorHandler

Private Sub cmdOK_Click()
DoCmd.OpenForm "Parcel Detail Form", acViewNormal, acEdit
DoCmd.Close acForm, "frmParameter"

Exit Sub


ErrorHandler:
MsgBox ("APN not found.")
Resume

End Sub

What could be wrong with it?

Dino



Graham Mandeno said:
Hi Dino

Your placement of the error handler is fine. It's normal to place it after
the normal exit point of the function and before the end of the function.
Usually you would also have another label after the main function code and
before Exit Function. This allows you to "clean up" (close recordsets, etc)
irrespective of whether or not an error has occurred.

Also important is the Resume statement. It comes in three flavours:

Resume
retries the statement that caused the error (presumably your error
handler has done something to rectify the condition that causes the error,
otherwise you will have an infinite loop!)

Resume Next
ignores the statement that causes the error and resumes at the next
statement

Resume SomeLabel
resumes execution at a given label - most often used to "bail out" of
the function but execute the clean-up code before exiting.

So, your standard template for the body of a procedure or function might
look like this:

Function (or Sub) FunctionName()
<declare variables>
On Error GoTo ErrorHandler

<all your main procedure code goes here>

ExitAndCleanUp:
On Error Resume Next ' ignore errors from here on
<clean-up code>
Exit Function ' or Sub

ErrorHandler:
<examine error code (Err.Number)>
If <error can be fixed> Then
<fix error condition>
Resume ' try again
End If
If <error condition can be ignored> Then
<maybe display message>
Resume Next
End If
' otherwise
<maybe display message>
Resume ExitAndCleanUp

End Function ' or Sub
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand


Dino said:
I need to insert a very simple error handling procedure in my code to open
a
form. I'm not getting where to place it in the original code sequence.
Both
are very simple but they aren't working together so far. I have a form
with a
text box for entering account number, and an OK button which opens a form
that's based on a query. It works fine until I try to put the error
handling
in there. It either ignores it completely or gives me an error message
depending on where I put the error code.

Original:
Private Sub cmdOK_Click()
DoCmd.OpenForm "Account Detail Form", acViewNormal, acEdit
End Sub

Error handling code:
Public Function Verify_Input() As Boolean
On Error GoTo ErrorHandler

Exit Function

ErrorHandler:
MsgBox ("Account not found.")
Resume

End Function

Where does this error code go in relation to the original open form code?

Dino
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top