Understanding Error Messages

G

Guest

Please explain the function of each piece of the error type message such as
the one below.

Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click

DoCmd.Close

Exit_cmdClose_Click:
Exit Sub

Err_cmdClose_Click:
msgbox Err.Description
Resume Exit_cmdClose_Click

End Sub
 
S

Steve

On Error GoTo Err_cmdClose_Click ----
If an error occurs between this line of code and the end of the sub,
execution is redirected from that point to the line Err_cmdClose_Click

msgbox Err.Description ----
Access has an Errors collection containing Err objects. Description is a
property of the Err object that provides the error message of the Err
object. Msgbox is the standard Access function to dosplay a message.

Resume Exit_cmdClose_Click ----
Resume is a standard VBA code to redirect code just like Goto. When code
execution reaches this statement, execution is redirected to the line
identifier, Exit_cmdClose_Click.

Exit_cmdClose_Click: ----
Exit_cmdClose_Click is merely a code line identifier.

The arrangement of the code is significant! If there is an error, the
execution goes to Err_cmdClose_Click, displays the error, goes to
Exit_cmdClose_Click and exits the sub. If there is no error, the execution
goes line by line to Exit_cmdClose_Click and exits the sub.
Err_cmdClose_Click must be below Exit_cmdClose_Click so that it is only
executed if there is an error. If Err_cmdClose_Click came before
Exit_cmdClose_Click and there was no error, msgbox Err.Description would
raise an error because Err.Description would be null.
 
M

Mark

Ok, here goes a line-by-line explanation:

When you click the command button "cmdClose":
--If there's an error, go to the section called Err_cmdClose_Click
--Try to run the DoCmd.Close function
--Create a program label called "Exit_cmdClose_Click" so you can call it
later if needed
--Exit this subroutine here so it won't trigger the rest of the code
underneath
--Create a program label called "Err_cmdClose_Click" so you can branch to it
when an error occurs (see the On Error Goto... line)
--Put up a message box with a description of the error that occurred
--Go to the section called "Exit_cmdClose_Click"

This is the most basic form of error-handling which is created by the Access
wizards when you use them to create objects like combo boxes and command
buttons. Creating your own error-handling routines gives you a lot of
flexibility to handle many types of errors and allows you to do certain
things when certain errors occur. It also allows you to open your own
"human friendly" error messages that actually tell the user something useful
about what happend.
 
G

Guest

OK... when you use the VBA code for errors ... it spits out Microsoft's
errors. Is this correct?

The forms I'm creating require input for three fields. The user can press
the "Save" command button and my code checks for missing fields and responds
with MY error messages. It works fine. HOWEVER, if the user tabs through
the records without completing the fields, the messy system message appears.
Another senerio is when a user decides to close the form by pressing X. The
form will close whether the input for the record was complete or not.

I need MY error messages to appear HOWEVER the user exits the form. Do I
need to rewrite the MsgBox code for each scenerio??? If so, what events do
I need to include the message codes for???

VB reference guides explains bits and pieces. I would very much
appreaciate your assistance in explaining the overall logic of error message
placements for a simple input form.

THNAKS!
 
G

George Akers

Yes, you use On Error to trap runtime errors.

You must write your own validation routines. Put it in the form's Close
event.
 
G

Guest

Please explain the overall picture. On a form that requires user input to
three out of eight fields, where and what type of error code should I use?
for the Save button, for tabbing to the next record, for exiting out of the
form, for a user who attempts to click to the next row before completing the
current row?

new Record: field 1 (R) Field 2 (R) Field 3 (R) Field 4 Field 5
Field 6 (Tab)
New empty row
 
G

George Akers

Have you considered making the three fields required by setting the
Required Property to True and letting the database handle this for you?
 
G

Guest

I NNED TO REPLACE SYSTEM ERROR messages with my own messages. I need to
udnerstand the logic of where to put messages so when fields are left blank,
a message appears no matter what the user clikcs! PLEASE explain to me as a
novice VBA programmer! Thanks......
 
J

John Nurick

Hi Sharon,

Please explain the overall picture. On a form that requires user input to
three out of eight fields, where and what type of error code should I use?
for the Save button, for tabbing to the next record, for exiting out of the
form, for a user who attempts to click to the next row before completing the
current row?

new Record: field 1 (R) Field 2 (R) Field 3 (R) Field 4 Field 5
Field 6 (Tab)
New empty row
I think this thread got off on the wrong foot (if threads have feet)
because you asked about VBA error-handling code while it seems that what
you actually want to do is data validation, which is a different matter
entirely. We also need to distinguish carefully between fields (which
are a feature of queries and tables) and controls such as textboxes,
which are used to display data on forms.

Ordinarily a textbox on a form is bound to a text field in the query or
table to which the form is bound.

By default, the control gets the same name as the field it's bound to,
which helps confuse matters. So from now on I'm going to say txtField1
to mean the textbox that is bound to Field1, and so on.

To prevent the user from exiting txtField1 without entering a value, put
code in the textbox's BeforeUpdate event procedure. If it's just to make
sure that *some* value has been entered, something like this will do it
(and similarly for the other textboxes bound to the "required" fields):

Private Sub txtField1_BeforeUpdate(Cancel As Integer)
If IsNull(Me.ActiveControl.Value) Then
Cancel = True
MsgBox "You must enter a value in this field", _
vbOKOnly + vbInformation, "Sharon's Form"
End If
End Sub

To cover the situation where the user moves to the next record without
touching the "required" fields at all, use the Form's BeforeUpdate
event, something like this:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.Controls("txtField1").Value) _
And IsNull(Me.Controls("txtField2").Value) _
And IsNull(Me.Controls("txtField3").Value) Then
Cancel = True
MsgBox "You must enter values for Field1, Field2 and Field3", _
vbOKOnly + vbInformation, "Sharon's Form"
End If
End Sub

If you need to check for incorrect values as well as missing ones, the
principle is the same: use an If or Select Case statement in the
BeforeUpdate event to test the value, and Cancel it and give feedback
with a messagebox if it fails the test.

As the name suggests, BeforeUpdate kicks in before the field or record
is updated, so the user will get your message and not the default one
triggered by an empty "required" field.
 
G

Guest

George,
Thanks for the clarification between error handling and validation. Your
code and placement suggestions make a lot of sense, however, I'm getting an
error message: "The control number you specificed is greater than the number
of controls". Any ideas?
 
G

Guest

John,
this should have gone to you....

Thanks for the clarification between error handling and validation. Your
code and placement suggestions make a lot of sense,

Thanks,
Sharon
 
F

flipfantasia

Excuse me if my message is not filled in right form
I'd like to know the meaning of this error message that is not listed in
the MS site:
Event Type: Error
Event Source: System Error
Event Category: (102)
Event ID: 1003
Date: 14/12/2008
Time: 14.02.23
User: N/A
Computer: USER-FE7063C07B
Description:
Error code 000000c2, parameter1 00000007, parameter2 00000cd4, parameter3
02020001, parameter4 89daf3c8.

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.
Data:
0000: 53 79 73 74 65 6d 20 45 System E
0008: 72 72 6f 72 20 20 45 72 rror Er
0010: 72 6f 72 20 63 6f 64 65 ror code
0018: 20 30 30 30 30 30 30 63 000000c
0020: 32 20 20 50 61 72 61 6d 2 Param
0028: 65 74 65 72 73 20 30 30 eters 00
0030: 30 30 30 30 30 37 2c 20 000007,
0038: 30 30 30 30 30 63 64 34 00000cd4
0040: 2c 20 30 32 30 32 30 30 , 020200
0048: 30 31 2c 20 38 39 64 61 01, 89da
0050: 66 33 63 38 f3c8
The error occurs at the start. The system (windows XP Home) seems to works.
Anyone can explain me?
 
G

Guest

flipfantasia said:
Excuse me if my message is not filled in right form
I'd like to know the meaning of this error message that is not listed in
the MS site:
Event Type: Error
Event Source: System Error
Event Category: (102)
Event ID: 1003
Date: 14/12/2008
Time: 14.02.23
User: N/A
Computer: JKIU7867875875760000000000000000000000000000000.
096> 0038: 30 30 30 30 30 63 64 34 00000cd4
 

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

Top