Results 1 to 4 of 4

Thread: Copy DB tables to a new DB with DAO?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Question Copy DB tables to a new DB with DAO?

    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?

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    I really need help with tihis....

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    *bump*

  4. #4
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose, Ca. - USA
    Posts
    302
    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:
    1. For Each daoTable In daoTableDefs
    2.     Set newTable = daoOutputDatabase.CreateTableDef(daoTable.Name)
    3.     newTable.ValidationRule = daoTable.ValidationRule
    4.     newTable.ValidationText = daoTable.ValidationText
    5.  
    6.     If ((daoTable.Attributes And dbAttachedODBC) Or (daoTable.Attributes And dbAttachedTable) Or (daoTable.Attributes And DB_ATTACHEDODBC) Or (daoTable.Attributes And DB_ATTACHEDTABLE)) Then
    7.         'LINKED TABLE
    8.         newTable.SourceTableName = daoTable.SourceTableName
    9.         newTable.Connect = daoTable.Connect
    10.     Else
    11.    
    12. CopyFields:
    13.         On Error Resume Next
    14.         For Each daoField In daoTable.Fields
    15.             Set newField = newTable.CreateField(daoField.Name, daoField.Type, daoField.Size)
    16.             newField.Attributes = daoField.Attributes
    17.             newField.AllowZeroLength = daoField.AllowZeroLength
    18.             newField.DefaultValue = daoField.DefaultValue
    19.             newField.Required = daoField.Required
    20.             newField.ValidationRule = daoField.ValidationRule
    21.             newField.ValidationText = daoField.ValidationText
    22.             If Err.Number <> 0 Then Err.Clear
    23.            
    24.             newTable.Fields.Append newField
    25.             If Err.Number <> 0 Then Err.Clear
    26.             Set newField = Nothing
    27.             Set daoField = Nothing
    28.         Next daoField
    29.        
    30. CopyIndexes:
    31.         For Each daoIndex In daoTable.Indexes
    32.             'Check for replication indices s_GUID, s_Generation
    33.             If InStr(1, daoIndex.Name, "s_", vbTextCompare) = 0 Then
    34.                 If daoIndex.Foreign = False Then
    35.                     Set newIndex = newTable.CreateIndex(daoIndex.Name)
    36.                     newIndex.Clustered = daoIndex.Clustered
    37.                     newIndex.IgnoreNulls = daoIndex.IgnoreNulls
    38.                     newIndex.Primary = daoIndex.Primary
    39.                     newIndex.Required = daoIndex.Required
    40.                     newIndex.Unique = daoIndex.Unique
    41.                                
    42.                     'Add Index field(s)
    43.                     For Each daoField In daoIndex.Fields
    44.                         Set newField = newIndex.CreateField(daoField.Name)
    45.                         newField.Attributes = daoField.Attributes
    46.                         newIndex.Fields.Append newField
    47.                         Set newField = Nothing
    48.                         Set daoField = Nothing
    49.                     Next daoField
    50.                     If Err.Number <> 0 Then Err.Clear
    51.                    
    52.                     newTable.Indexes.Append newIndex
    53.                     Set newIndex = Nothing
    54.                     Set daoIndex = Nothing
    55.                 End If
    56.             End If
    57.             If Err.Number <> 0 Then Err.Clear
    58.         Next daoIndex
    59.     End If
    60.    
    61. AppendTable:
    62.     daoOutputDatabase.TableDefs.Append newTable
    63.     If Err.Number <> 0 then
    64.         MsgBox "An error ocurred while appending a copied table-defintion for the : " & newTable.Name & vbNewLine & _
    65.             "Error Number : " & Err.Number & vbNewLine & _
    66.             "Error Description : " & Err.Description, vbCritical + vbOKOnly, newTable.Name & " was not copied correctly!"
    67.         Err.Clear
    68.     End If
    69.     On Error GoTo 0
    70.    
    71.     Set newTable = Nothing
    72.     Set daoTable = Nothing
    73.  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/
    Last edited by frigginjerk; Apr 9th, 2003 at 11:41 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width