【问题标题】:Getting 'Type Mismatch' Error but program still works出现“类型不匹配”错误,但程序仍然有效
【发布时间】:2018-06-12 18:49:12
【问题描述】:

我正在尝试使用 VBAMS 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


    【解决方案1】:

    你的一个或多个字符串不是纯粹的整数。您是否尝试过循环并打印它们?您的原始字符串是否以分隔符结尾?这可能会给你一个空字符串作为数组的最后一个值,它不能转换为数字。

    【讨论】:

    • 最后是分号!非常感谢!我不敢相信我错过了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2013-01-31
    • 2016-01-23
    • 2015-06-21
    • 2020-06-11
    • 1970-01-01
    相关资源
    最近更新 更多