Search code examples
ms-access

How do I Store a table in Access to a Variable?


I want to store Table Customer to a variable Cust_tbl.

I used the following code but it gives error 3420 at Debug.Print "Table Name: " & tbl.Name. Please help

Dim tbl As TableDef
Dim tableName As String

Dim Db As Database
    Set Db = CurrentDb
' Replace "YourTableName" with the actual name of the table you want to assign
tableName = "Client"

' Get a reference to the table
Set tbl = CurrentDb.TableDefs("Client")

' Now you can use the "tbl" variable to work with the table properties and data
Debug.Print "Table Name: " & tbl.Name
Debug.Print "Number of Fields: " & tbl.Fields.Count
' ... (other operations)

' Release the reference to the table
Set tbl = Nothing

Solution

  • Use the variable Db instead of CurrentDb, because objects defined via CurrentDb are not permanent, leading to the error "Object invalid or no longer set".

    Set tbl = Db.TableDefs("Client")  'Db instead of CurrentDb