带计数器的动态变量支架

时间:2018-12-14 22:56:37

标签: ms-access access-vba office365

我在表单上有两个txt框和两个组合框。还有一个与临时表链接的子窗体,我希望在每次更改一个控件时都进行重建/过滤(在每个控件上更新后使用该子窗体来触发以下子窗体)

我收到运行时错误'91:未在行Items(i)=物件上设置对象变量或带有块变量

我不确定使用“(i)”是否可以与MS Access 365一起使用,或者尺寸不正确?

谢谢。

    Private Sub Lookupstuff()
    Dim i As Integer
    Dim Items(1 To 4) As Object

        sql = "DELETE * FROM tblTemp"
        CurrentDb.Execute sql
        i = 0
    FilterArray = Array(Me.txtNew, Me.cmbS, Me.cmbP, Me.txtSl)
    For Each Thing In FilterArray
        If Not IsNull(Thing) Then
            i = i + 1
            Items(i) = Thing <--Error is here. Items(i) is empty.
        End If
    Next

    If i = 0 Then
        Forms!frmNew.Requery
        Forms!frmNew.Refresh
    End If
    If i = 1 Then
        Filter = Items1
    End If
    If i = 2 Then
        Filter = Items1 & " AND " & Items2
    End If
    If i = 3 Then
        Filter = Items1 & " AND " & Items2 & " AND " & Items3
    End If
    If i = 4 Then
        Filter = Items1 & " AND " & Items2 & " AND " & Items3 & " AND " & Items4
    End If

    sql = "INSERT INTO tblTemp SELECT * FROM tblQ"
    If Not IsNull(Filter) Then
        sql = sql & " WHERE " & Filter
    End If
    CurrentDb.Execute sql
        Forms!frmNew.Requery
        Forms!frmNew.Refresh
    End Sub

1 个答案:

答案 0 :(得分:0)

由于您要分配对数组中对象的引用,因此必须使用Set,即:

Set Items(i) = Thing

此外,大概每个对Items1Items2等的引用实际上应该是Items(1)Items(2),以便访问在数组的这些索引处引用的对象。