基于两个数组字段在VBA中创建数组

时间:2018-05-07 04:56:51

标签: arrays ms-access access-vba

我有几个表,其中查找字段指向第三个表中的相同数据,并且这两个字段都可以选择多个值。用户可以从每个表中选择一条记录,我需要能够在VBA中分离出重叠的值。

为此,我尝试在VBA中创建第三个数组,我将转储结果值,但我收到类型不匹配错误。我似乎无法找到这种情况发生的原因。代码,尽可能减少而不会失去它的意义如下:

Function SetEnabledColours()
'Indexes for arrays of available colours
Dim IndA As Long, IndG As Long, IndO As Long
'Arrays of available colour options
Dim AuthorCol, GenreCol, OverlapCol()

AuthorCol = DLookup("[AllowedColours]", "tblAuthor", "[Author]= '" & cmbAuthor & "'")
GenreCol = DLookup("[AllowedColours]", "tblGenre", "[Genre]= '" & cmbGenre & "'")

'Separate overlapped options
'Cycle through AuthorCol
For IndA = LBound(AuthorCol) To UBound(AuthorCol)

[然后我得到错误]

    'Check each GenreCol against this AuthorCol
    For IndG = LBound(GenreCol) To UBound(GenreCol)
        If GenreCol(IndG) = AuthorCol(IndA) Then
            'Add to OverlapCol(CountO)
            ReDim Preserve OverlapCol(IndO)
            OverlapCol(IndO) = GenreCol(IndG)
            IndO = IndO + 1
            'Skip over to next AuthorCol
            GoTo Escape1
        End If
    Next IndG
Escape1:
Next IndA

最初我将索引变暗为整数,但我已经意识到这是因为我认为数组是范围。我知道这种类型的数据存储为数组而不是范围。

Erik在这里的回答指向了阵列:Multi-select Lookup Field data to VBA

这就是我在重叠阵列创建的基础上:https://www.experts-exchange.com/questions/28530517/remove-array-element-in-access-vba.html

1 个答案:

答案 0 :(得分:1)

DLookup for multiple values字段返回一个字符串,其中包含以逗号(和空格)分隔的值列表,因此您应该使用Split函数将此字符串转换为数组:

AuthorCol = Split(DLookup("[AllowedColours]", "tblAuthor", "[Author]= 'd'"), ", ")
GenreCol = Split(DLookup("[AllowedColours]", "tblGenre", "[Genre]= '" & cmbGenre & "'"), ", ")