【发布时间】:2018-06-12 18:49:12
【问题描述】:
我正在尝试使用 VBA 在 MS Access 中设置多选列表框的值。我要选择的值的索引保存在一个名为 Index 的表中。我想从表中检索它们,拆分它们(因为它们用分号分隔),然后设置列表框值。这是我的代码:
Private Sub SetListBox()
Dim Indexes As String
Dim IndexArray() As String
Indexes = rsIndex!EIndex //Gets the semi-colon delimited value from 'Index' table
IndexArray = Split(Indexes, ";") //Array of strings holding the different indexes
ReDim IndexArrayLong(UBound(IArray)) As Long //Creating an array of Long with the same length as IndexArray
For i = 0 To UBound(IndexArray)
IndexArrayLong(i) = CLng(IndexArray(i)) //Converts the Strings into Longs and places them in Long Array
Me.listBox.Selected(IArrayL(i)) = True //Sets listbox values
Next
End Sub
经过一些研究,我发现 List Box 值的索引是 Long 数据类型,这就是我将 Strings 转换为 Longs 的原因。子成功地设置了列表框的值,但在它之前,它给了我
运行时错误:“13”类型不匹配
它突出显示了这行代码:
IndexArrayLong(i) = CLng(IndexArray(i))
我似乎无法弄清楚为什么我会收到错误或解决方案。任何建议表示赞赏。
【问题讨论】:
标签: ms-access vba runtime-error type-mismatch