Solved

Ado cmd.execute not working

Posted on 2001-08-29
3
603 Views
Last Modified: 2008-03-10
I have a stored procedure as follows:
CREATE PROCEDURE Test @cid as char(2) ,@fax as varchar(20) output as
select @fax = fax
from t1
where cid = @cid

The table T1 contains the following rows:
cid     fax
aa     100
cc     200
dd     <NULL>
bb     <NULL>
     
And here is the ado code:
Public Sub test()
Dim oconn As ADODB.Connection
Dim ors As ADODB.Recordset
Dim ocmd As ADODB.Command
Dim opar As ADODB.Parameter
Dim oIDs As New Collection
Dim oID As Variant
Dim myfax as variant

Set oconn = New ADODB.Connection
Set ocmd = New ADODB.Command

    oconn.Open (testconnstr)
    ocmd.ActiveConnection = oconn
    ocmd.CommandText = "Test"
    ocmd.CommandType = adCmdStoredProc
ocmd.Parameters.Refresh
oIDs.Add ("aa")
oIDs.Add ("qq")
oIDs.Add ("bb")
    For Each oID In oIDs
        ocmd.Parameters("@CID") = oID
        ocmd.Execute
        myfax = ocmd.Parameters("@FAX")
    Next
Set ocmd = Nothing
Set oconn = Nothing
End Sub

When oid = "qq", I want myfax to = null. It appears @FAX is not update and instead myfax = 100 when oid = qq. I can make some work arounds, but what is going on, how is this normally handled?
0
Comment
Question by:Paullkha
  • 2
3 Comments
 

Accepted Solution

by:
abhijitmathure earned 75 total points
ID: 6437474
As u have used the
ocmd.Parameters.Refresh to clear the cmd collection, u would have to do the same within the loop , so that it does not contain information of the first run , add the following line immediately after the

   For Each oID In oIDs
'/******
ocmd.Parameters.Refresh
'/*******
       ocmd.Parameters("@CID") = oID
       ocmd.Execute
       myfax = ocmd.Parameters("@FAX")
   Next
0
 
LVL 2

Author Comment

by:Paullkha
ID: 6437530
I need some help on .Refresh I think.
The below additional code does not give me the same problem as .Refresh does:
   Set opar = ocmd.CreateParameter("@cid", adChar, adParamInput, 2)
    ocmd.Parameters.Append opar
    Set opar = ocmd.CreateParameter("@FAX", adVarChar, adParamOutput, 20)
    ocmd.Parameters.Append opar
 
I though .Refresh does exactly like above; go to server, get parameters, append parameters, return.

Why do I have to .Refresh before each .Execute? Why does this problem occur only when  "empty" set returned.

0
 

Expert Comment

by:abhijitmathure
ID: 6437711
When you profile the processing of the stored proc on the database, would will notice the following, during the 3 loops that take place thru the VB code

a) declare @P1 varchar(20)
   set @P1='100'
   exec Test 'aa', @P1 output
   select @P1
b) declare @P1 varchar(20)
   set @P1='100'
   exec Test 'qq', @P1 output
   select @P1
c) declare @P1 varchar(20)
   set @P1='200'
   exec Test 'bb', @P1 output
   select @P1

Remember that the oCMD object is persistent in your VB code, when u first run the stored proc for 'aa' the VALUES are set in the oCMD object for all the 3 types defined in the stored procedures (3rd being the returntype)

The first run update the fax variable, which is used for the SET in the second run, within the database context. This SET is done, since the oCMD is now NOT REFRESHED to reflect the context of the database, which would be essential for updating the object reference in VB , which does not still have the info. from the database execution.

If you want to check the behaviour, keep the table in MS SQL 7 + , run profiler and keep the events for only stored procedures, you will notice the about 3 steps taking place, with the SET before the exec for the stored procedure.

Do note the as per the second part, where you do

  Set opar = ocmd.CreateParameter("@cid", adChar, adParamInput, 2)
 you are resetting the stored-procedure variables (the 3 as mentioned above)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question