如何在VB中连接两个多维数组?

时间:2021-11-19 21:33:43

I've got this code:

我有这个代码:

rs1 = getResults(sSQL1)
rs2 = getResults(sSQL2)

rs1 and rs2 and 2D arrays. The first index represents the number of columns (static) and the second index represents the number of rows (dynamic).

rs1和rs2以及2D数组。第一个索引表示列数(静态),第二个索引表示行数(动态)。

I need to join the two arrays and store them in rs3. I don't know what type rs1 and rs2 are though.

我需要加入两个数组并将它们存储在rs3中。我不知道rs1和rs2是什么类型的。

3 个解决方案

#1


1  

Are you sure that the columns will match up? Because if that's not the case I don't know how you'd do it in a generic way in any language. If it is the case, then you could probably do it very simply like this:

你确定列会匹配吗?因为如果不是这样的话,我不知道你是如何用任何语言的通用方式来做的。如果是这种情况,那么你可能会非常简单地这样做:

rs1 = getResults(sSQL1 & " UNION " sSQL2)

#2


1  

I've figured it out. Turns out I was doing it the right way all along, I was just off by one. You don't need a third array either.

我已经弄清楚了。事实证明我一直在以正确的方式做到这一点,我只是一个接一个。您也不需要第三个数组。

        aRS_RU = rowsQuery(sSQL & ", 'RU'")
        aRS_KR = rowsQuery(sSQL & ", 'KR'")

        uboundRU1 = UBound(aRS_RU, 1)
        uboundRU2 = UBound(aRS_RU, 2)
        uboundKR2 = Ubound(aRS_KR, 2)

        ' Redim original array
        ReDim Preserve aRS_RU(uboundRU1, uboundRU2 + uboundKR2 + 1 )
        uboundRU2 = UBound(aRS_RU, 2)

        ' Add the values from the second array            
        For m = LBound(aRS_KR, 1) To UBound(aRS_KR, 1)      'Loop for 1st dimension
            For n = LBound(aRS_KR, 2) To UBound(aRS_KR, 2)  'Loop for 2nd dimension
                aRS_RU(m, uboundRU2 + n) = aRS_KR(m,n)
            Next
        Next           

#3


0  

I know this post is old, but I adapted the code to fix some errors I had during its execution. The following code sample works for me:

我知道这篇文章很老,但我修改了代码来修复我在执行过程中遇到的一些错误。以下代码示例适用于我:

Sub ConcatRecordSets(ByRef avFirstRS As Variant, ByRef avSecondRS As Variant)

    Dim lIndex1 As Long, lIndex2 As Long
    Dim lFirstRSSize As Long, lSecondRSSize As Long

    ' Redim original array
    lFirstRSSize = UBound(avFirstRS, 2) - LBound(avFirstRS, 2) + 1
    lSecondRSSize = UBound(avSecondRS, 2) - LBound(avSecondRS, 2) + 1
    ReDim Preserve avFirstRS(LBound(avFirstRS, 1) To UBound(avFirstRS, 1), LBound(avFirstRS, 2) To UBound(avFirstRS, 2) + lSecondRSSize)

    ' Add the values from the second array
    For lIndex1 = LBound(avSecondRS, 1) To UBound(avSecondRS, 1)      ' Loop for 1st dimension
        For lIndex2 = LBound(avSecondRS, 2) To UBound(avSecondRS, 2)  ' Loop for 2nd dimension
            avFirstRS(lIndex1, lFirstRSSize + lIndex2) = avSecondRS(lIndex1, lIndex2)
        Next lIndex2
    Next lIndex1

End Sub

#1


1  

Are you sure that the columns will match up? Because if that's not the case I don't know how you'd do it in a generic way in any language. If it is the case, then you could probably do it very simply like this:

你确定列会匹配吗?因为如果不是这样的话,我不知道你是如何用任何语言的通用方式来做的。如果是这种情况,那么你可能会非常简单地这样做:

rs1 = getResults(sSQL1 & " UNION " sSQL2)

#2


1  

I've figured it out. Turns out I was doing it the right way all along, I was just off by one. You don't need a third array either.

我已经弄清楚了。事实证明我一直在以正确的方式做到这一点,我只是一个接一个。您也不需要第三个数组。

        aRS_RU = rowsQuery(sSQL & ", 'RU'")
        aRS_KR = rowsQuery(sSQL & ", 'KR'")

        uboundRU1 = UBound(aRS_RU, 1)
        uboundRU2 = UBound(aRS_RU, 2)
        uboundKR2 = Ubound(aRS_KR, 2)

        ' Redim original array
        ReDim Preserve aRS_RU(uboundRU1, uboundRU2 + uboundKR2 + 1 )
        uboundRU2 = UBound(aRS_RU, 2)

        ' Add the values from the second array            
        For m = LBound(aRS_KR, 1) To UBound(aRS_KR, 1)      'Loop for 1st dimension
            For n = LBound(aRS_KR, 2) To UBound(aRS_KR, 2)  'Loop for 2nd dimension
                aRS_RU(m, uboundRU2 + n) = aRS_KR(m,n)
            Next
        Next           

#3


0  

I know this post is old, but I adapted the code to fix some errors I had during its execution. The following code sample works for me:

我知道这篇文章很老,但我修改了代码来修复我在执行过程中遇到的一些错误。以下代码示例适用于我:

Sub ConcatRecordSets(ByRef avFirstRS As Variant, ByRef avSecondRS As Variant)

    Dim lIndex1 As Long, lIndex2 As Long
    Dim lFirstRSSize As Long, lSecondRSSize As Long

    ' Redim original array
    lFirstRSSize = UBound(avFirstRS, 2) - LBound(avFirstRS, 2) + 1
    lSecondRSSize = UBound(avSecondRS, 2) - LBound(avSecondRS, 2) + 1
    ReDim Preserve avFirstRS(LBound(avFirstRS, 1) To UBound(avFirstRS, 1), LBound(avFirstRS, 2) To UBound(avFirstRS, 2) + lSecondRSSize)

    ' Add the values from the second array
    For lIndex1 = LBound(avSecondRS, 1) To UBound(avSecondRS, 1)      ' Loop for 1st dimension
        For lIndex2 = LBound(avSecondRS, 2) To UBound(avSecondRS, 2)  ' Loop for 2nd dimension
            avFirstRS(lIndex1, lFirstRSSize + lIndex2) = avSecondRS(lIndex1, lIndex2)
        Next lIndex2
    Next lIndex1

End Sub