我是新手访问和SQL。尝试将记录集中的数据转换为数组(Excel VBA),以便我可以处理数据而不会将数据粘贴到电子表格中。
有些研究和大多数网站都提到过GetRows功能会起作用,但我得到了运行时错误'3021'。有关如何解决此问题的任何建议,好吗?谢谢!
Dim conConnect As Object
Dim cmdCommand As Object
Dim rstRecordSet As Object
Dim DBPath, StrCon As String
Dim tblarray As Variant
Set conConnect = CreateObject("ADODB.Connection")
Set cmdCommand = CreateObject("ADODB.Command")
Set rstRecordSet = CreateObject("ADODB.Recordset")
DBPath = "C:\Users\Documents\DbMhours.accdb"
conConnect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBPath & _
";Mode=Read|Write"
conConnect.CursorLocation = adUseClient
conConnect.Open
With cmdCommand
.ActiveConnection = conConnect
.CommandText = "SELECT * FROM tblOverallStats;"
.CommandType = adCmdText
End With
With rstRecordSet
.CursorType = adOpenStatic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open cmdCommand
End With
'Call StatsSum function in module
tblarray = StatsSum (rstRecordSet)
rstRecordSet.Close
conConnect.Close
Function StatsSum(Summary As Object) As Variant 'Input to be recordset from workbook
tblarray = Summary.GetRows
End Function
答案 0 :(得分:1)
你的功能错了 - 应该是:
Function StatsSum(Summary As Object) As Variant 'Input to be recordset from workbook
StatsSum = Summary.GetRows
End Function
您还应该首先使用.EOF
答案 1 :(得分:0)
在自己解决这个问题之后,我意识到最简单的方法可能就是蛮力:
With rstRecordset
.MoveLast
.MoveFirst
For i = 1 To .RecordCount
for j = 1 to .Fields.Count
tblArray(i,j) = .Fields(j).Value
Next j
.MoveNext
Next i
End With