Need VBA code to copy table from two different databases

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aflores41
    New Member
    • Nov 2014
    • 57

    Need VBA code to copy table from two different databases

    Hello,

    I have the code below. It used to work but now for some reason access breaks when running with error code 2486, "You can't carry out this action at present time.".

    Code:
    Private Sub btn_archive_Click()
    Dim objAcc As Object
    Dim objAcc2 As Object
    Dim filename As String
    filename = InputBox(prompt:="Type your file name here.")
    If Len(filename) = 0 Then
    MsgBox "No file name chosen"
    Exit Sub
    End If
     
    Set objAcc = New Access.Application
    objAcc.OpenCurrentDatabase ("P:IBA Delinquency DB Data.accdb")
    objAcc.DoCmd.CopyObject "P:IBA Delinquency DB Archive.accdb", filename, acTable, "tl_delinquency"
     
    objAcc.CloseCurrentDatabase
     
    Set objAcc = Nothing
    MsgBox ("Your data has been archived, " & filename & "!")
    Any assistance is greatly appreciated!

    Thank you!

    -Al
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    Al,
    As with any other problem report, it's important to give all of the details. Have you stepped through this with the debugger to see where the error is happening?

    Perhaps your drive P: is not available? Maybe it would help if the code included the full path instead of just "P:", even if the full path is only "P:\"

    You might try doing this "manually" to see if you can discover anything that is not obvious letting VBA handle it.

    Do the DBs involved get compressed from time to time?

    Jim

    Comment

    • twinnyfo
      Recognized Expert Moderator Specialist
      • Nov 2011
      • 3664

      #3
      Your lines 12-13 should be:

      Code:
      objAcc.OpenCurrentDatabase ("P:[B][U]\[/U][/B]IBA Delinquency DB Data.accdb")
       objAcc.DoCmd.CopyObject "P:[B][U]\[/U][/B]IBA Delinquency DB Archive.accdb", filename, acTable, "tl_delinquency"
      You need the backslash, even if the file is in the root directory.

      Comment

      • aflores41
        New Member
        • Nov 2014
        • 57

        #4
        The full path works. Also, the backlash "\" is there on the code. I just took it out when copying.

        full path of the code.

        P:\Travel Card Program\Delinqu ency DB\IBA Delinquency DB\IBA Delinquency DB Data.accdb

        After the error code pops up. (From the initial inquiry up top). The code below is highlighted:

        Code:
        objAcc.DoCmd.CopyObject "P:\Travel Card Program\Delinquency DB\IBA Delinquency DB\IBA Delinquency DB Archive.accdb", filename, acTable, "tl_delinquency"

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3664

          #5
          aflores,

          Now that I look at the rest of that line, it looks like you may be using the incorrect method. Try using the Transfer Database method, which allows you to more easily transfer objects between two databases.

          Your original post is trying to copy a table as a database....

          Comment

          • aflores41
            New Member
            • Nov 2014
            • 57

            #6
            I had the error 13, "type mismatch" for the code below after modifying to transfer database rather copyobject.

            Code:
            Dim objAcc As Object
            Dim objAcc2 As Object
            Dim filename As String
            filename = InputBox(prompt:="Type your file name here.")
            If Len(filename) = 0 Then
            MsgBox "No file name chosen"
            Exit Sub
            'ElseIf filename = "Data Entry_tbl" Then
            'MsgBox "Change file name"
            'Exit Sub
            End If
             
            Set objAcc = New Access.Application
            objAcc.OpenCurrentDatabase ("P:\Travel Card Program\Delinquency DB\Delinquency DB Data.accdb")
            objAcc.DoCmd.TransferDatabase "P:\Travel Card Program\Delinquency DB\Delinquency DB Archive.accdb", filename, acTable, "tl_delinquency"
             
            objAcc.CloseCurrentDatabase
             
            Set objAcc = Nothing
            MsgBox ("Your data has been archived, " & filename & "!")
            [qry_Delinquent 51 subform].Requery
                    [qry_Delinquency Frequency subform].Requery
                        [tl_delinquency_staging subform].Requery
                                             [qry_list of delinquency subform].Requery
                                                            [tl_delinquency_tracker].Requery
                Application.SetOption ("auto compact"), 1
                Me.TabDelinquency.Value = 0

            Comment

            • twinnyfo
              Recognized Expert Moderator Specialist
              • Nov 2011
              • 3664

              #7
              Where is the error happening? Which line?

              If it is Line 15, then it could be a confusion in the arguments:

              Code:
              objAcc.DoCmd.TransferDatabase _
                  [B][U]acImport[/U][/B], _
                  "P:\Travel Card Program\Delinquency DB\Delinquency DB Archive.accdb", _
                  [B][U]acTable[/U][/B], _
                  [B][U]filename[/U][/B], _
                  "tl_delinquency"
              Also, is the "Delinquenc y DB Archive.accdb" the file you are currently in or the DB from where the source table is. Your descriptions and question are not altogether clear, so we have to guess at exactly what you are trying to do.

              Additionally, with this method, I don't believe you have to actually open the DB (setting objACC), you just execute the code.

              Comment

              • aflores41
                New Member
                • Nov 2014
                • 57

                #8
                So there's three databases:
                1. Delinquency DB - Front End - this is where the code is
                2. Delinquency DB Data - Store Data - takes data from here
                3. Delinquency DB Archive - Archive Data - store data here

                Comment

                • twinnyfo
                  Recognized Expert Moderator Specialist
                  • Nov 2011
                  • 3664

                  #9
                  aflores,

                  Have you have able to experiment with this method?

                  Comment

                  • aflores41
                    New Member
                    • Nov 2014
                    • 57

                    #10
                    The code worked before and It's not working now. I don't know why. I'll keep researching. Thanks Twinn.

                    Comment

                    Working...