Results 1 to 12 of 12

Thread: Error hand

  1. #1

    Thread Starter
    Lively Member sameer spitfire's Avatar
    Join Date
    Nov 2001
    Location
    India
    Posts
    120

    Error hand

    I want to have a single error hand for the whole application
    there are about 6 to 7 forms in it

    any error accurres , i want to type the information into a log file and should unload the the current form

    where should i write the code

    my applications loads form sub main()


    Please hepl urgent
    with Regards
    sameer Mulgaonkar

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    no easy way I think,

    in your form, something like this :

    VB Code:
    1. Private Sub Command1_Click()
    2.     On Error GoTo xit
    3.    
    4.     'code
    5.     'code
    6.     'code
    7.    
    8.     Exit Sub
    9. xit:
    10.     LogIt Me.Name & " -> " & "Command1_Click()" & "Err: " & Err.Number & " -> " & Err.Description
    11. End Sub
    12.  
    13. Private Sub Form_Load()
    14.     On Error GoTo xit
    15.    
    16.     'code
    17.     'code
    18.     'code
    19.    
    20.     Exit Sub
    21. xit:
    22.     LogIt Me.Name & " -> " & "Form_Load()" & "Err: " & Err.Number & " -> " & Err.Description
    23. End Sub

    in a module

    VB Code:
    1. Option Explicit
    2.  
    3. Public Function LogIt(sInfoToLog As String, Optional sFileName As String)
    4.     Dim ff As Integer
    5.     If sFileName = "" Then sFileName = App.Path & "\" & App.EXEName & ".LOG"
    6.     ff = FreeFile
    7.     Open sFileName For Append As ff
    8.     Print #ff, sInfoToLog
    9.     Close ff
    10. End Function


    tidious work
    -= a peet post =-

  3. #3

    Thread Starter
    Lively Member sameer spitfire's Avatar
    Join Date
    Nov 2001
    Location
    India
    Posts
    120
    So for every procedure in the form i have the write the code
    is that so?
    that it become very duplication of job
    i can i have a central error handler when my application starts
    i get activated and so on
    with Regards
    sameer Mulgaonkar

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by sameer spitfire
    So for every procedure in the form i have the write the code
    is that so?
    ...
    yes
    -= a peet post =-

  5. #5
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Angry

    This drives me nuts!!! Grrrrr... On Error events should have been classed to procedures, modules, public, etc. ages ago, just like variables. I usually end up making something to go through the code and write text for error handling for every procedure. What a pain!

  6. #6
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Originally posted by peet
    yes

    Does VB let you line jump to a label outside the current scope?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  7. #7
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Nope.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  8. #8
    Addicted Member nota141's Avatar
    Join Date
    Feb 2000
    Location
    The place down there under everyone else.
    Posts
    224
    i modded orion belt so that it also adds the error handler lines to each modual
    On Error wake up and try again ;-)
    ___________________________
    ICQ # 65392645
    email - [email protected]

    Visual Studio 6 ent with SP5 Running on XP with an AMD 1.2 ThunderBird with 512 MB RAM And a 120GB primary HDD (very sweet)

  9. #9
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    You can have a public procedure in a standard module which accepts an error object (or its number and description etc.) and performs whatever actions you want to be taken on an error. Then you can simply call this procedure from each procedure's error handler. This way, although technically you have to write error handlers for every procedure, practically all your errors will be being handled at a central place. Also in this case since the error handlers in each procedure will be almost identical, you can use some tools such as MZTools (www.mztools.com) to insert a "procedure header" into your application which contains the error handler.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    I use a module for error handling (that way I can have lots of functionality - eg: save errors to log files/email to me, nice messages for common errors..), but it needs to be called from each sub.

    Here is the code that I copy & paste to use it:

    VB Code:
    1. On Error GoTo ErrorHandler:
    2.   Screen.MousePointer = vbHourglass
    3.  
    4. '** (module/function here) **
    5.  
    6.   Err.Clear    '(ignore errors under any previous Resume Next)
    7. ErrorHandler:                       '** STANDARD ERROR HANDLING CODE: **
    8.   If Err > 0 Then
    9.     If Std_Error_Handler(Me.Name & " - **module/function name**", True, App.title, SQL) = "ignore" Then Resume Next
    10.     If ResumeNow Then Resume
    11.   End If
    12.   On Error Resume Next              '** EXIT SUB/FUNCTION CODE (eg: re-enable form etc.) **
    13.   Screen.MousePointer = vbDefault

    as you can see, "Std_Error_Handler" is the function that does it all, and returns "ignore" for a resume next, or sets the global "ResumeNow" to true for a resume (yes, I know now a Select Case would be better, but legacy code I'm afraid!).

  11. #11
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Originally posted by rjlohan
    Does VB let you line jump to a label outside the current scope?
    If you have an error handler in the upper scope ( sub or whatever where other function or whatever calls are done)
    and not in the ones you call from it , and an error occurs the error trap in the upper scope will trap it.

    If you don't want to rewrite an error handler each time again , you could consider making your own add-in, make one which creates your own property's (get-let) functions , subs.

    Then when you add a new function or sub or property do it with the add-in, this could make it a lot easier.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  12. #12
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Expanding on the above concept you could only have error handlers in the event procedures of your controls, and maybe in the Sub Main if you have one. This should probably make matters easier. The idea is to only have error handling in the topmost of all procedures. Barring the Sub Main and the control event procedures, all other procedures are actually on the second or later level of execution, i.e. they are called by either the Sub Main or the control event procedures. However the Sub Main and the control event procedures do not have any "callers", so to speak. Also an error that occurs in a procedure can be trapped by that procedure or by its calling procedure. Extending this idea further, if none of the procedures handle an error, it can be trapped in the topmost of procedures, which is the Sub Main or control event procedures.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

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