Variables as table names

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mathilin
    New Member
    • Aug 2010
    • 3

    #1

    Variables as table names

    Hello,
    I have a database that asks the user for many inputs and stores them in variables. From these variables a table name is created. Like so:

    Code:
    'Specify table name
        Set tbl = dbs.CreateTableDef(userCircuit & "-" & userStart & "," & userEnd)
    Where userCircuit, userStart, and userEnd are all variables. I am attempting to add information to this table that is created by this statement:

    Code:
     DoCmd.RunSQL ("INSERT INTO Table1 ([Field1])VALUES(Cable_Start)")
    isnstead of "Table1" I want the table to be the table I created with the first set of code. I attempted to replace "Table1" with "userCircui t & "-" & userStart & "," & userEnd" but I get a syntax error. I have attempted many things and could not figure this out. I am sure it is something incredibly simple being that I have never worked with VBA before, but I just cant point it. Thank You!

    My most current attempt I saved the 3 seperate variables into one string variable, so I end up with:
    Code:
     DoCmd.RunSQL ("INSERT INTO tableName([Field1])VALUES(Cable_Start)")
    so all that is left to be done is to figure out how to have it identifiy "tableName" as a variable instead of actually the word "tableName"
    Seth
  • Jerry Winston
    Recognized Expert New Member
    • Jun 2008
    • 145

    #2
    It's been awhile since I've touched VBA. Will this work?
    Code:
     Dim myCMDStr STRING
     myCmdStr = "INSERT INTO [" & userCircuit & "-" & userStart & "," & userEnd & "] ([Field1])VALUES(Cable_Start)"
     DoCmd.RunSQL (myCMDStr )

    Comment

    • mathilin
      New Member
      • Aug 2010
      • 3

      #3
      It seemed as though this worked! Thanks a million you saved me hours of aggravation. Just as a note to anyone else who may be encounting this problem I changed the above suggestion a bit just to work properly in my code. Thanks again!

      Code:
      Dim myCMDStr As String
         myCMDStr = "INSERT INTO [" & userCircuit & "-" & userStart & "," & userEnd & "] ([Cable_Start])VALUES(Cable_Start)"
         DoCmd.RunSQL (myCMDStr)

      Comment

      Working...