Consulting

Results 1 to 3 of 3

Thread: Error handling odd problem

  1. #1
    VBAX Newbie
    Joined
    Jun 2009
    Posts
    1
    Location

    Error handling odd problem

    Dear Experts,

    I have a very odd problem with error handling.
    I want to have multipal error handlings inside a single module.

    The problem is that the firt error is successfully read, but the next one not.

    Will try to explain how my code is written.

    [vba]Sub errors()

    dim job_1, job_2, job_3 as boolean

    job1:
    On error goto errormsg1
    job_1 = false
    'my code'
    job_1=true

    job2:
    on error gotto errormsg2
    job_2 = false
    'my code'
    job_2=true

    job3:
    on error gotto errormsg3
    job_3 = false
    'my code'
    job_3=true


    Exit Sub

    errormsg1:
    if job_1 = false then
    msg box "job1 didnot run"
    go to job2
    endif

    errormsg2:
    if job_2 = false then
    msg box "job2 didnot run"
    go to job3
    endif

    errormsg3:
    if job_3 = false then
    msg box "job3 didnot run"
    go to job4
    endif

    End Sub[/vba]


    Now let us say that there is an error in job2 , system reads the code correctly and goes to errormsg2.
    After reading errormsg2, it goes to job3. Now say if there is error in job3, it is not going to errormsg3, it is throwing up the debug error box.

    Hope i have made my problem clear.

    Thanks
    Subbu.
    Last edited by Bob Phillips; 07-06-2009 at 05:29 AM. Reason: Added VBA tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use Resume not Goto in the error handler

    [vba]

    Sub errors()
    Dim job_1, job_2, job_3 As Boolean

    job1:
    On Error GoTo errormsg1
    job_1 = False
    'my code'
    job_1 = True

    job2:
    On Error GoTo errormsg2
    job_2 = False
    'my code'
    job_2 = True

    job3:
    On Error GoTo errormsg3
    job_3 = False
    'my code'
    job_3 = True

    Exit Sub

    errormsg1:
    If job_1 = False Then
    MsgBox "job1 did not run"
    Resume job2
    End If

    errormsg2:
    If job_2 = False Then
    MsgBox "job2 did not run"
    Resume job3
    End If

    errormsg3:
    If job_3 = False Then
    MsgBox "job3 did not run"
    Resume job4
    End If

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    A small thing that might make a difference later ... the way the variables are Dim-ed

    [vba]
    Dim job_1, job_2, job_3 As Boolean
    [/vba]

    Make job_1 and job_2 Variant, and from context I assume that your intention was to also make them Boolean

    [vba]
    Dim job_1 As Boolean, job_2 As Boolean, job_3 As Boolean
    [/vba]

    I believe that VB and VBA treat unexplicitly DIM-ed variables differently

    [vba]
    Sub errors()
    Dim job_1, job_2, job_3 As Boolean


    'vbEmpty 0 Empty (uninitialized)
    'vbNull 1 Null (no valid data)
    'vbInteger 2 Integer
    'vbLong 3 Long integer
    'vbSingle 4 Single-precision floating-point number
    'vbDouble 5 Double-precision floating-point number
    'vbCurrency 6 Currency value
    'vbDate 7 Date value
    'vbString 8 String
    'vbObject 9 Object
    'vbError 10 Error value
    'vbBoolean 11 Boolean value
    'vbVariant 12 Variant (used only with arrays of variants)
    'vbDataObject 13 A data access object
    'vbDecimal 14 Decimal value
    'vbByte 17 Byte value
    'vbUserDefinedType 36 Variants that contain user-defined types
    'vbArray 8192 Array

    MsgBox VarType(job_1)
    MsgBox VarType(job_2)
    MsgBox VarType(job_3)

    job_1 = True
    job_2 = True
    job_3 = True

    MsgBox VarType(job_1)
    MsgBox VarType(job_2)
    MsgBox VarType(job_3)


    job_1 = 1234.56
    job_2 = "abcdef"

    MsgBox VarType(job_1)
    MsgBox VarType(job_2)
    MsgBox VarType(job_3)


    End Sub
    [/vba]

Posting Permissions

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