명령과 함께 저장 프로 시저를 호출합니다. 결과를 레코드 세트로받습니다. 이제 계산을해야하므로 반환 된 레코드 수를 알아야합니다. RecordSet.RecordCount는 그것을 제공해야하지만 -1 만 반환합니다. MoveLast를 시도한 CursorType과 CursorLocation의 다른 값을 시도했습니다. 아무것도 도움이되지 않습니다.저장 프로 시저 및 명령을 사용할 때 VBA RecordSet.RecordCount = -1
레코드 수에 대한 고정 숫자를 가정하여 임시 해결책을 만들었습니다.
RecordCount에 관한 많은 스레드와 기사를 읽었지만 아무 것도 도움이되지 않는 것 같습니다. 명령을 사용하는 데 문제가 있습니까? 대부분의 예제에서는 명령을 사용하지 않습니다.
저는 경험있는 프로그래머이지만 SQL과 VBA에는 익숙하지 않습니다. 나에게 도움을 줄 수있는 사람은 누구야?
최고 감사합니다, 당신은 반환 된 레코드의 수는 아래 A5에서 차지하는 행의 수를 그 시점에서
Sheets("Sheet1").Range("A5").CopyFromRecordset RecordSet
그래서
을했습니다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