views:

213

answers:

3

I'm working on a legacy VB6 app here at work, and it has been a long time since I've looked at VB6 or ADO. One thing the app does is to executes SQL Tasks and then to output the success/failure into an XML file. If there is an error it inserts the text the task node.

What I have been asked to do is try and do the same with the other mundane messages that result from succesfully executed tasks, like (323 row(s) affected).

There is no command object being used, it's just an ADODB.Connection object. Here is the gist of the code:

Dim sqlStatement As String
Set sqlStatement = /* sql for task */

Dim sqlConn As ADODB.Connection
Set sqlConn = /* connection magic */

sqlConn.Execute sqlStatement, , adExecuteNoRecords

What is the best way for me to capture the non-error messages so I can output them? Or is it even possible?

A: 

I think the rows affected is a function of the Query Analyzer/Enterprise manager, and not something returned through the API.

If I recall correctly, using classic ADO, we had to do a MoveLast then a MoveFirst to force all of the records to come over the wire, then do a count of the Recordset.

I also remember something about which cursor type being used affecting the count of records coming back.

Other than that, are you trying to grab the print statement... It seems like you are using no stored procedures, so beyond count, what are you expecting to get?

Jeremy
Unfortunately I can't really say what I can expect, except I was specifically asked for "number of records modified" and "any success messages due to a query successfully completing". There may or may not be some sprocs being used at times here (the app is responsible for patching clients, so there could be few to very many tasks being executed. Being new, I Can't really speak to the nature of SQL tasks being executed, except that I suspect it varies widely).I am not sure if the ADO.Net InfoMessage can be used for this purpose, and wondered if there was an analogue.
peacedog
Well I've never seen any "success" message other than number of rows returned. The only thing that ever gets printed with messages is the result of PRINT statement is sprocs. I'm pretty sure those messages never make it to the client.
Jeremy
I'm in the process of getting more feedback, but I'm not clear if they want the capability to return PRINT statements or not (I'm planning for yes). I do know that something like a create sproc statement would still return "command successful" (or something similar), and I believe they want that too.
peacedog
If you are using sprocs, the only way to get something back is either through a return status or an output variable.
Jeremy
+4  A: 

Besides having a generic error handler in your routines the ADO connection object has an Errors collection. After performing some action check the errors for a count > 0 and if it is you need to iterate the collection and log all the errors. There is a Clear method if you want to continue after logging.

After making a quick test project I found that declaring my variable using WithEvents VB adds the InfoMessage event. I ran a DBCC CHECKDB command and the InfoMessage event fired once. The pConnection variable had 284 errors in it with all the other messages.

note: the Connection.Errors collection is 0 based.

Beaner
I know that, but if the SQL Statement in question is successful there won't be any errors, and that's already being taken care of.
peacedog
B is right. SQL "Errors" mean something totally different from typical VB errors. If the query fails, it will throw an error that you can catch in VB, but warnings only end up in the .Errors property of the ADODB.Connection object. B's suggestion to check the .Errors.Count will tell you if there are any warning messages (or non-fatal errors)
tgolisch
Sorry if I wasn't clear in the first comment, but this still won't get me any other messages. With events, OTOH, might be exactly what I need.
peacedog
+2  A: 

The number of rows affected is returned through an optional second argument of the Connnection object's Execute method

Dim num As Long    
sqlConn.Execute sqlStatement, num, adExecuteNoRecords     
MsgBox num & " records were affected"
MarkJ
Yes I really should have looked up the function on MSDN. Doh. Thanks for the suggestion, but that only gets me part of the way there (and will still be worthwhile if it is all I can do).
peacedog