PDA

View Full Version : Error handling odd problem



skbhat03
07-06-2009, 05:16 AM
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.

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


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.

Bob Phillips
07-06-2009, 05:27 AM
Use Resume not Goto in the error handler



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

Paul_Hossler
07-06-2009, 07:59 AM
A small thing that might make a difference later ... the way the variables are Dim-ed


Dim job_1, job_2, job_3 As Boolean


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


Dim job_1 As Boolean, job_2 As Boolean, job_3 As Boolean


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


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