How to loop through hundreds of tables and build a new table.
How to loop through hundreds of tables and build a new table.
(OP)
Hello, I am a little behind with a project and could do with some help / pointers.
I am working with a database that has several thousand tables (yes I know that is a long way from normalized, but it is a professional program and one which I have no option to modify.
The tables are all named as numbers i.e. 1,2 3, etc....
I have a tables called Property, from which I can select all the tables of interest (around 500)
I need to look at every table, go to the last record and copy this record into a new table (so I will end up with a new table with 500 records - those being the last record of each of the individual tables).
I'm guessing I will need to do the following:
1. Create a new table which has fields matching the tables I am looking at (let say I csll it tbl_Master)
2. Open the first table
3 Go to the last record
4. Select the last record
5. Append this record to my tbl_Master
6. go to the next table etc...
I would much appreciate direction / methods / thoughts
Many thanks Mark
I am working with a database that has several thousand tables (yes I know that is a long way from normalized, but it is a professional program and one which I have no option to modify.
The tables are all named as numbers i.e. 1,2 3, etc....
I have a tables called Property, from which I can select all the tables of interest (around 500)
I need to look at every table, go to the last record and copy this record into a new table (so I will end up with a new table with 500 records - those being the last record of each of the individual tables).
I'm guessing I will need to do the following:
1. Create a new table which has fields matching the tables I am looking at (let say I csll it tbl_Master)
2. Open the first table
3 Go to the last record
4. Select the last record
5. Append this record to my tbl_Master
6. go to the next table etc...
I would much appreciate direction / methods / thoughts
Many thanks Mark
RE: How to loop through hundreds of tables and build a new table.
And when you say: "Go to the last record" - what constitutes "last record"?
Have fun.
---- Andy
A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
RE: How to loop through hundreds of tables and build a new table.
They have a unique ID called Reference (1 appears at the top...)
Many thanks Mark
RE: How to loop through hundreds of tables and build a new table.
I would use the query designer to create the base SQL statement and then use code to change the SQL of the query and run it.
Duane
Hook'D on Access
MS Access MVP
RE: How to loop through hundreds of tables and build a new table.
Select TOP 1 (*)
From 7
Order By ID Desc
and you said you have 500 tables named 1, 2, 3, ..., 499, 500
I would just do a simple loop like this:
CODE
Have fun.
---- Andy
A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
RE: How to loop through hundreds of tables and build a new table.
Duane
Hook'D on Access
MS Access MVP
RE: How to loop through hundreds of tables and build a new table.
Let's see what Mark says about it...
Have fun.
---- Andy
A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
RE: How to loop through hundreds of tables and build a new table.
I used the following code on a command button, but it gave me the error: runtime error 424, object required.
Any idea - thank you. (tables 1197 & 1198 exist)
CODE -->
RE: How to loop through hundreds of tables and build a new table.
Using the code below it works, but errors if the table does not exist. How could I get it to skip the table if it does not exist?
Many thanks Mark
CODE -->
RE: How to loop through hundreds of tables and build a new table.
Can you provide an accurate description of your requirements?
You can use some code that loop through all tables but I expect you have some extra tables that shouldn't be included. Are all of the table names numbers?
Duane
Hook'D on Access
MS Access MVP
RE: How to loop through hundreds of tables and build a new table.
I will try and describe in detail what I am trying to do:
I am wanting to build a table to show current arrears for a number of property accounts.
The database (which is a commercial one and NOT written by me) sets up a new table for each property account.
As the database is quite old, there are over 3000 tables in it (all identical). There are also other tables in the database such as tbl_Property etc...
All the account tables are named using a number (basically say 1-3000 for example - although there are a few gaps, probably where users have deleted a table).
Not all of the tables are of interest to me as I am only interested in active accounts.
I am able to determine the Active properties from the tbl_Property. The [Reference] field in tbl_Property is also the same number as the corresponding account table. For example record 100 in tbl_Property, will also have an account table called 100 (although if for some reason the account table 100 was missing I would like the code to not crash).
I therefore want to get some code that can:
1. Look at tbl_Property, see which are the active properties
2. Loop through the account tables of the active properties
3. Add the last record in each table to tbl_Account_Master (which would be identical to the account table, but additionally have an extra field to record the [reference] ID
4. Optionally it would be great if rather than just adding the last record I could also have the option to add the last record where the field [Type] (which exists in the accounts tables) could be defined. For example if I set criteria for [Type] to 'rent' my master tables would be populated with the last rent that each tenant had paid etc..
Many thanks as always. Best regards Mark
RE: How to loop through hundreds of tables and build a new table.
CODE --> vba
Wouldn't you want to store the Reference number in tbl_Master? Also, I would probably add a line of code to avoid duplicate values.
Duane
Hook'D on Access
MS Access MVP
RE: How to loop through hundreds of tables and build a new table.
I'm sorry for the delay in replying, but very much appreciate the help.
Best regards Mark