Results 1 to 3 of 3

Thread: join two Data bases

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2002
    Location
    newcastle
    Posts
    60

    join two Data bases

    i am trying to join two data bases by primary key with vb to produce one data base
    any ideas
    thanks

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    not sure exactly what you want to do, but I think it is something like this...

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim cat As ADOX.Catalog
    3.     Dim tbl As ADOX.Table
    4.    
    5.     Dim sDBFrom As String
    6.     Dim sDBTo As String
    7.     Dim sql As String
    8.     Dim cnn As New ADODB.Connection
    9.    
    10.     sDBFrom = "D:\TEST\From.mdb"
    11.     sDBTo = "D:\TEST\To.mdb"
    12.    
    13.    
    14.     'check to see if the database exist. if not create it.
    15.     If Dir(sDBTo) = "" Then
    16.         'database does not exist, make the database
    17.         Set cat = New ADOX.Catalog
    18.         cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sDBTo
    19.         Set cat = Nothing
    20.     End If
    21.    
    22.     'setup db connection
    23.     cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sDBFrom
    24.     Set cat = New ADOX.Catalog
    25.     Set cat.ActiveConnection = cnn
    26.  
    27.     'copy all the tables using SQL and Execute
    28.     For Each tbl In cat.Tables
    29.         Debug.Print tbl.Name & "  :  " & tbl.Type
    30.         'Make sure this is a table and not a query / view
    31.         If UCase(tbl.Type) = "TABLE" Then
    32.             'make sure that you do not try to create the system tables,
    33.             'cause they will already be in the db
    34.             If UCase(Left(tbl.Name, 4)) <> "MSYS" Then
    35.                 sql = "SELECT * INTO " & tbl.Name & " IN '" & sDBTo & "' FROM " & tbl.Name
    36.                 cnn.Execute sql
    37.             End If
    38.         End If
    39.     Next tbl
    40.    
    41.     'close the connection
    42.     Set cat = Nothing
    43.     Set cnn = Nothing
    44.     MsgBox "Import finished", vbInformation
    45. End Sub

    this sample copies all tables from one db to another...
    -= a peet post =-

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    if you use DAO

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim db As Database
    3.     Dim sDBFrom As String
    4.     Dim sDBTo As String
    5.     Dim td As TableDef
    6.     Dim sql As String
    7.    
    8.     sDBFrom = "C:\TEST\From.mdb"
    9.     sDBTo = "C:\TEST\To2.mdb"
    10.    
    11.    
    12.     'check to see if the database exist. if not create it.
    13.     If Dir(sDBTo) = "" Then
    14.         'database does not exist, make the database
    15.         Set db = CreateDatabase(sDBTo, dbLangGeneral, dbVersion40)
    16.         db.Close
    17.     End If
    18.    
    19.    'open the db where you want to get the data from
    20.     Set db = OpenDatabase(sDBFrom)
    21.  
    22.     'copy all the tables using SQL and Execute
    23.     For Each td In db.TableDefs
    24.         'make sure that you do not try to create the system tables,
    25.         'cause they will already be in the db
    26.         If UCase(Left(td.Name, 4)) <> "MSYS" Then
    27.             sql = "SELECT * INTO " & td.Name & " IN '" & sDBTo & "' FROM " & td.Name
    28.             db.Execute sql
    29.         End If
    30.     Next td
    31.    
    32.     'close the db
    33.     db.Close
    34. End Sub
    -= a peet post =-

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