Solved

Ado cmd.execute not working

Posted on 2001-08-29
3
595 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now