I want to copy 4 tables of 20 from a DB to a new DB with DAO.
I also want Index to follow when I copy the tables.
In wich way can I do that?
Printable View
I want to copy 4 tables of 20 from a DB to a new DB with DAO.
I also want Index to follow when I copy the tables.
In wich way can I do that?
I really need help with tihis....
*bump*
This should get you off to a good start. I probably wont be around for a few days so your on your own from here.
VB Code:
For Each daoTable In daoTableDefs Set newTable = daoOutputDatabase.CreateTableDef(daoTable.Name) newTable.ValidationRule = daoTable.ValidationRule newTable.ValidationText = daoTable.ValidationText If ((daoTable.Attributes And dbAttachedODBC) Or (daoTable.Attributes And dbAttachedTable) Or (daoTable.Attributes And DB_ATTACHEDODBC) Or (daoTable.Attributes And DB_ATTACHEDTABLE)) Then 'LINKED TABLE newTable.SourceTableName = daoTable.SourceTableName newTable.Connect = daoTable.Connect Else CopyFields: On Error Resume Next For Each daoField In daoTable.Fields Set newField = newTable.CreateField(daoField.Name, daoField.Type, daoField.Size) newField.Attributes = daoField.Attributes newField.AllowZeroLength = daoField.AllowZeroLength newField.DefaultValue = daoField.DefaultValue newField.Required = daoField.Required newField.ValidationRule = daoField.ValidationRule newField.ValidationText = daoField.ValidationText If Err.Number <> 0 Then Err.Clear newTable.Fields.Append newField If Err.Number <> 0 Then Err.Clear Set newField = Nothing Set daoField = Nothing Next daoField CopyIndexes: For Each daoIndex In daoTable.Indexes 'Check for replication indices s_GUID, s_Generation If InStr(1, daoIndex.Name, "s_", vbTextCompare) = 0 Then If daoIndex.Foreign = False Then Set newIndex = newTable.CreateIndex(daoIndex.Name) newIndex.Clustered = daoIndex.Clustered newIndex.IgnoreNulls = daoIndex.IgnoreNulls newIndex.Primary = daoIndex.Primary newIndex.Required = daoIndex.Required newIndex.Unique = daoIndex.Unique 'Add Index field(s) For Each daoField In daoIndex.Fields Set newField = newIndex.CreateField(daoField.Name) newField.Attributes = daoField.Attributes newIndex.Fields.Append newField Set newField = Nothing Set daoField = Nothing Next daoField If Err.Number <> 0 Then Err.Clear newTable.Indexes.Append newIndex Set newIndex = Nothing Set daoIndex = Nothing End If End If If Err.Number <> 0 Then Err.Clear Next daoIndex End If AppendTable: daoOutputDatabase.TableDefs.Append newTable If Err.Number <> 0 then MsgBox "An error ocurred while appending a copied table-defintion for the : " & newTable.Name & vbNewLine & _ "Error Number : " & Err.Number & vbNewLine & _ "Error Description : " & Err.Description, vbCritical + vbOKOnly, newTable.Name & " was not copied correctly!" Err.Clear End If On Error GoTo 0 Set newTable = Nothing Set daoTable = Nothing Next daoTable
I also want to point out that some of this code is probably from gab2001uk's dao vs ado project. you can find it at:
http://www.gab2001uk.com/