【问题标题】:VBA RecordSet.RecordCount = -1 when using stored procedure and commandVBA RecordSet.RecordCount = -1 使用存储过程和命令时
【发布时间】:2018-05-23 21:28:18
【问题描述】:

我用命令调用存储过程。我在记录集中收到结果。现在我需要做一些计算,因此需要知道返回的记录数。 RecordSet.RecordCount 应该给我,但只返回-1。我尝试了 CursorType 和 CursorLocation 的不同值,尝试了 MoveLast。没有什么帮助。

我通过假设记录数的固定数字来做了一个临时解决方案。

这里有很多关于 RecordCount 的主题和文章,我阅读了但似乎没有任何帮助。是我使用命令的问题吗?我看到的大多数示例都不使用命令。

我是一位经验丰富的程序员,但不太习惯 SQL 和 VBA。谁能给我一些帮助?

最好的问候, 沃尔夫冈

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

【问题讨论】:

    标签: sql vba stored-procedures command recordset


    【解决方案1】:

    你已经完成了

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

    所以此时返回的记录数是从a5向下占用的行数:

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

    加四是因为您从第 5 行开始

    【讨论】:

      猜你喜欢
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      • 1970-01-01
      • 2012-03-31
      • 2019-07-07
      • 1970-01-01
      相关资源
      最近更新 更多