this is for an access 2016 db I have say 50 tables that have a total of say 100 unique columns so I can create a blank table with all 100 columns
so table 1 might have cols 1, 10, 99, table 2 might have cols 2, 15, 27, etc I would prefer to programmically iterate through the 50 tables in the db and as I read the records for each table dynamically associate the col name and its data so I can populate a 100 column target record columns with whatever column data there is in current table and write out all the records. naturally there will be many blank fields in the 100 col record. I know how to iterate through all tables and then get all the column names in each table, and get the table records data but can't figure out how to dynamically create the output records, actually not sure if it possible? If possible can somebody help with enough of a sample to get stated or links of course
see below for code I am attempting so far
Sub makeTable()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fldSource As DAO.Field
Dim fldTarget As DAO.Field
Dim rstTable As DAO.Recordset
Dim rstTargetTable As DAO.Recordset
Dim rstSourceTable As DAO.Recordset
Dim columnCount As Integer
Dim colname, sheetname As String
Set db = CurrentDb
'this is the table with universe all columns to populate from source tables
Set rstTargetTable = db.OpenRecordset("master")
For Each tdf In db.TableDefs
' iterate through all the potential source data tables
If (tdf.Name Like "someID*") Then
Set rstSourceTable = db.OpenRecordset(tdf.Name)
Do While Not rstSourceTable.EOF
rstSourceTable.Next
rstTargetTable.AddNew
For Each fldSource In rstSourceTable.Fields
For Each fldTarget In rstTargetTable.Fields
If fldSource.Name = fldTarget.Name Then
'??how to dynamically match target column with source column data
rstTargetTable!matchingColName = rstSourceTable!currColName.Value
End If
Next 'next field in target table record
Next 'field in current source table record
rstTargetTable.Update
End 'next record in source table
rstSourceTable.Close
Next 'next source table
End Sub
so table 1 might have cols 1, 10, 99, table 2 might have cols 2, 15, 27, etc I would prefer to programmically iterate through the 50 tables in the db and as I read the records for each table dynamically associate the col name and its data so I can populate a 100 column target record columns with whatever column data there is in current table and write out all the records. naturally there will be many blank fields in the 100 col record. I know how to iterate through all tables and then get all the column names in each table, and get the table records data but can't figure out how to dynamically create the output records, actually not sure if it possible? If possible can somebody help with enough of a sample to get stated or links of course
see below for code I am attempting so far
Sub makeTable()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fldSource As DAO.Field
Dim fldTarget As DAO.Field
Dim rstTable As DAO.Recordset
Dim rstTargetTable As DAO.Recordset
Dim rstSourceTable As DAO.Recordset
Dim columnCount As Integer
Dim colname, sheetname As String
Set db = CurrentDb
'this is the table with universe all columns to populate from source tables
Set rstTargetTable = db.OpenRecordset("master")
For Each tdf In db.TableDefs
' iterate through all the potential source data tables
If (tdf.Name Like "someID*") Then
Set rstSourceTable = db.OpenRecordset(tdf.Name)
Do While Not rstSourceTable.EOF
rstSourceTable.Next
rstTargetTable.AddNew
For Each fldSource In rstSourceTable.Fields
For Each fldTarget In rstTargetTable.Fields
If fldSource.Name = fldTarget.Name Then
'??how to dynamically match target column with source column data
rstTargetTable!matchingColName = rstSourceTable!currColName.Value
End If
Next 'next field in target table record
Next 'field in current source table record
rstTargetTable.Update
End 'next record in source table
rstSourceTable.Close
Next 'next source table
End Sub