简体   繁体   English

使用存储过程和命令时,VBA RecordSet.RecordCount = -1

[英]VBA RecordSet.RecordCount = -1 when using stored procedure and command

I call a stored procedure with a command. 我用命令调用存储过程。 I receive the result in a record set. 我收到记录集中的结果。 Now I need to do some calculations and therefore need to know number of records that was returned. 现在,我需要进行一些计算,因此需要知道返回的记录数。 RecordSet.RecordCount should give me that but only returns -1. RecordSet.RecordCount应该给我,但只返回-1。 I have tried different values of CursorType and CursorLocation, tried MoveLast. 我尝试过使用CursorType和CursorLocation的不同值,尝试使用MoveLast。 Nothing helps. 没有任何帮助。

I made a temporary solution by assuming a fixed number for the number of records. 我通过为记录数假设一个固定的数字来制定了一个临时解决方案。

There are many threads here about RecordCount, and also articles, that I read but nothing seems to help. 这里有许多有关RecordCount的主题以及我读过的文章,但似乎无济于事。 Is the problem that I use a command? 是我使用命令的问题吗? Most examples I see dont use commands. 我看到的大多数示例都不使用命令。

I am an experienced programmer but not so used to SQL and VBA. 我是一位经验丰富的程序员,但不习惯使用SQL和VBA。 Anyone who can give me some help? 有人可以给我些帮助吗?

Best Regards, Wolfgang 最好的问候,沃尔夫冈

Sub CallStoredProcedure()

Dim Conn As ADODB.Connection, RecordSet As ADODB.RecordSet

Dim Command As ADODB.Command
Dim ConnectionString As String, StoredProcName As String
Dim LoginID As ADODB.Parameter, Entity_Id As ADODB.Parameter

Application.ScreenUpdating = False

Set Conn = New ADODB.Connection
Set RecordSet = New ADODB.RecordSet
Set Command = New ADODB.Command

'w
RecordSet.CursorType = adOpenStatic
RecordSet.CursorLocation = adUseClient


ConnectionString = "Provider=SQLOLEDB;Data Source=HIQARBL218\SQLEXPRESS;Initial Catalog=SweSalaryStore;Trusted_Connection=yes;"

On Error GoTo CloseConnection

Conn.Open ConnectionString

StoredProcName = "dbo.getInfoFromSQLDB"

With Command
    .ActiveConnection = Conn
    .CommandType = adCmdStoredProc
    .CommandText = StoredProcName
End With

Set EMPNR = Command.CreateParameter("@EMPNR", adVarChar, adParamInput, 100, "107")
Command.Parameters.Append EMPNR

Set PERNR = Command.CreateParameter("@PERNR", adVarChar, adParamInput, 100, "1111110008")
Command.Parameters.Append PERNR

Set CMPNR = Command.CreateParameter("@CMPNR", adVarChar, adParamInput, 100, "5612")
Command.Parameters.Append CMPNR

Set PERIODFROM = Command.CreateParameter("@PERIODFROM", adVarChar, adParamInput, 100, "1001")
Command.Parameters.Append PERIODFROM

Set PERIODTO = Command.CreateParameter("@PERIODTO", adVarChar, adParamInput, 100, "1701")
Command.Parameters.Append PERIODTO

Set RecordSet = Command.Execute


Sheets("Sheet1").Range("A5").CopyFromRecordset RecordSet


'loop to get column headings.
For i = 0 To RecordSet.Fields.Count - 1
    Sheets("Sheet1").Cells(1, i + 1).Value = RecordSet.Fields(i).Name
Next i


'Calculate last row in RecordSet
Dim lastRow As Integer
lastRow = 2000
'lastRow = RecordSet.RecordCount 'NOT WORKING!!!

'Average OB
Dim AvgOB As Double
AvgOB = Excel.WorksheetFunction.Average(Range(Sheets("Sheet1").Cells(5, 2), Sheets("Sheet1").Cells(lastRow, 2)))
AvgOB = Excel.WorksheetFunction.Round(AvgOB, 2)
Sheets("Sheet1").Cells(2, 2).Value = AvgOB
Sheets("Sheet1").Cells(3, 2).Value = AvgOB * 12


RecordSet.Close
Conn.Close
On Error GoTo 0
Application.ScreenUpdating = True
Exit Sub

CloseConnection:
    Application.ScreenUpdating = True
    MsgBox "SQL Stored Procedure Did Not Execute Sucessfully!", vbCritical, "SQL Error"
    Conn.Close

End Sub

You've done 你做完了

 Sheets("Sheet1").Range("A5").CopyFromRecordset RecordSet  

so at that point the number of records returned is the number of rows occupied from a5 down: 因此,此时返回的记录数是从a5向下占用的行数:

 Dim lastrow as long  'not integer!
 lastrow = Sheets("Sheet1").Range("A5").currentregion.rows.count + 4

the plus four is since you start at row 5 加四是因为您从第5行开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM