Avatar of jsuanque
jsuanque
Flag for Australia asked on

Import csv files to a table in MS Access

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.
Microsoft Access

Avatar of undefined
Last Comment
jsuanque

8/22/2022 - Mon
SOLUTION
peter57r

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jsuanque

ASKER
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
jsuanque

ASKER
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
...
jsuanque

ASKER
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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
peter57r

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?
ASKER CERTIFIED SOLUTION
als315

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
GRayL

Remove the comma and double quotes after True in the Docmd. line
SOLUTION
Helen Feddema

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jsuanque

ASKER
Thanks heaps Guys...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.