Hi can andbody help me? Below is my code.
I get this message:
Run-time error '-2147217900(80040e14)':
Table 'PreampAutoDetect' already exists.
The error points to the line below with the stars around it.
stringSQL4 = ...
Please help me ...
Private Sub Command4_Click()
' Copy Tables from List1 to Database(s) in List2
Dim db As DAO.Database
Dim db2 As DAO.Database
Dim tdf As DAO.TableDef
Dim tdf2 As DAO.TableDef
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim cn As ADODB.Connection ' Connection to the source database
Dim Num_Of_Fields As Integer
Dim stringSQL As String
Dim stringSQL2 As String
Dim stringSQL3 As String
Dim stringSQL4 As String
' Open database with the standard tables
Set db = OpenDatabase(Text1.text)
' Opening the selected database(s) in List3
For Counter = 0 To List3.ListCount - 1
Text3.text = List3.List(Counter)
Set db2 = OpenDatabase(Text3.text)
' Iterating through the tables in List1
For i = 0 To db.TableDefs.Count - 1
Set tdf = db.TableDefs(i)
' Iterating through the tables in the database(s) in List2
For j = 0 To db2.TableDefs.Count - 1
Set tdf2 = db2.TableDefs(j)
' Filter all system tables
If ((tdf2.Attributes And &H2) = 0 And (tdf2.Attributes And &H80000000) = 0) Then
' Delete the tables from the database(s) in List2 that match the table names in List1
If (tdf2.Name = tdf.Name) Then
' SQL query to delete the tables
stringSQL = "DROP TABLE " & tdf2.Name
db2.Execute stringSQL
End If
End If
Next
Next
Next Counter
' Open connection to the source database
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Text1.text & ";Persist Security Info=False"
cn.Open
' Iterates through the tables in List1
For i = 0 To db.TableDefs.Count - 1
If ((db.TableDefs(i).Attributes And &H2) = 0 And (db.TableDefs(i).Attributes And &H80000000) = 0) Then
Set tdf = db.TableDefs(i)
Set rs = db.OpenRecordset(tdf.Name)
Num_Of_Fields = rs.Fields.Count ' Calculates number of fields
Text4.text = tdf.Name ' Stores the names of the tables in List1
Text5.text = tdf.Name
For j = 0 To List3.ListCount - 1
Text3.text = List3.List(j)
Set db2 = OpenDatabase(Text3.text)
' SQL query to create tables in DBs in List2 with names of tables in List1
stringSQL2 = "CREATE TABLE " & Text4.text
db2.Execute stringSQL2
' Adds the correct number of fields
For x = 0 To Num_Of_Fields - 1
' SQL query to add the fields from the tables in List1 to the tables in the DBs in List2
stringSQL3 = "ALTER TABLE " & Text4.text & " ADD COLUMN " & rs.Fields(x).Name & " TEXT(10); "
db2.Execute stringSQL3
Next
' Filter all the system tables
' SQL query to copy all data from the tables in List1 to the DBs in List2
For k = 0 To List2.ListCount - 1
For x = 0 To Num_Of_Fields - 1
If ((tdf.Attributes And &H2) = 0 And (tdf.Attributes And &H80000000) = 0) Then
**** stringSQL4 = "SELECT * INTO " & Text5.text & " FROM " & Text5.text & " IN '" & Text3.text & "';" ****
cn.Execute stringSQL4
End If
Next
Next
Next
End If
Next
' Close the connection and databases
cn.Close
Set cn = Nothing
db.Close
db2.Close
Thank you.