Tuesday, August 29, 2023

Create Array with key in VBA

 Cr.https://stackoverflow.com/questions/73786921/vba-collection-or-dictionary-with-an-array-and-key

Sub tettt()

Dim rsDetail

Dim sql As String

Dim myCol As Object

Dim bb

Set myCol = CreateObject("Scripting.dictionary")

Set db = CodeDb()

box1_code = "261603GF2ASERVP"

sql = "SELECT * FROM " & database_select & " where " & database_select & ".Barcode = '" & UCase(Mid(box1_code, 1, 17)) & "'"

    Set rsDetail = db.OpenRecordset(sql)

        bb = rsDetail.GetRows

    For i = 0 To rsDetail.Fields.Count - 1

        myCol.Add rsDetail.Fields(i).Name, bb(i, 0)  'first row only

    Next

    rsDetail.Close

    a = myCol("FG")

End Sub


**** case foreach

 For Each myVar In myCol.Keys     

    xxxx=myCol(myVar)

 Next myVar