在VBA中将记录集存储到阵列

友郎

我有一个动态函数来调用存储过程,并将记录集存储在一个数组中,我想在另一个子数组中使用它。但是我没有在这样的数组中得到结果:

Array(0,0) = 1
Array(0,1) = Miller
Array(1,0) = 2
Array(1,1) = Jones
Array(2,0) = 3
Array(2,1) = Jackson
....

我的数组结果是这样的:

Array(0,0) = 1
Array(1,0) = Miller
Array(0,1) = 2
Array(1,1) = Jones
Array(0,2) = 3
Array(1,2) = Jackson
....

为了了解该过程,我向您展示SQL-statement

CREATE PROCEDURE dbo.sp_GetAllPersons
AS
BEGIN
    SET NOCOUNT ON;

    SELECT DISTINCT u.ID, u.Name
    FROM dbo.v_Users u
END
GO

获取记录集并将其存储在数组中的函数:

Public Function fGetDataBySProc(ByVal sProcName As String) As Variant
    ...
    
    Dim Cmd As New ADODB.Command
    With Cmd
        .ActiveConnection = cn
        .CommandText = sProcName
        .CommandType = adCmdStoredProc
    End With
    
    Dim ObjRs As New ADODB.Recordset: Set ObjRs = Cmd.Execute
    Dim ArrData() As Variant
    
    If Not ObjRs.EOF Then
        ArrData = ObjRs.GetRows(ObjRs.RecordCount)
    End If
    
    ObjRs.Close
    cn.Close
    
    fGetDataBySProc = ArrData
End Function

该子函数被称为:

Public Sub cbFillPersons()
    Dim sProcString As String: sProcString = "dbo.sp_GetAllPersons"
    Dim ArrData As Variant: ArrData = fGetDataBySProc(sProcString)   
    Dim i as Integer
    
    ' Just for testing
    For i = LBound(ArrData) To UBound(ArrData)
        Debug.Print "AddItem: " & ArrData(0, i)
        Debug.Print "List: " & ArrData(1, i)
    Next
End Sub

我不知道我在做什么错。也许是.GetRows()-方法?

VBasic2008

转置二维数组

  • 您可以使用getTransposedArray函数转置结果数组

  • 然后,fGetDataBySProc函数的最后一行将是:

     fGetDataBySProc = getTransposedArray(ArrData) 
    

功能

Function getTransposedArray(Data As Variant) _
         As Variant
    
    Dim LB2 As Long
    LB2 = LBound(Data, 2)
    Dim UB2 As Long
    UB2 = UBound(Data, 2)
    
    Dim Result As Variant
    ReDim Result(LB2 To UB2, LBound(Data, 1) To UBound(Data, 1))
    
    Dim i As Long
    Dim j As Long
    
    For i = LBound(Data, 1) To UBound(Data, 1)
        For j = LB2 To UB2
            Result(j, i) = Data(i, j)
        Next j
    Next i
    
    getTransposedArray = Result
                 
End Function

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

VBA将记录集存储为整数变量

来自分类Dev

存储过程测试返回记录集

来自分类Dev

在VBA中合并MySQL记录集

来自分类Dev

Excel VBA-遍历记录集

来自分类Dev

为VBA记录集传递参数

来自分类Dev

VBA & Excel - 循环记录集

来自分类Dev

获取记录集值而不复制到工作表(Excel VBA)

来自分类Dev

访问列表的 VBA 集记录集不起作用

来自分类Dev

存储过程执行后,记录集关闭

来自分类Dev

如何从内部调用到存储过程获取记录集?

来自分类Dev

从C#中存储的proc返回多个记录集

来自分类Dev

如何从内部调用到存储过程获取记录集?

来自分类Dev

在存储过程中循环记录集

来自分类Dev

vba遍历记录集中的字段,而另一个记录集不是EOF

来自分类Dev

从ADODB设置表单的VBA记录集时出错

来自分类Dev

VBA调试-查看所有打开的DAO记录集

来自分类Dev

在VBA中深度复制或克隆ADODB记录集

来自分类Dev

从VBA,DoEvents中的Access处理“大”记录集

来自分类Dev

C#和VBA记录集之间的DAO差异

来自分类Dev

通过遍历记录集访问vba数组

来自分类Dev

VBA从SQL Server记录集获取价值

来自分类Dev

VBA:根据当前数据更新记录集

来自分类Dev

Access 2013-VBA-记录集插入获取ID

来自分类Dev

从ADODB设置表单的VBA记录集时出错

来自分类Dev

访问VBA-检查记录集时出错

来自分类Dev

代码跳过记录集输入,ADODB Excel VBA

来自分类Dev

MS Access 使用 vba 复制记录集

来自分类Dev

Access 2013错误绑定到ADO记录集

来自分类Dev

Access 2013错误绑定到ADO记录集