Results 1 to 2 of 2

Thread: Importing CSVs to Access with VBA
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular tyija1995's Avatar
    Join Date
    Feb 2019
    Posts
    381
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    3 Thread(s)

    Default Importing CSVs to Access with VBA

    Hey, I have 55 CSV files that I want to export in to Access, I am currently using a macro but it only takes the first file from the directory, I need a loop to take all of the files from the same directory but I am not sure how to implement it, any help would be appreciated. I am using the following code currently:

    Code:
    Public Function Imprt_CSV()
    
    
     Dim strPathFile As String
     Dim strFile As String
     Dim strPath As String
     Dim strTable As String
     Dim blnHasFieldNames As Boolean
    
    
        blnHasFieldNames = True
    
    
     strPath = Left(CurrentProject.Path, InStrRev(CurrentProject.Path, "\")) & "\OrigData\"
    
    
     strFile = Dir(strPath & "*.csv")
     
           strTable = strFile
           strPathFile = strPath & strFile
           DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
    
    
           strFile = Dir()
           
    End Function
    Running this pushes through the first file in the directory I am using.
    √-1 2³ ∑ π
    …And it was delicious!

  2. #2
    Board Regular
    Join Date
    May 2013
    Posts
    582
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Importing CSVs to Access with VBA

    Set up a loop

    Code:
    Do While strFile <> ""
           strTable = strFile
           strPathFile = strPath & strFile
           DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
    
    
           strFile = Dir()
    Loop
    I cannot see how you can have a table name the same as the file name as that also have the extension? xxx.csv
    Office 2007
    Access novice. Sometimes trying to give something back

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •