Results 1 to 8 of 8

Thread: [RESOLVED] Code for ERROR HANDLING !!!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2005
    Posts
    570

    Resolved [RESOLVED] Code for ERROR HANDLING !!!

    How can I write the code if I don't know or don't want to write error number?

    Private Sub cmdfirst_Click()
    On Error GoTo FIRST
    rs.MoveFirst
    show_rec
    FIRST:
    If Err.Number = 3021 Then 'How can I modify the code here?
    End If
    End Sub

  2. #2
    Hyperactive Member vbcode1980's Avatar
    Join Date
    Nov 2005
    Location
    Anywhere the wind blows
    Posts
    365

    Re: Code for ERROR HANDLING !!!

    Not sure if I understand the question.
    What has to be done when the error is raised?
    I code C#....

  3. #3
    Lively Member eusty's Avatar
    Join Date
    Jan 2006
    Location
    UK
    Posts
    71

    Re: Code for ERROR HANDLING !!!

    Why not write the errors to a .txt file?
    VB Code:
    1. ErrorLog:
    2. Open App.Path & "\errorlog.txt" For Append As #1
    3. Write #1, Format(Now, "C"), Err.Number, Err.Description, " Your Descriptiion"  
    4. Close #1
    Steve

    No trees were cut down or harmed in the posting of this message. A lot of electrons were, however, severely inconvenienced.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2005
    Posts
    570

    Re: Code for ERROR HANDLING !!!

    Quote Originally Posted by vbcode1980
    Not sure if I understand the question.
    What has to be done when the error is raised?
    On Error GoTo FIRST (This is ok)
    If Err.Number = 3021 Then (In this sentence what should I write in the place of Err.Number = 3021 if I dont want to write the error number like this "3021".

  5. #5
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Code for ERROR HANDLING !!!

    Quote Originally Posted by seema_s
    On Error GoTo FIRST (This is ok)
    If Err.Number = 3021 Then (In this sentence what should I write in the place of Err.Number = 3021 if I dont want to write the error number like this "3021".

    could do this,

    VB Code:
    1. On Error GoTo FIRST
    2.     rs.MoveFirst
    3.     show_rec
    4.  
    5. Exit Sub
    6.  
    7. FIRST:
    8.     MsgBox Err.Description & " " & Err.Number

  6. #6
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Code for ERROR HANDLING !!!

    Quote Originally Posted by seema_s
    How can I write the code if I don't know or don't want to write error number?

    Private Sub cmdfirst_Click()
    On Error GoTo FIRST
    rs.MoveFirst
    show_rec
    FIRST:
    If Err.Number = 3021 Then 'How can I modify the code here?
    End If
    End Sub
    What exactly do you want the error handler to do for you?
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Code for ERROR HANDLING !!!

    You have a couple of options. First, you don't have to use any If statment at all.
    VB Code:
    1. Private Sub Command1_Click()
    2. On Error Goto ErrTrap
    3. 'code for click event
    4. Exit Sub
    5. ErrTrap:
    6. 'use eusty's example
    7. Open "c:\ErrorLog.txt" For Append As #1
    8. Print #1, Format(Now, "mm/dd/yyyy") & " " & Err.Number & " " & Err.Description
    9. Close #1
    10. End Sub
    This will trap and log the error. Note, no code after the error will be executed.

    If you want to trap for a specific error, as well as all other errors, then you can use the If statement
    VB Code:
    1. Private Sub Command1_Click()
    2. On Error Goto ErrTrap
    3. 'code for click event
    4. Exit Sub
    5. ErrTrap:
    6. If Err.Number = 3021 Then
    7.    'do something for this specific error
    8. Else
    9.    'do something else regardless of what the error is
    10. End If
    11. End Sub
    Again, no code will be executed after the line the error occurred on. If you want to trap and log the error, and continue code execution, then in your error trap, log the error, then add a Resume Next, which will continue executing the code after the line on which the error has occured.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2005
    Posts
    570

    Re: Code for ERROR HANDLING !!!

    Quote Originally Posted by Hack
    You have a couple of options. First, you don't have to use any If statment at all.
    VB Code:
    1. Private Sub Command1_Click()
    2. On Error Goto ErrTrap
    3. 'code for click event
    4. Exit Sub
    5. ErrTrap:
    6. 'use eusty's example
    7. Open "c:\ErrorLog.txt" For Append As #1
    8. Print #1, Format(Now, "mm/dd/yyyy") & " " & Err.Number & " " & Err.Description
    9. Close #1
    10. End Sub
    This will trap and log the error. Note, no code after the error will be executed.

    If you want to trap for a specific error, as well as all other errors, then you can use the If statement
    VB Code:
    1. Private Sub Command1_Click()
    2. On Error Goto ErrTrap
    3. 'code for click event
    4. Exit Sub
    5. ErrTrap:
    6. If Err.Number = 3021 Then
    7.    'do something for this specific error
    8. Else
    9.    'do something else regardless of what the error is
    10. End If
    11. End Sub
    Again, no code will be executed after the line the error occurred on. If you want to trap and log the error, and continue code execution, then in your error trap, log the error, then add a Resume Next, which will continue executing the code after the line on which the error has occured.
    Thanks a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width