Private Sub Form_Load()
Dim strLocationSource As String
Dim strLocationTarget As String
Dim myField
'Put your user input source file into a variable (I'm hardcoding the location
strLocationSource = "C:\DATA\Databases\SiteAccess.mdb"
With Adodc1
'Assuming a Microsoft Access provider
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
strLocationSource & ";Persist Security Info=False"
'Setting CommandType to a table
.CommandType = adCmdTable
End With
'Put your user input target file into a variable (I'm hardcoding the location
strLocationTarget = "C:\DATA\Databases\TestSites.mdb"
With Adodc2
'Assuming a Microsoft Access provider
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
strLocationTarget & ";Persist Security Info=False"
'Setting CommandType to a table
.CommandType = adCmdTable
End With
End Sub
Private Sub cmdTransferData_Click()
Dim MyObject
Dim x As Integer
Dim y As Integer
Dim NumberOfTables As Integer
'Sets up error handling in case a defined Source table doesn't exist so _
it can skip that one
On Error GoTo TableNotFound
NumberOfTables = 2 'This would be the maximum number of Tables you would load
'I couldn't find a way to do a For Each loop on a Tables collection,
'hence the For loop with the hard-coded number of tables from above
For y = 1 To NumberOfTables
Select Case y
Case 1
'List first source table name & corresponding target table name
Adodc1.RecordSource = "Sites"
Adodc2.RecordSource = "TestSitesTable"
Case 2
'List second source table name & corresponding target table name
Adodc1.RecordSource = "TableDoesntExist"
Adodc2.RecordSource = "TestSitesBogus"
Case 3
'Change the recordsource to the names your next set of tables
'Repeat this as many times as necessary
End Select
'These statements open the recordsource(s) based on above table names
Adodc1.Refresh
Adodc2.Refresh
'Walk through each record in source table recordset until EOF
Do Until Adodc1.Recordset.EOF
'Issues AddNew method on target table recordset in order to enter new data
Adodc2.Recordset.AddNew
'Assuming source & target tables have same table structures
'Fields are indexed from 0
For x = 0 To Adodc1.Recordset.Fields.Count - 1
'Writes value of each source field into corresponding target field
Adodc2.Recordset.Fields(x) = Adodc1.Recordset.Fields(x)
Next x
'Issues Update method on Target recordset to write record
Adodc2.Recordset.Update
'Moves to next Source record
Adodc1.Recordset.MoveNext
Loop
Adodc2.Refresh
DoNextTable:
Next y
Exit Sub
TableNotFound:
Select Case Err
'Traps the error for the non-existing table
Case -2147217865
Resume DoNextTable
End Select
End Sub