Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Import csv files to a table in MS Access

Posted on 2012-03-18
9
Medium Priority
?
824 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

722 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