none
VBA immediately dereferences Set variables

    Question

  • The last line in the following code throws Run-time error '3420': Object invalid or no longer set.

    Public Sub SetNullRefIssue()
        Dim tableName As String
        Dim myTableDef As TableDef
        Dim myFields As Fields
        tableName = "SomeTableName"
        Set myTableDef = CurrentDb.TableDefs(tableName)
        Set myFields1 = myTableDef.Fields ' Throws here: Object invalid or no longer set
        Set myFields2 = CurrentDb.TableDefs(tableName).Fields ' But this works
    End Sub
    

    Any ideas?

    • Edited by Calvin Craig Friday, February 09, 2018 11:09 PM Improve clarity
    Friday, February 09, 2018 11:05 PM

All replies