venmarces
asked on
Update multi tables in MS-Access via one SQL Code
Hello,
I have an old MS Access database that contains more than 100 tables, most of these tables are similar: so they have the same structure and the same number of columns with the same data-type.
Now, I want to make a SQL Script that allows me to open every table in this database and update a specific column values based on a JOIN on another table ... here is a simulation
For I=1 to 100
Open everytable
Update Table(i)
Set Column X = (SELECT Ref from ReferenceTable WHEN ColumnX.Value = Ref.Value)
I ++
Thank you
I have an old MS Access database that contains more than 100 tables, most of these tables are similar: so they have the same structure and the same number of columns with the same data-type.
Now, I want to make a SQL Script that allows me to open every table in this database and update a specific column values based on a JOIN on another table ... here is a simulation
For I=1 to 100
Open everytable
Update Table(i)
Set Column X = (SELECT Ref from ReferenceTable WHEN ColumnX.Value = Ref.Value)
I ++
Thank you
u can try:
Dim tbl As TableDef
For Each tbl In CurrentDb.TableDefs
SQL = "Update " & tbl.Name & " " & _
"Set Column X = (SELECT Ref from ReferenceTable WHEN ColumnX.Value = Ref.Value) "
CurrentDb.Execute SQL
Next
customize the looping logic and update SQL accordingly
ASKER
Thank you Ryan
as I said, not all of the tables in the database but a majority of thyem may have this script applied for them
what about having a list of tables .. how we can do that ?
as I said, not all of the tables in the database but a majority of thyem may have this script applied for them
what about having a list of tables .. how we can do that ?
>>not all of the tables in the database but a majority of thyem may have this script applied for them
Yup, so you gonna tell us what tables to be excluded or included, and also try to answer on Rey's questions
Yup, so you gonna tell us what tables to be excluded or included, and also try to answer on Rey's questions
ASKER
Rey,
Table(i).Manufacturer = FinalUnion.Manufacturer
Table(i).Manufacturer = FinalUnion.Manufacturer
@venmarces
pls answer the questions above..
here is the code to list all tables
Dim td As DAO.TableDef, db As DAO.Database, sql As String
Set db = CurrentDb
For Each td In db.TableDefs
If Not td.Name Like "Msys*" And Not td.Name = "ReferenceTable" Then
debug.print td.name
End If
Next
pls answer the questions above..
here is the code to list all tables
Dim td As DAO.TableDef, db As DAO.Database, sql As String
Set db = CurrentDb
For Each td In db.TableDefs
If Not td.Name Like "Msys*" And Not td.Name = "ReferenceTable" Then
debug.print td.name
End If
Next
try this codes, replace [NameOfFielfdHere] with the actual name of field you want to update.
Sub updateTables()
Dim td As DAO.TableDef, db As DAO.Database, sql As String
Set db = CurrentDb
For Each td In db.TableDefs
If Not td.Name Like "Msys*" And Not td.Name = "FinalUnion" Then
sql = "update [" & td.Name & "] Inner Join FinalUnion" _
& " On [" & td.Name & "].Manufacturer=FinalUnion .Manufactu rer" _
& " Set [" & td.Name & "].[NameOfFielfdHere]=Fina lUnion.[Na meOfFielfd Here]"
db.Execute sql, dbFailOnError
End If
Next
End Sub
Sub updateTables()
Dim td As DAO.TableDef, db As DAO.Database, sql As String
Set db = CurrentDb
For Each td In db.TableDefs
If Not td.Name Like "Msys*" And Not td.Name = "FinalUnion" Then
sql = "update [" & td.Name & "] Inner Join FinalUnion" _
& " On [" & td.Name & "].Manufacturer=FinalUnion
& " Set [" & td.Name & "].[NameOfFielfdHere]=Fina
db.Execute sql, dbFailOnError
End If
Next
End Sub
ASKER
is this a VBA -Code ? I don't have VB6 program ... how I can test this ?
If we use the TableDefs then I will use all the tables in the database. What about using a specific list of tables instead ? how this code may be changed to support it
thanks
If we use the TableDefs then I will use all the tables in the database. What about using a specific list of tables instead ? how this code may be changed to support it
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
getting error on
db.Execute SQL, dbFailOnError
Object Required
db.Execute SQL, dbFailOnError
Object Required
try the codes i posted
what is the name of the field in the 100 tables that correspond to the field name in the "ReferenceTable"?
what is the name of the "ReferenceTable"?
is the "ReferenceTable" in the same database?