Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Ado cmd.execute not working

Posted on 2001-08-29
3
Medium Priority
?
637 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 300 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

916 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