在VBA阵列上调用LBound()或UBound()时,"下标超出范围"错误

use*_*600 2 ms-access vba access-vba

以下代码生成错误"下标超出范围",我不知道为什么.有人可以解释一下吗?

    Dim errorc As Integer
    Dim myarray() As Variant


    errorc = 1

    If Len(Me.txt_Listnum) = 0 Then
    ReDim Preserve myarray(errorc)
    myarray(errorc) = "Numer Listy"
    errorc = errorc + 1
    End If

    If Len(Me.cbo_ByWho) = 0 Then
    ReDim Preserve myarray(errorc)
    myarray(errorc) = "Wystawione przez"
    errorc = errorc + 1
    End If

    If Len(Me.cbo_ForWho) = 0 Then
    ReDim Preserve myarray(errorc)
    myarray(errorc) = "Wystawione na"
    errorc = errorc + 1
    End If

    For i = LBound(myarray) To UBound(myarray)
        msg = msg & myarray(i) & vbNewLine
    Next i

    If errorc > 0 Then
       MsgBox "da" & msg

    End If
Run Code Online (Sandbox Code Playgroud)

Gor*_*son 6

如果填充了所有表单控件,则代码将失败,因此myarray永远不会得到ReDim.对于单元化动态数组,

Dim myarray() As Variant
Run Code Online (Sandbox Code Playgroud)

(即,未随后调整大小的那个ReDim),调用LBound()UBound()打开它将失败并且"下标超出范围".