Solved

Import csv files to a table in MS Access

Posted on 2012-03-18
9
816 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
Comment Utility
You need to modify your import command..

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

Author Comment

by:jsuanque
Comment Utility
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
Comment Utility
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
 

Author Comment

by:jsuanque
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 77

Expert Comment

by:peter57r
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks heaps Guys...
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now