ilitirit
ilitirit

Reputation: 16352

How do I concatenate two multi-dimensional arrays in VB?

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).

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

Upvotes: 0

Views: 4826

Answers (3)

Fred
Fred

Reputation: 1

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

Upvotes: 0

ilitirit
ilitirit

Reputation: 16352

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           

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 416053

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)

Upvotes: 1

Related Questions