Results 1 to 4 of 4
  1. #1
    AbdullahGH is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2018
    Posts
    6

    use variable instead of table name

    Hi,,

    In Access VBA, I have many tables and instead to write same code for each table, I want to use Variable name for those Tables and useing loop.
    Below statements not accepted and give error "Object required", The original tables object defined and accepted.

    Set Table_Temp = db.OpenRecordset("First_Table_Name")
    or
    Set Table_Temp = db.TableDef("First_Table_Name")

    In Excel VBA, it is working fine like below:



    Set Table_Temp = Workbooks("First_Table_Name.xlsm"

    Set Sheet_Temp = Temp.Sheets("First_Sheet_Name")

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    See if this will help. tblStreets is an existing table.
    Code:
    Public Sub test()
        Dim db As DAO.Database
        Dim table_temp As DAO.TableDef
        Set db = CurrentDb
        Set table_temp = db.TableDefs("tblStreets")
        Debug.Print table_temp.RecordCount       'verify that it works
    End Sub
    Or if you want the table name passed as a parameter:
    Code:
    Public Sub subTest(tblArg As String)
        Dim db As DAO.Database
        Dim table_temp As DAO.TableDef
        Set db = CurrentDb
        Set table_temp = db.TableDefs(tblArg)
        Debug.Print table_temp.RecordCount
    End Sub
    Last edited by davegri; 09-15-2018 at 10:53 AM. Reason: more options

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    Also...
    Code:
    Public Sub subTest(tblArg As String)
        Msgbox  CurrentDb.TableDefs(tblArg).RecordCount
    End Sub

  4. #4
    John_G is offline VIP
    Windows 10 Access 2016
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Below statements not accepted and give error "Object required",
    Set Table_Temp = db.OpenRecordset("First_Table_Name")
    You are probably getting that error message because Access doesn't know what the variable db is, or what Table_Temp is.

    You can fix this in two ways. The first way is to add these lines to your code:

    Dim db as database
    Dim Table_temp as Recordset
    set db = currentdb


    The second way is to use

    Dim Table_temp as Recordset

    as before, then change
    Set Table_Temp = db.OpenRecordset("First_Table_Name")
    to
    Set Table_Temp = Currentdb.OpenRecordset("First_Table_Name")

    Also, if First_Table_Name is a string variable containing a table name, use it without the quotation marks:

    Set Table_Temp = Currentdb.OpenRecordset(First_Table_Name)

    because it is already a string.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How can use variable instead of table name?
    By AbdullahGH in forum Access
    Replies: 4
    Last Post: 07-08-2018, 08:35 PM
  2. store table value in a variable
    By chromachem in forum Programming
    Replies: 3
    Last Post: 02-17-2016, 01:38 PM
  3. Table with variable data
    By kcollop in forum Database Design
    Replies: 14
    Last Post: 07-20-2012, 01:28 PM
  4. Define Table with a Variable
    By Bedsingar in forum Queries
    Replies: 3
    Last Post: 07-18-2011, 07:07 AM
  5. SELECT INTO variable table name
    By Ian P in forum Queries
    Replies: 2
    Last Post: 05-29-2010, 12:49 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums