Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error Handler

Posted on 1999-07-19
8
Medium Priority
?
258 Views
Last Modified: 2011-09-20
I am writing an error handler in cmdSave button.  The codes are like this:
private sub cmdSave_click()
   on error goto errorhandler
     .....
     .....
   exit sub
errorhandler
   msg err.description
   err.clear
end sub

When I run the program and click the Save button, an error occur and the err message pop up.  It requires me to input an valid value in a text box.  I then put an valid value in, but it still display the same message even though I put in a valid value already.  It seemed like the err.clear did not work right.  Anyone have any idea how to stop that err message from display after I fix the problem?  By the way, err is an run-time error
0
Comment
Question by:cdloves
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:agrillage
ID: 1525902
Your error.clear statement is OK. Can we see your validation code just in case you are doing something wrong there.

Andrew
0
 

Author Comment

by:cdloves
ID: 1525903
Private Sub cmdSave_Click()
    'Add/Save NewJob
    Err.Clear
    On Error GoTo ErrorHandler 'If Error occur, jump to errorfound
    Dim ans As Integer
    Dim messages As String
    Dim curjob As String
    messages = "Please check all information before Add/Save" + vbCrLf + vbCrLf + "          READY TO SAVE?"
    ans = MsgBox(messages, vbYesNo, "Add/Save New Job")
    If ans = 6 Then 'User clicked yes
        'Start saving information into database
        With datJobs.Recordset
            '.MoveFirst
            .AddNew                         'Create a new BLANK ROW
            !EntryDate = txtDate.Text       'set EntryDate
            !JobNumber = txtJobNo.Text      'set Jobnumber
            !PoNumber = txtPONo.Text        'set PONumber
            !partnumber = dbcPartNo.Text
            !Customer = dbcCustomer.Text
            '!Billingaddress1 = txtaddress.Text
            !shipaddress1 = txtshipAddress1.Text            
            .Update
        End With
        Open "d:curjob.txt" For Output As #2    'Open curjob.txt
        curjob = txtJobNo.Text                  'set curjob var as txtjobno.text
        Print #2, curjob                        'Print curjob into curjob.txt file
        Close #2                                'close curjob.txt file
        'Done saving the current job
   
        ans = MsgBox("New Job has been successfully saved", vbExclamation, "New Job Saved")
    Else            'User clicked no
        MsgBox ("The new job is not SAVED")
    End If
    'if no error found then exit function here
    Exit Sub
ErrorHandler:
    MsgBox Err.Description + vbCrLf + vbCrLf + "FAILED TO SAVE NEW JOB", vbInformation, "Failed"
    Err.Clear
End Sub


Comments:  When I left the txtPONo blank, the compiler would say "field 'Jobs.PONumber' can't be a zero-length string".  Without exit the program, I input value for the PONo text but it still pop up the same message as though it did not notice that I just put in some value for the text box.  If any text was already in the text box the first time I click the SAVE command, it would not display the error message.

Same thing happen to other fields as well.  Hope this would make you understand my problem.  By the way, the ".update" statement is the place where the error occurs.
0
 

Expert Comment

by:amesg
ID: 1525904
It seems like your data isn't formatted right.  Your database doesn't like the information you are passing it.  If a value isn't filled in, you might want to default it to a space or possibly use NZ() to return NULL.

Not inputting a value for a data value which can't be zero length will cause your error, because the field value isn't valid.  That's why when you enter all fields it doesn't error out on you.

amesg
Let me know if that helps
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:cdloves
ID: 1525905
I understand what you mean, but thats is not my point.  When I first run the program, before entering any value in text box, I click save.  it displayed that cant take zero-length string.  Then I input "aaa" and clicked save again.  It still display the same message.  However, after I closed the program and run it again.  This time, I input "aaa"  and then click save, it would accept the value.
0
 
LVL 7

Expert Comment

by:kamall
ID: 1525906
The reason for the error comming again is that the record can not be updated after an error happend even though you supply the appropriate data type or value. What you should do, which is the most reliable solution, is to check the data type and value BEFORE executing the ADDNEW statement. If everything is ok, then you issue the ADDNEW with all the remaining code.
Hope that helps.
Regards.
0
 

Author Comment

by:cdloves
ID: 1525907
Is there a way for me to clear the error in update statement so I dont have to check all data before calling Update?
0
 

Accepted Solution

by:
scottsch earned 400 total points
ID: 1525908
Your code shows that you merely cleared the error code back to 0, however, your update is still pending, you need to issue the CancelUpdate method.
(It is a good practice to validate the data before the update)


Private Sub cmdSave_Click()
    'Add/Save NewJob
'--    Err.Clear
    On Error GoTo ErrorHandler 'If Error occur, jump to errorfound
    Dim ans As Integer
    Dim messages As String
    Dim curjob As String
    messages = "Please check all information before Add/Save" + vbCrLf + vbCrLf + "          READY TO SAVE?"
    ans = MsgBox(messages, vbYesNo, "Add/Save New Job")
    If ans = 6 Then 'User clicked yes
        'Start saving information into database
        With datJobs.Recordset
            '.MoveFirst
            .AddNew                         'Create a new BLANK ROW
            !EntryDate = txtDate.Text       'set EntryDate
            !JobNumber = txtJobNo.Text      'set Jobnumber
            !PoNumber = txtPONo.Text        'set PONumber
            !partnumber = dbcPartNo.Text
            !Customer = dbcCustomer.Text
            '!Billingaddress1 = txtaddress.Text
            !shipaddress1 = txtshipAddress1.Text            
            .Update
        End With
        Open "d:curjob.txt" For Output As #2    'Open curjob.txt
        curjob = txtJobNo.Text                  'set curjob var as txtjobno.text
        Print #2, curjob                        'Print curjob into curjob.txt file
        Close #2                                'close curjob.txt file
        'Done saving the current job
     
        ans = MsgBox("New Job has been successfully saved", vbExclamation, "New Job Saved")
    Else            'User clicked no
        MsgBox ("The new job is not SAVED")
    End If
    'if no error found then exit function here
cmdSave_ClickExit:
    Exit Sub
    '
    '
ErrorHandler:
    MsgBox Err.Description + vbCrLf + vbCrLf + "FAILED TO SAVE NEW JOB", vbInformation, "Failed"
    datJobs.Recordset.CancelUpdate
    Resume cmdSave_ClickExit
End Sub
0
 

Author Comment

by:cdloves
ID: 1525909
Thanks a lot, Scottsch.  The problem is solved...thank you
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

704 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