by Jeffry O'Neil ()
Category: Custom Controls/Forms/Menus
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sun 10th February 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Handle Errors Within Forms
When you load or show a form, errors don’t bubble up. That is, even if the calling procedure has an error handler, and an error occurs in Form_Load, Form_Initialize, or any other form event, processing doesn’t transfer to the calling error handler. In this code, Sub Main has an error handler. But when an error occurs in the Form_Load, the error handler isn’t called:
' Code in a bas module
Sub Main()
On Error Resume Next
Load Form1
' further processing code ...
End Sub
' Code in Form1
Private Sub Form_Load()
Dim a As Integer
a = 1 / 0 ' error is fatal!
End Sub
If you check the call stack when the error occurs, you see an entry '<Non-Basic Code>' before the Form_Load. Even though Sub Main is loading Form1, Sub Main is not the direct caller of Form_Load, and that results in this behavior.
'thanx to Ravindra Okade
'We bear witness that there is no God but Allah and Muhammad is the slave and prophet of Allah!