Results 1 to 5 of 5
  1. #1
    sisva18 is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2021
    Posts
    7

    Copy data from one database to another ADOX

    Hey I was wondering about how you would move tables from one db to another.

    So far I have defined the function TransferData and I have defined the sConn from my source db to give to cnn and set sourceCat with it.
    When I look in the debugger I can see that the tables are showing and the connection is right.

    The problem is with my targetCat where I have tried giving it the currentproject.connection as active connection. But that wont do it..
    Then I tried defining a sConnOne which is the other db connection string then i created another adodb connection cnnOne to give it the sConnOne connection.
    I then looked in the debugger and saw that it was indeed the right tables it was showing from sConnOne.
    But then when I ran the code TransferData("TableTest") I get a errorcode 3251.

    Function TransferData(tableName As String)

    Dim cnn As New ADODB.Connection
    Dim cnnOne As New ADODB.Connection
    Dim sourceCat As New ADOX.Catalog
    Dim targetCat As New ADOX.Catalog
    Dim sConn As String
    Dim sConnOne As String

    sConn = "Driver={SQL Server};" & _
    "SERVER=SRV-SQL-TEST;" & _
    "Trusted_Connection=Yes;" & _
    "DATABASE=" & getDatabaseNameDev

    sConnOne = "Driver={SQL Server};" & _
    "SERVER=SRV-SQL-TEST;" & _
    "Trusted_Connection=Yes;" & _
    "DATABASE=" & getDatabaseName


    cnn.Open sConn
    Set sourceCat.ActiveConnection = cnn

    'cnnOne.Open sConnOne
    Set targetCat.ActiveConnection = CurrentProject.Connection
    'cnnOne



    Dim sourceTable As ADOX.Table
    Set sourceTable = sourceCat.Tables(tableName)

    Dim newTable As New ADOX.Table
    Set newTable.ParentCatalog = targetCat
    newTable.name = sourceTable.name

    Dim sourceCol As ADOX.Column
    Dim newCol As ADOX.Column

    For Each sourceCol In sourceTable.Columns
    Set newCol = New ADOX.Column
    newCol.name = sourceCol.name
    'newCol.Type = sourceCol.Type
    newCol.DefinedSize = sourceCol.DefinedSize
    newCol.ParentCatalog = targetCat

    newTable.Columns.Append newCol
    Next sourceCol

    targetCat.Tables.Append newTable

    End Function

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Do you know that you can just open the target db and select New Data Source on External Data tab, choose From Database > Access and navigate to the db containing the tables you want and import them? IIRC, you can save that as an import spec.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    sisva18 is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2021
    Posts
    7
    Quote Originally Posted by Micron View Post
    Do you know that you can just open the target db and select New Data Source on External Data tab, choose From Database > Access and navigate to the db containing the tables you want and import them? IIRC, you can save that as an import spec.
    Yes I know I can but the problem is that I have a developement database and a production database. I want to commit the changes from developement database to production database so I need it to be in code.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Sorry, I still am not on the same page. Sounds like your db is not split, which would be a mistake. Any development db I worked on only contained the modules, forms, reports and queries. When that was released (or re-released) only the fe was distributed and the links altered to connect to the production data in the back end tables. So no tables ever needed to be copied anywhere. I'm afraid I don't see the point of all that code to do what you can do with the ribbon regardless of the reason why it needs to be done. Best I let someone with more ADO experience help you as I have rarely needed ADO as opposed to DAO.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Must be split if trying to connect to SQLServer.

    However, I also don't understand the situation.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Need to copy a lot of data from old to new database
    By ultrarunner2017 in forum Access
    Replies: 21
    Last Post: 02-10-2020, 07:52 AM
  2. A quick way to copy all tables from a database (data only)
    By earlcools in forum Import/Export Data
    Replies: 1
    Last Post: 10-13-2017, 02:48 AM
  3. Copy data from table in another database
    By jcc285 in forum Import/Export Data
    Replies: 8
    Last Post: 04-07-2016, 05:12 AM
  4. Replies: 0
    Last Post: 12-17-2010, 07:29 AM
  5. Replies: 0
    Last Post: 08-05-2010, 08:45 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
  •  
Other Forums: Microsoft Office Forums