“Object Invalid or no Longer Set” when using Variable to Reference Collection

前端 未结 1 1496
栀梦
栀梦 2021-01-07 10:43

In the process of answering this question, I wrote a simple function to test whether an MS Access table contained all fields in a supplied array:



        
相关标签:
1条回答
  • 2021-01-07 11:29

    The problem here is:

    CurrentDb creates a DAO.Database object of the currently open database. Your TableDef is a member of that.

    But since you're not storing that object, it gets closed and deallocated right after you copied the tabledef to an object, and with it the members will be deallocated too.

    Store the database object, and the members will also persist:

    Function ValidateFields(strTbl As String, arrReq As Variant) As Boolean
        Dim fld
        Dim fldTmp As Field
        Dim colFld As Fields
        Dim db As DAO.Database
        Set db = CurrentDb
        Set colFld = db.TableDefs(strTbl).Fields
        On Error GoTo err
        For Each fld In arrReq
            Set fldTmp = colFld(fld)
        Next fld
        ValidateFields = True
    err:
        Exit Function
    End Function
    
    0 讨论(0)
提交回复
热议问题