Solved

Import csv files to a table in MS Access

Posted on 2012-03-18
9
818 Views
Last Modified: 2012-03-22
Hello Experts,
I'm planning to build an MS Access db that imports files on button click. Sounds simple but unfortunately i'm quite lost as to where or how to start it...Anyway, below are the things i wanted to happen or what my access db would do.
1.0 On ButtonClick.
2.0 Import all the csv files which follow the fielname convention (C:\Temp\Request_*.csv) to Request table in access. Note all csv's have the same number of columns.
So far my search for solution yielded me this piece of code(see attached) which somehow matches my requirements.

Private Sub cmdButton_Click()
    LocateFile ("Request_*.csv")    ' uses * for wildcard for strfilename to match.
End Sub

Function LocateFile(strFileName As String)
   Dim csvFile As Variant
   With Application.FileSearch
      .FileName = strFileName
      .LookIn = "c:\Temp\"
      .SearchSubFolders = False   'do not search in lower folders.
      .Execute
      For Each csvFile In .FoundFiles
         DoCmd.TransferSpreadsheet acImport, MySpecs, Request,csvFile, True, ""
      Next csvFile
   End With

End Function

...Would you be able to help me simplify/modify  the code so that it would work on my solution if this would address my requirements otherwise other suggested avenues is welcome.
0
Comment
Question by:jsuanque
9 Comments
 
LVL 77

Assisted Solution

by:peter57r
peter57r earned 30 total points
ID: 37736093
You need to modify your import command..

 DoCmd.TransferText acImport, "MySpecs", "Request", csvFile, True
0
 

Author Comment

by:jsuanque
ID: 37736190
Thanks peters57r...but for some reason it still does not populate. I meant no error ...but seems to have nothing happened.

Private Sub cmdButton_Click()
    LocateFile ("Request_*.csv")    ' uses * for wildcard for strfilename to match.
End Sub

Function LocateFile(strFileName As String)
   Dim csvFile As Variant
   With Application.FileSearch
      .FileName = strFileName
      .LookIn = "c:\Temp\"
      .SearchSubFolders = False   'do not search in lower folders.
      .Execute
      For Each csvFile In .FoundFiles
         DoCmd.TransferText acImport, "MySpecs", "Request",csvFile, True, ""
      Next csvFile
   End With

End Function
0
 

Author Comment

by:jsuanque
ID: 37736209
Now i did get the error ...
Run-timeerror '2455':

You entered an expression that has an invalid reference to the property FileSearch.

And when you click debug it goes to this part of the code...
....
   With Application.FileSearch
...
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:jsuanque
ID: 37736224
Seems that the code i copied is for older versions ...current version of MS Access does not have filesearch but rather DIR or FileSystemObject class.
0
 
LVL 77

Expert Comment

by:peter57r
ID: 37736690
I didn't notice the version in the Tag and I assumed you were using code appropriate to your version.
Are you saying you have sorted this out now or do you still need assistance?
0
 
LVL 39

Accepted Solution

by:
als315 earned 60 total points
ID: 37737737
Example of file import:
Dim path As String, Mask As String, i As Long
path = "c:\Temp"
Mask = ".csv"
i = read_files(path, Mask)
MsgBox i & " files imported", vbOKOnly

Function read_files(pathd As String, smask As String) As Long
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
read_files = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(pathd)
For Each objFile In objFolder.Files
  If MatchSpec(objFile.Name, smask) Then
        DoCmd.TransferText acImport, "MySpecs", "Request",objFile, True, ""
        read_files = read_files + 1
  End If
Next objFile
End Function

Open in new window

0
 
LVL 44

Expert Comment

by:GRayL
ID: 37737908
Remove the comma and double quotes after True in the Docmd. line
0
 
LVL 31

Assisted Solution

by:Helen_Feddema
Helen_Feddema earned 60 total points
ID: 37739473
I do see FileSearch as a member of the Application object in Access 2003, FWIW.  It is really part of the Office object model, but you have a FileSeach property in Access.

I recommend using the named argument syntax for clarity:

    DoCmd.TransferText transfertype:=acExportDelim, _
      specificationname:="CustomerImportSpecs", _
      tablename:="tblCustomers", _
      FileName:="D:\Documents\Examples\Export Delimited.csv", _
      hasfieldnames:=True

Open in new window

0
 

Author Closing Comment

by:jsuanque
ID: 37752550
Thanks heaps Guys...
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question