Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get ON ERROR working on SQLEXEC command

Status
Not open for further replies.

ellehcsim

Programmer
Dec 9, 2003
16
0
0
PH
hi guys!

this is my code...

ON ERROR do errorHandler

lSqlString = "update a_item_masters set a_field_her 'a'"

&& This code should produce an ODBC Error
SQLEXEC(g_connHandle,lSqlString,&quot;item_query&quot;) < 0

MESSAGEBOX(&quot;if this messagebox appears, errorHandler dosen't work&quot;)

ON ERROR

PROCEDURE errorHandler

=AERROR(lar)
MESSAGEBOX(lar(2),48,&quot;SQL Server Error&quot;)
RETURN TO MASTER

ENDPROC

supposedly, if the ON ERROR works after executing the SQLEXEC, the MESSAGEBOX wont appear anymore.

for the meantime, what i did is like this..

IF SQLEXEC(g_connhandle,lSqlString,&quot;item_query&quot;) < 0
do errorHandler
endif

.. which works just fine. but what if i have a series of updates, i have to do this IF..ENDIF statement just to insert the DO ERRORHANDLER.

is there a way on how i can make ON ERROR routine working in SQLEXEC command.

thanks!!!!



 
SQLExec, like API calls, TABLEUPDATE, and low-level file functions, do not trigger an error if they have a problem. They simply return an error code. Your technique of checking if the return value is negative is the correct way to do error handling in this case.


-BP (Barbara Peisch)
 
ellehcsim,

As Barbara says, errors reported by SQLEXEC() do not trigger the error handler. But, in other respects, your code is correct. Essentially, you are testing for a negative reply from SQLEXEC(), and then calling a routine which handles the error.

To deal with the problem of constantly having to test for a negative reply when you have a whole series of SQLEXEC() calls, I use a simple class which wraps up the whole thing. The class has a Send method which calls SQLEXEC(). It also has properties called SQLResult (which tells you if there is an error), ErroNo and ErrorMess (which tell you what the error is).

Of course, you still have to test for an error after each call to the Send method, but at least all the AERROR() stuff is done for you.

If you'd like a copy of my class (which I call SQLSend), you can grab it from:

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top