Link to home
Start Free TrialLog in
Avatar of kian042298
kian042298

asked on

handling run-time error

Dear friends,
    I've got a VB application with four forms. And each of the forms contain some sql statement such as insert,delete, update and select. I'm using ODBC and sql passthrough. However there would be times when my sql passthrough would have error due to some server problem(ie when server is down). Below is so called the pictorial form.

<Form 1>
Dim connect as string,db as database
connect$="ODBC;DSN=Oracle;UID='" & txt_1.Text & "';Pwd='" & txt_2.TExt & "';
set db=opendatabase("",false,false,connect)

<Form 2>
other vb codes....
sql_insert="Insert into table......"
db.execute sql_insert,dbsqlpassthrough
other vb codes....

<Form3>
other vb codes.....
sql_truncate="Truncate table...."
db.execute sql,dbsqlpassthrough
sql_insert="Insert into table......"
db.execute sql_insert,dbsqlpassthrough

<FOrm4>
other vb codes......
sql_delete="Delete table......"
db.execute sql_delete,dbsqlpassthrough
other vb codes...

Expert friends,
Can anyone please advise me with workable VB codes as how I should handle the run-time errors for these sql passthrough.
Q1)For statement "On error go to Error handler",are we supposed to define only once in every form or do we only need to define in one form.And in every form can we define more than once, is it necessary.

Q) Besides sql passthrough giving me run-time errors, there would be times when other of my VB Codes would give me run-time errors as well.
Therefore I would be most grateful if someone Can advise me on how I should code my application in order to handle these errors together(other vb codes and sql passthrough) elegantly.
ASKER CERTIFIED SOLUTION
Avatar of bin_huwairib
bin_huwairib

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kian042298
kian042298

ASKER

Hi Bin_huwairib,
   I realised you always provide others with sound and solid VB codes and explanation. And I also do noticed that your ranking is going up as well. So sincerity and being helpful does, isn't it? Yes men! Bravo! Once again, thank you for all that.
   Just one more doubt to clarify with you, does it mean that I will have to declare the above section of codes in every of my VB forms and with that I would be able to capture and handle any run-time errors so long as I know the error number. If that is so where would be the best place to put in those error handling codes based on my scenario in the early question (any guidelines). Could you please enlighten me. Once again, thank you.
kian,

Define it in every sub/function in each form because the handler will be in the procedure level only not for the whole form.

Example
=======
Suppose you have form1 with two subs: Form_Load and ConnectToSQL in this case you have to define the handler for both of the subs, it will be as follows:

Sub Form_Load()
 On Error Goto Err_Form_Load

 ' Your code....
 '..................

Exit_Form_Load:
 On Error Goto 0
 Exit Sub

Err_Form_Load:
 MsgBox Err.Description
 Resume Exit_Form_Load
End Sub

Sub ConnectToSQL(Param1 As String)
 On Error Goto Err_ConnectToSQL

 ' Your code....
 '..................

Exit_ConnectToSQL:
 On Error Goto 0
 Exit Sub

Err_ConnectToSQL:
 MsgBox Err.Description
 Resume Exit_ConnectToSQL
End Sub

Now you must similarly define the handler for the rest of subs/functions for this form and the other forms as well.

Ben Huwairib