Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Import csv files to a table in MS Access

Posted on 2012-03-18
9
Medium Priority
?
827 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 120 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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 40

Accepted Solution

by:
als315 earned 240 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 240 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

916 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