views:

36

answers:

3

Hello,

I have the following code that executes a Stored Procedure in Sybase throught VBA.

Sub GetInfo()

    Dim rstRUB As ADODB.Recordset
    Dim strRUB As String
    Dim strcnn As String
    Dim productType As String
    Dim DealId As String

    strcnn = "DSN=KONDOR_QUA;DATABASE=Kustom;UID=odbcuser;PWD=odbcuser123;"
    Set cnn = New ADODB.Connection
    cnn.Open strcnn

    Set rstRUB = New ADODB.Recordset

    Set ws = Workbooks("BODN_CPOTC.xls").Sheets("BODN_CPOTC")

    Dim row As Long
    row = 5

    While ws.Cells(row, 1) <> ""
        productType = ws.Cells(row, 1)
        DealId = ws.Cells(row, 3)



        Set cmd = New ADODB.Command
        With cmd
        .CommandText = "[Proc_BO_Deriv_AFOFLD]"
        .ActiveConnection = cnn
        .CommandType = adCmdStoredProc

        '.Parameters.Append .CreateParameter("@valueD", adDate, adParamInput, , strDate)
        '.Parameters.Append .CreateParameter("@maturityD", adDate, adParamInput, , Format$("20100504", "YYYYMMDD"))
        .Parameters.Append .CreateParameter("@tipoProduto", adVarChar, adParamInput, 9, productType)
        .Parameters.Append .CreateParameter("@dealId", adInteger, adParamInput, 0, DealId)
        End With


        rstRUB.Open cmd.Execute

        If rstRUB.EOF = False Then
            ws.Cells(row, 5) = rstRUB.Fields(0).Value
            ws.Cells(row, 6) = rstRUB.Fields(1).Value
            ws.Cells(row, 7) = rstRUB.Fields(2).Value
            ws.Cells(row, 8) = rstRUB.Fields(3).Value
            ws.Cells(row, 9) = rstRUB.Fields(4).Value
            ws.Cells(row, 10) = rstRUB.Fields(5).Value
            ws.Cells(row, 11) = rstRUB.Fields(6).Value
            ws.Cells(row, 12) = rstRUB.Fields(7).Value
            ws.Cells(row, 13) = rstRUB.Fields(8).Value
            ws.Cells(row, 14) = rstRUB.Fields(9).Value
            ws.Cells(row, 15) = rstRUB.Fields(10).Value
            ws.Cells(row, 16) = rstRUB.Fields(11).Value
            ws.Cells(row, 17) = rstRUB.Fields(12).Value
            ws.Cells(row, 18) = rstRUB.Fields(13).Value
            ws.Cells(row, 19) = rstRUB.Fields(14).Value
            ws.Cells(row, 20) = rstRUB.Fields(15).Value
            ws.Cells(row, 21) = rstRUB.Fields(16).Value
            ws.Cells(row, 22) = rstRUB.Fields(17).Value
            ws.Cells(row, 23) = rstRUB.Fields(18).Value
            ws.Cells(row, 24) = rstRUB.Fields(19).Value
            ws.Cells(row, 25) = rstRUB.Fields(20).Value
            ws.Cells(row, 26) = rstRUB.Fields(21).Value
            ws.Cells(row, 27) = rstRUB.Fields(22).Value
            ws.Cells(row, 28) = rstRUB.Fields(23).Value
            ws.Cells(row, 29) = rstRUB.Fields(24).Value
            ws.Cells(row, 30) = rstRUB.Fields(25).Value
            ws.Cells(row, 31) = rstRUB.Fields(26).Value
            ws.Cells(row, 32) = rstRUB.Fields(27).Value
        Else
            ws.Cells(row, 5) = "Deal Not Found"
        End If


        row = row + 1
    Wend


End Sub

The first Stored Procedure Execution works well, but when it goes for the second

rstRUB.Open cmd.Execute

the program does not end..

I allready switched arguments (the first ones with the second one) and the program won't end at the second time.

Do you know what am I doing wrong?

Thanks in advance!

+1  A: 

try set rstRUB = cmd.execute instead of rstRUB.Open cmd.Execute

bugtussle
that gives compile error
aF
+1  A: 

Not sure why Set rstRUB = cmd.Execute would give a compile error...

you can also try a call like this:

rstRUB.Open cmd

No need to call the cmd's execute method within the Open command, since the Open command triggers the query execution. Here is a link to the recordset open command: http://msdn.microsoft.com/en-us/library/ms675544(VS.85).aspx

Might also help to clear the memory of the cmd and rstRUB objects after you increment the row count to get ready for the next loop.

Set cmd = Nothing
Set rstRUB = Nothing
Fink
it's like this :) I have to open and "close" the connection every time
aF
A: 

I would rcommend that you set the timeout property for your command object:

http://msdn.microsoft.com/en-us/library/ms678265(v=VS.85).aspx

At least you'll get your application back.

I suspect that there is an error in your parameters - name, type, or the number of parameters you're passing; or worse, there's an error in the SQL. Unfortunately, some OLEDB database providers and their associated drivers do not implement error-handling very well, and I suspect that you'll get nothing from the Connection object's error collection, other than a 'Timeout' message.

So you're left with the task of capturing the SQL and the parameters collection, and pasting it into whatever SQL development window you've got for the database server: and it is very, very common for an error in the VBA's parameter set to be corrected invisibly during your attempts to run the SQL manually.

Nigel Heffernan