How to use user input to build table name in query

sumdumgai

Registered User.
Local time
Today, 15:49
Joined
Jul 19, 2007
Messages
453
Hello. Is there a way to solicit user input and use that input to create a table name in a query? For example, when the query asks for the parameter, the user enters for parameter 'year', "2021". The Access query then builds a table name as "Table " + year, resulting in a table name 'Table 2021'. The query would then use that name to insert records into that table.

Thanks.
 
I'd like to give the user an easy way to archive records. Records are added in bulk on a monthly basis to a production database. One of the fields in the record is 'Year_Month' (format yyyymm). When the user wants to archive last year's records, the query is executed. It asks the user for the yyyymm. User enters yyyymm. The query selects records from the production table using yyyymm, and appends the records to a new table 'Table yyyymm'. User can then go to the archive database and import the 'Table yyyymm'. A delete query would then be used to remove the archived records from the production table.

I could do this in VBA but just wondered if a simple query could be used.

Thanks.
 
I'm with uncle Gizmo. Stop telling us the details of what you want to execute and start telling us the purpose.

Why must records be 'archived'? Why can't this table just hold all your data?
 
Thanks for all the suggestions. No one is going to pay to redesign the database. It's been in use for years and serving its purpose. So, can I please get an answer to my original question. Can I form a query that creates a table name from user input and use that table name to update a table? For example, something like "INSERT INTO [Table " + [?] + "]"

Thanks.
 
So, can I please get an answer to my original question. Can I form a query that creates a table name from user input and use that table name to update a table? For example, something like "INSERT INTO [Table " + [?] + "]"
Hi. I sort of answered that question earlier. I said you can't do it that way. What you could do is use VBA instead.
 
So, can I please get an answer to my original question. Can I form a query that creates a table name from user input and use that table name to update a table? For example, something like "INSERT INTO [Table " + [?] + "]"
As stated you cannot do "dynamic sql" in access, but you can do it through vba. If this was SQL Server you could build this dynamically.

Does the user really need to tell it what table? I would think this happens automatically since the database should know which records go into which archive. As long as all archives are named consistently.
 
As stated you cannot do "dynamic sql" in access, but you can do it through vba. If this was SQL Server you could build this dynamically.

Does the user really need to tell it what table? I would think this happens automatically since the database should know which records go into which archive. As long as all archives are named consistently.
"the database should know which records go into which archive" - please explain. If a record has field value 202101 (Jan 2021), what name would the archive table have to have? I can rename the archive tables to whatever I want.
 
You said your table naming convention is Table_yyyymm
User can then go to the archive database and import the 'Table yyyymm'
so 202101 record would get archived in Table_202101. Or maybe I misunderstood. I am assuming you have a Front end back end/s set up where you are linked to all the archived tables.
 
You said your table naming convention is Table_yyyymm

so 202101 record would get archived in Table_202101. Or maybe I misunderstood. I am assuming you have a Front end back end/s set up where you are linked to all the archived tables.
Yes, the production database contains tables that are linked to tables in the archive database.
 
So, you are not using actual date fields? Instead, you are mushing two attributes into a single field?

In the archive form, specify the year, assuming you are archiving an entire year at a time, That will be used in the name of the new table and with the Left() function as selection criterial
Got it. Thanks. This is without VBA, right?
 
How about, instead of messing about with tables and queries and naming and renaming, you simply make a back up copy of the accdb and call it the "archive" for the previous year? Then in the currently active accdb, you can delete records not intended for current use going forward.
 
How about, instead of messing about with tables and queries and naming and renaming, you simply make a back up copy of the accdb and call it the "archive" for the previous year? Then in the currently active accdb, you can delete records not intended for current use going forward.
A bit kludgy but I could create say 10 empty tables, same design as production table, on the archive database. I think the archive db can hold about 10 years worth of records. I'll name these tables Archive2021, Archive202, etc. On the production db, I'll set up a select/append query that will ask for the year. The query selects all records for that year and appends them to an empty table, say 'Archive_Temp'. The user is then instructed to open the archive db and Import the 'Archive_Temp' table from the production db into the correct 'Archiveyyyy' table.
Not critical but is there a way, via table design, to prevent import into wrong table based on field 'Year_Month' value?
That would do me for 10 years.
 
Mechanically, you can do it in a couple of lines of VBA. You have some constant strings for the front and back part of the query (SELECT in the front, WHERE in the back). Then using perhaps a combo box with a short list of year numbers, concatenate

Code:
strSQL = "SELECT thisfield, thatfield, theother field, ... FROM " & tablenamestring & " WHERE DatePart( 'Y', datefield )  = " & CStr(  selectedyear ) & ";"
currentdb.Execute strSQL

 

Users who are viewing this thread

Back
Top Bottom