Trapping for an error

Can we trap an error at module level? I don't want to write "on error" in each and every procedure.
kbalarajuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcriderCommented:
On Error needs to be used in each module.
0
JuiletteCommented:
on error in your main sub will trap errors in all the subs it calls.
0
mcriderCommented:
at the procedure level.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

kbalarajuAuthor Commented:
Could u write in more detail if possible with an exapmple. My project has one module
0
Erick37Commented:
No.
Once you leave a function or sub, the current error handler is no longer active.
0
Erick37Commented:
Oops, Wayne is correct.
If you have a Sub Main() then the error handler there will be active until your program ends.  But tracking where the error is raised will be a problem.
0
kbalarajuAuthor Commented:
Can't we write the module level Error trapping routines as my application contains 70 to 80 procedures. And I need to test for whether the database is available or not before making any quey on the database. So, i want to avoid redundancy of code for the error trapping.
0
JuiletteCommented:
Thank you....
write your error handler as a sub and call it on error in each function or sub.

On Error
Call ErrHanler

Wayne
0
kbalarajuAuthor Commented:
Juilette,
As per your suggestion
I added the following code in my module. But it is not coming to ErrRoutine when it encounters an error in form_load event of start form. It is giving runtime error message only.

Public Sub main()
    On Error GoTo ErrHandler
    start.Show
    Exit Sub
ErrHandler:
    Call ErrRoutine
End Sub

Public Sub ErrRoutine()
    MsgBox "Error Routine"
    End
End Sub

Thank You
Raju
0
AnswerTheManCommented:
:)
first you talk about a module. then a FORM is being mentioned...
after the <Start.Show> line - the Start
form is dispalyed AND the SubMain is OVER !!!
the error trapping is over too with the SubMain.
this would not happened if it was
<Start.Show 1>, but that is up to you to decide if you want the form modal or not.
0
kbalarajuAuthor Commented:
Thank you...
Public Sub main()
    On Error GoTo ErrHandler
    start.Show vbModal
    Exit Sub
ErrHandler:
    Call ErrRoutine
End Sub

Public Sub ErrRoutine()
    MsgBox "Error Routine"
    End
End Sub

I tried with above code. Still the control doesn't transferred to ErrRoutine.

My application has multiple forms and I want to use the same error routine everywhere.
0
mcriderCommented:
What version of VB are you using?
0
JuiletteCommented:
..what is the runtime error.
0
kbalarajuAuthor Commented:
I'm using VB6.
I'm trying to open a Access Database connection using ADOs. Basically the database doesn't exists. I just want to generate a runtime error & doing that.
0
JuiletteCommented:
where is the open in the form load or start?
I'll dupe it into an app on this end and see what is happening.
Give me the code...not for all your forms ...just for the experimental open.

0
AnswerTheManCommented:
are you sure your app starts with SubMain ???
0
JuiletteCommented:
that's why I want the code...2 secs to check it.
0
JuiletteCommented:
It works as I stated...perhaps you misunderstood...you don't have to write the errhandler for each function but you have to reference it.
I tested this with your submain and it works. I left a:\ empty to produce error


Private Sub Start_Load()
On Error GoTo err:

   Open "a:\Help.mdb" For Input As #1
err:
Call ErrRoutine

End Sub

Wayne
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kbalarajuAuthor Commented:
'Form load is as follows

Private Sub Form_Load()
    Dim DSNstr As String
                       
    '// init log file and write the log header
    g_lLogFileNum = FreeFile
    Open "C:\ConventionExpress\ConvData\" & g_sLogFileName For Append As #g_lLogFileNum
    Print #g_lLogFileNum, ""
    Print #g_lLogFileNum, ""
    Print #g_lLogFileNum, Now()
    Print #g_lLogFileNum, ""
    '// -----------------------------------------------------------
           
'   keypreview=true causes keyboard events for forms are invoked before keyboard events for controls.
'   The keyboard events are KeyDown, KeyUp, and KeyPress.
    KeyPreview = True
   
    InitFolders DSNstr
    'Open the ADO connection for SQL Server database
    cn.CommandTimeout = 30
    cn.Open "DSN=" & DSNstr & ";uid=sa;pwd=workisgood"
   
    If (cn.Properties("DBMS Name").Value <> "ACCESS") Then
        UpdateAccessDB = True
    Else
        UpdateAccessDB = False
    End If
    If (UpdateAccessDB) Then
        'Open connection for Access database
        cnAccessDB.CommandTimeout = 30
        cnAccessDB.Open "DSN=ConventionExpressAccessDB;uid=;pwd=;"
    End If

    InitGlobalVariables
    InitLabels
    InitCardReader
    FindConventionID
    InitListSpecialPrograms
    CreateShowcaseBtnArray
    FindPendingEntries
    MakeControlsVisible False
    MakeDrugsVisible False
    Exit Sub
End Sub
0
Erick37Commented:
Once you have left Sub Main() (even with a modal form) the error handler is no longer active.  An error in Form1 will use any handler in the procedure of the form and not the Main() handler.  You have to place an On Error statement in each of the functions you wish to handle errors in.

The error handler can call a function in a module, but you still need the On Error statement in all procedures to catch them.

I think... (VB5)
0
kbalarajuAuthor Commented:
Juilette, Eric37! You are correct.
But I can accept Juilette's answer as it was answered first.
Thanks
0
JuiletteCommented:
Sounds fair to me....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.