DoCmd.TransferDatabase - use a variable for source

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bytes access nubie
    New Member
    • Oct 2008
    • 34

    DoCmd.TransferDatabase - use a variable for source

    Attached is code to link tables in databases that are in a folder. My variable for the source in the DoCmd.transferD atabase code Dir(strPath & rst!FolderName) is not working right. I want to loop through the db names to link the tables.

    each db has only 1 table which is named the same as the db. We have a macro that pulls all the db names from the folder into a table called tblDirectory into the field FolderName (that's a bit misleading, I know). I have very little VB knowledge. I'd appreciate any detailed instructions.

    Code:
    Function LinkTable()
    
    Dim rst As DAO.Recordset
    Dim strPath As String
    Dim strFile As String
    
      strPath = "D:\Temp\Testing\"
    
    Set rst = CurrentDb.OpenRecordset("tblDirectory", dbOpenTable)
    Do Until rst.EOF
         'ensure exist
           strFile = Dir(strPath & rst!FolderName)
          If Len(Dir(strPath & rst!FolderName)) > 0 Then
                DoCmd.TransferDatabase acLink, "Microsoft Access", Dir(strPath & rst!FolderName), acTable, rst!FolderName, rst!FolderName, False
                End If
        rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
    
    End Function
  • jforbes
    Recognized Expert Top Contributor
    • Aug 2014
    • 1107

    #2
    I didn't see anything glaring obviously wrong with the code, so it would be nice to know how it is not working right. What error are you getting?

    Comment

    • bytes access nubie
      New Member
      • Oct 2008
      • 34

      #3
      access says, "error 3024, cannot find the file..." and displays the path of my default folder (the documents folder) and the name of the first db in the list. thank you for looking.

      Comment

      • bytes access nubie
        New Member
        • Oct 2008
        • 34

        #4
        after clicking debug access stops on the line DoCmd.TransferD atabase acLink, "Microsoft Access", Dir("D:\Temp\Te sting\" & rst!FolderName) etc

        Comment

        • jforbes
          Recognized Expert Top Contributor
          • Aug 2014
          • 1107

          #5
          I think line 14 should be:
          Code:
                      DoCmd.TransferDatabase acLink, "Microsoft Access", strPath & rst!FolderName, acTable, rst!FolderName, rst!FolderName, False

          Comment

          • bytes access nubie
            New Member
            • Oct 2008
            • 34

            #6
            unfortunately that did not do it. I get a message that access database engine could not find the object... and lists the first db name in the table. here's the current code:
            Code:
            Function LinkTable()
            
            Dim rst As DAO.Recordset
            Dim strPath As String
            Dim strFile As String
            
              strPath = "D:\Temp\Testing\"
            
            Set rst = CurrentDb.OpenRecordset("tblDirectory", dbOpenTable)
            Do Until rst.EOF
                 'ensure exist
                   strFile = Dir(strPath & rst!FolderName)
                  If Len(Dir(strPath & rst!FolderName)) > 0 Then
                        DoCmd.TransferDatabase acLink, "Microsoft Access", strPath & rst!FolderName, acTable, rst!FolderName, rst!FolderName, False
                        'DoCmd.TransferDatabase acLink, "Microsoft Access", Dir("D:\Temp\Testing\" & rst!FolderName), acTable, rst!FolderName, rst!FolderName, False
                        End If
                rst.MoveNext
            Loop
            rst.Close
            Set rst = Nothing
            End Function

            Comment

            • jforbes
              Recognized Expert Top Contributor
              • Aug 2014
              • 1107

              #7
              Here is the examples from Microsoft on the
              Code:
              DoCmd.TransferDatabase:
              DoCmd.TransferDatabase acImport, "Microsoft Access", _ 
                  "C:\My Documents\NWSales.mdb", acReport, "NW Sales for April", _ 
                  "Corporate Sales for April"
              
              DoCmd.TransferDatabase acLink, "ODBC Database", _ 
                  "ODBC;DSN=DataSource1;UID=User2;PWD=www;LANGUAGE=us_english;" _
                  & "DATABASE=pubs", acTable, "Authors", "dboAuthors"
              When they refer to an Access database the full Database Name is listed, including “.MDB”. I have a feeling that may need to be included as well. So maybe Line 14 should be:
              Code:
                          DoCmd.TransferDatabase acLink, "Microsoft Access", strPath & rst!FolderName & “.mdb”, acTable, rst!FolderName, rst!FolderName, False

              Comment

              • bytes access nubie
                New Member
                • Oct 2008
                • 34

                #8
                thanks again. the .accdb is included in the table (see below):

                FolderName
                AUTH.accdb
                MONTHS.accdb
                PHASE.accdb
                ACCT_LST.accdb

                Comment

                • bytes access nubie
                  New Member
                  • Oct 2008
                  • 34

                  #9
                  actually, access tells me the first table name, it says it can't find the folder. so it's having problems finding the actual location

                  Comment

                  • jforbes
                    Recognized Expert Top Contributor
                    • Aug 2014
                    • 1107

                    #10
                    The picture is getting clearer. I would guess the File is being found since you are getting past line 13 which is testing for it's existence. I should have realized that if that was working, the file extension is included in rst!FolderName.

                    So since the Extension is in FolderName, it should be stripped out when referring to the tables. You might want to try this for line 14:
                    Code:
                    DoCmd.TransferDatabase acLink, "Microsoft Access", strPath & rst!FolderName, acTable, Left(rst!FolderName, InStr(rst!FolderName, ".") - 1), Left(rst!FolderName, InStr(rst!FolderName, ".") - 1), False
                    If it works, it might be best to go back and clean it up by putting table name into a string of it's own.

                    Comment

                    • bytes access nubie
                      New Member
                      • Oct 2008
                      • 34

                      #11
                      wooohooo!! thank you!!! worked!!

                      Comment

                      Working...