I am tryting to update a field that appears in multiple tables within my DB. These tables all share an identical structure (which I gather is less than ideal DB design). The DB is a commercial product and I use MS Access Linked Tables to generate the reports I use. I cannot change the underlying structure.
So to query all of the data in these tables I first construct a temporary table (using the code I have pasted below)- this compiles all the data from seperate tables into one single table with an additional field that records the name of the parent table. I then query the temp table. The query will give me results similar to below:
Job ObjectID Status
J000300 ABC1 H
J000300 CAB1 H
J000297 BBB2 H
J000297 CCC2 H
J000295 DDD3 H
The Job field is actually the table from which the other data came - and is the name of the tables I wish to update. So for the query above I would like to update the status for records in tables J000300, J000297 and J000295 from H to B.
Changing the status for the records in the query could be done with an update query. However those changes would not cascade through to the parent tables (J000300, J000297, J000295).
I guess what I am looking for is a piece of code that will change records in the tables identified in the query. If that makes sense.
The code used to build the temp table is as follows:
Private Sub Command0_Click()
Dim db As Database
Dim rsRjobs As Recordset
Dim rsRapps As Recordset
Dim LengthofUnionSQL As Long
Dim sql As String
Dim UnionSQL As String
Set db = CurrentDb
Set rsRjobs = db.OpenRecordset("Select * from rjobs where Status = 'Live'", dbOpenSnapshot)
Do While Not rsRjobs.EOF
UnionSQL = UnionSQL & "Select ObjectID, SearchNo, DateSearched, Consultant, Status, """ & rsRjobs!JobID & """ AS Job from [" & rsRjobs!JobID & "] Union "
rsRjobs.MoveNext
Loop
'following two lines are to remove the trailing word Union from the string unionsql
LengthofUnionSQL = Len(UnionSQL)
UnionSQL = Mid(UnionSQL, 1, LengthofUnionSQL - 7)
' Now variable Unionsql will hold the value something like
' Select ObjectID, SearchNo, DateSearched, Consultant from J000145
' Union Select ObjectID, SearchNo, DateSearched, Consultant from J000146
' Union Select ObjectID, SearchNo, DateSearched, Consultant from J000147
MsgBox UnionSQL
Set db = CurrentDb
Dim rsUnionquery As Recordset
Dim rstemp As Recordset
Set rstemp = db.OpenRecordset("temp", dbOpenDynaset, dbSeeChanges)
Set rsUnionquery = db.OpenRecordset(UnionSQL)
Do While Not rsUnionquery.EOF
rstemp.AddNew
rstemp!ObjectID = rsUnionquery!ObjectID
rstemp!SearchNo = rsUnionquery!SearchNo
rstemp!DateSearched = rsUnionquery!DateSearched
rstemp!Consultant = rsUnionquery!Consultant
rstemp!Status = rsUnionquery!Status
rstemp!Job = rsUnionquery!Job
rstemp.Update
rsUnionquery.MoveNext
Loop
End Sub
So to query all of the data in these tables I first construct a temporary table (using the code I have pasted below)- this compiles all the data from seperate tables into one single table with an additional field that records the name of the parent table. I then query the temp table. The query will give me results similar to below:
Job ObjectID Status
J000300 ABC1 H
J000300 CAB1 H
J000297 BBB2 H
J000297 CCC2 H
J000295 DDD3 H
The Job field is actually the table from which the other data came - and is the name of the tables I wish to update. So for the query above I would like to update the status for records in tables J000300, J000297 and J000295 from H to B.
Changing the status for the records in the query could be done with an update query. However those changes would not cascade through to the parent tables (J000300, J000297, J000295).
I guess what I am looking for is a piece of code that will change records in the tables identified in the query. If that makes sense.
The code used to build the temp table is as follows:
Private Sub Command0_Click()
Dim db As Database
Dim rsRjobs As Recordset
Dim rsRapps As Recordset
Dim LengthofUnionSQL As Long
Dim sql As String
Dim UnionSQL As String
Set db = CurrentDb
Set rsRjobs = db.OpenRecordset("Select * from rjobs where Status = 'Live'", dbOpenSnapshot)
Do While Not rsRjobs.EOF
UnionSQL = UnionSQL & "Select ObjectID, SearchNo, DateSearched, Consultant, Status, """ & rsRjobs!JobID & """ AS Job from [" & rsRjobs!JobID & "] Union "
rsRjobs.MoveNext
Loop
'following two lines are to remove the trailing word Union from the string unionsql
LengthofUnionSQL = Len(UnionSQL)
UnionSQL = Mid(UnionSQL, 1, LengthofUnionSQL - 7)
' Now variable Unionsql will hold the value something like
' Select ObjectID, SearchNo, DateSearched, Consultant from J000145
' Union Select ObjectID, SearchNo, DateSearched, Consultant from J000146
' Union Select ObjectID, SearchNo, DateSearched, Consultant from J000147
MsgBox UnionSQL
Set db = CurrentDb
Dim rsUnionquery As Recordset
Dim rstemp As Recordset
Set rstemp = db.OpenRecordset("temp", dbOpenDynaset, dbSeeChanges)
Set rsUnionquery = db.OpenRecordset(UnionSQL)
Do While Not rsUnionquery.EOF
rstemp.AddNew
rstemp!ObjectID = rsUnionquery!ObjectID
rstemp!SearchNo = rsUnionquery!SearchNo
rstemp!DateSearched = rsUnionquery!DateSearched
rstemp!Consultant = rsUnionquery!Consultant
rstemp!Status = rsUnionquery!Status
rstemp!Job = rsUnionquery!Job
rstemp.Update
rsUnionquery.MoveNext
Loop
End Sub