Results 1 to 14 of 14

Thread: Importing Access tables -- Solution found - Thanks

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    78

    Question Importing Access tables -- Solution found - Thanks

    Hello everyone!

    I am new to this forum and new to VB!! I have really learned quite a bit from the postings that I have reviewed so far, now I hope someone can help me with a particular issue (one of MANY!) that I am faced with. It seems as if it should be easy, however, with my naivity I just can't find the "straightforward" answer!

    I am programming a user interface in VB 6.0. I will have two databases that I am working with. The first db (shell) contains preprogrammed queries and the second db has the data I need to import into the "shell" database.

    It was recommended that I use ADO to connect to the database which is pretty much a foreign language to me right now!

    I need to import all of the tables from the second db into the first database through code. The file name and location will be different everytime (both files! ) so the user will be prompted for this information... other than that, the rest of the process should be in code without user intervention.

    Any thoughts, ideas, or a nice sawed-off shotgun to put me out of my misery right now!!!

    Please tell me this is easy... which is why I can't find any help on the web!!!!!!!

    Thanks,
    Mary
    Last edited by A441OTA; Jul 2nd, 2002 at 03:27 PM.

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Loop through all tables in the db where you want to get the data from.

    use SQL on each of these tables in order to insert data or create new tables and insert data in the other db.

    not sure where u want me to start, do you have anything made out already, or do you need it from scratch?

    if you need help with the entire operation, you should upload a copy of each of the databases, and let us play arround with it.
    Last edited by peet; Jul 2nd, 2002 at 12:26 AM.
    -= a peet post =-

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    78
    Thanks for your quick reply Peet!

    Let me see if I can break this down better for further clarification...

    Database 1 (Shell) -- I created and built some queries just to clean up the data that will be imported in from the second database. This is going to be the main database the users will be connected to for the rest of the program that I create.

    Database 2 (Macro data) -- this is a database created by a macro process in SAS. The tables will be set up exactly the same as the main tables in the Shell. There will be one table named "demodata", then there will be a number of tables that fortunately they have agreed to name the same thing everytime, although there will not always be the same amount. This will make loops easier to write hopefully.

    I didn't even know where to begin on incorporating the import into my interface. What I have right now is a form that requests from the user the location and name of the Shell database (this will reside on individual user's machines -- not a network). Then another form that requests the same information for the Macro database. That, unfortunately is as far as I have gotten with my limited knowledge.

    When I was asked to do this project they made it sound easy -- taking raw data and making it look pretty.... NOT!! It has turned into writing a full-blown VB interface to Access data that will NEVER (the structure of the file will remain the same, but the data is far from "normal") be the same to produce pretty reports!!

    I'm not trying to get someone to write this code for me, I just simply have no idea where to begin!!!!

    Thanks,
    Mary

  4. #4
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Omaha, NE
    Posts
    270
    This isn't the most elegant of solutions, but it generally works to copy data from one database/table to another database/table. For this example, I am using 2 ADO Data Controls on a form & a command button. (This could probably be done without a form by using ADO objects & just put the whole thing into a Sub Main in a module.) Also, this example uses two different Access databases which each have only 1 table whose structure is identical.
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim strLocationSource As String
    3.     Dim strLocationTarget As String
    4.     Dim myField
    5.    
    6.     'Put your user input source file into a variable (I'm hardcoding the location
    7.     strLocationSource = "C:\DATA\Databases\SiteAccess.mdb"
    8.     With Adodc1
    9.         'Assuming a Microsoft Access provider
    10.         .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    11.             strLocationSource & ";Persist Security Info=False"
    12.         'Setting CommandType to a table
    13.         .CommandType = adCmdTable
    14.     End With
    15.    
    16.     'Put your user input target file into a variable (I'm hardcoding the location
    17.     strLocationTarget = "C:\DATA\Databases\TestSites.mdb"
    18.     With Adodc2
    19.         'Assuming a Microsoft Access provider
    20.         .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    21.             strLocationTarget & ";Persist Security Info=False"
    22.         'Setting CommandType to a table
    23.         .CommandType = adCmdTable
    24.     End With
    25. End Sub
    26.  
    27. Private Sub cmdTransferData_Click()
    28.     Dim MyObject
    29.     Dim x As Integer
    30.     Dim y As Integer
    31.     Dim NumberOfTables As Integer
    32.    
    33.     'Sets up error handling in case a defined Source table doesn't exist so _
    34.         it can skip that one
    35.     On Error GoTo TableNotFound
    36.     NumberOfTables = 2  'This would be the maximum number of Tables you would load
    37.  
    38.     'I couldn't find a way to do a For Each loop on a Tables collection,
    39.     'hence the For loop with the hard-coded number of tables from above
    40.     For y = 1 To NumberOfTables
    41.         Select Case y
    42.             Case 1
    43.                 'List first source table name & corresponding target table name
    44.                 Adodc1.RecordSource = "Sites"
    45.                 Adodc2.RecordSource = "TestSitesTable"
    46.             Case 2
    47.                 'List second source table name & corresponding target table name
    48.                 Adodc1.RecordSource = "TableDoesntExist"
    49.                 Adodc2.RecordSource = "TestSitesBogus"
    50.             Case 3
    51.                 'Change the recordsource to the names your next set of tables
    52.             'Repeat this as many times as necessary
    53.         End Select
    54.         'These statements open the recordsource(s) based on above table names
    55.         Adodc1.Refresh
    56.         Adodc2.Refresh
    57.        
    58.         'Walk through each record in source table recordset until EOF
    59.         Do Until Adodc1.Recordset.EOF
    60.             'Issues AddNew method on target table recordset in order to enter new data
    61.             Adodc2.Recordset.AddNew
    62.             'Assuming source & target tables have same table structures
    63.             'Fields are indexed from 0
    64.             For x = 0 To Adodc1.Recordset.Fields.Count - 1
    65.                 'Writes value of each source field into corresponding target field
    66.                 Adodc2.Recordset.Fields(x) = Adodc1.Recordset.Fields(x)
    67.             Next x
    68.             'Issues Update method on Target recordset to write record
    69.             Adodc2.Recordset.Update
    70.             'Moves to next Source record
    71.             Adodc1.Recordset.MoveNext
    72.         Loop
    73.         Adodc2.Refresh
    74. DoNextTable:
    75.     Next y
    76.     Exit Sub
    77. TableNotFound:
    78.     Select Case Err
    79.         'Traps the error for the non-existing table
    80.         Case -2147217865
    81.             Resume DoNextTable
    82.     End Select
    83. End Sub
    I'm sure any number of people in this forum can give you better/cleaner solutions. But, hopefully this can get you started.

    Nate

  5. #5
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208
    Well I've tried the following in an Access environment. I haven't converted it into actual VB. Some syntax will change cause of VBA.

    The following will create all tables excluding system tables and will then populate all the tables created.
    Sub CREATE_NEW_DB()
    Dim rs As Recordset
    Dim newrs As Recordset
    Dim td As TableDef
    Dim newtd As TableDef
    Dim fld As Field
    Dim db As Database
    Dim db2 As Database
    Dim f As Integer
    Dim i As Integer
    'this is your shell db
    Set db = CurrentDb
    'this is your SAS db
    Set db2 = OpenDatabase("c:\test1.mdb")
    'this will create all the database tables excluding the system tables as they already exist
    For Each td In db2.TableDefs
    If Mid(td.Name, 1, 4) <> "msys" Then
    Set newtd = CurrentDb.CreateTableDef(td.Name)
    For Each fld In td.Fields
    With newtd
    .Fields.Append .CreateField(fld.Name, fld.Type, fld.Size)
    End With
    Next fld
    db.TableDefs.Append newtd

    Set rs = td.OpenRecordset
    Set newrs = newtd.OpenRecordset
    While Not rs.EOF
    With newrs
    .AddNew
    For f = 1 To rs.Fields.Count - 1
    .Fields(f).Value = rs.Fields(f).Value
    Next f
    .Update
    End With
    rs.MoveNext
    Wend
    newrs.Close
    rs.Close
    End If


    Next td




    End Sub

  6. #6
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    this is how I would do it.

    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 =-

  7. #7
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    this is how I would do it.

    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 =-

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    78

    Wink Solution found for table import -- Thanks!!!!!!!!!!!!! :-)

    Thank you, Thank you, and Thank you!!!

    Thank you Peet, NateBrei, and Killazzz!! You all could never imagine how grateful I am to you right now (I'll get some sleep tonight!!!).

    I tried all three approaches and Peet's seems to work the best for what I have (I had to reference the DAO library but that was a quick fix -- is that an issue, to reference DAO and ADO in the same project? It was recommended that I use ADO so I hadn't referenced the DAO).

    I owe you all a beer -- look me up when you're in VA!!!!

    Thanks,
    Mary

    P.S. -- this thing gets more and more complicated as I hurdle each step so I'll probably be back, just ignore me when you get sick of my questions!!!! I am trying to find the answers first so I don't wear out my welcome with anyone!!!

    Mary >>>>>>>

  9. #9
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Omaha, NE
    Posts
    270
    You're welcome. I knew that Peet would be one of the expert resources with the simple, concise method. I'd use his solution, too. In fact, I'd use anything I see that he suggests. They're all good.

    Good luck.
    Nate

  10. #10
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    thank you Nate

    A441OTA, if you are told to use ADO, you should stick to ADO.

    I'll make a sample for you later on.

    Now... off to the kindergarden with E

    later
    -= a peet post =-

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

    same sample using ADO and ADOX

    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
    -= a peet post =-

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    78
    Thank you again Peet!!!! Nate is right, I have read through a lot of your replies -- you're wonderful!

    Until next time..... Mary

  13. #13
    New Member
    Join Date
    Aug 2002
    Posts
    2
    when the DBto has password, how to write the code?

  14. #14
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by hsuwz
    when the DBto has password, how to write the code?
    what do you mean hsuwz ?
    -= 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