import CSV files into new tables

ashwinghanta

Board Regular
Joined
Dec 6, 2011
Messages
118

I am trying to import CSV files located in a folder into Access as new tables and I have been trying with a code

<tbody>
</tbody>



I am trying to import CSV files located in a folder into Access as new tables and I have been trying with a code

Code:
Public Const rootdir = "C:\Users\deb670s\Desktop\importcsv"


Sub import()
    Dim nr As Integer
    Dim file As AcBrowseToObjectType
    file = Dir$(rootdir & "*.csv")
    nr = 1
    Do While file <> ""
    DoCmd.TransferText acImportDelim, "ImportSpec", "NewTableName-" & nr, rootdir & file, True, msoEncodingCentralEuropean
    file = Dir$
    nr = nr + 1
    Loop
End Sub

But I get an error saying variable not defined at msoEncodingCentralEuropean Can someone tell me where am I going wrong?

<tbody>
</tbody>
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Where did you get that "msoEncodingCentralEuropean" argument from?
What happens if you remove it from the command all together?
 
Upvote 0
I got the "msoEncodingCentralEuropean" arguement from another code. I have tried removing it and now I get the error Type mismatch on the line
Code:
 [/COLOR][/COLOR]file = Dir$(rootdir & "*.csv")[COLOR=#333333][COLOR=#333333]
 
Upvote 0
Is "importcsv" a folder name?
If so, try adding another slash after it, i.e.
Code:
Public Const rootdir = "C:\Users\deb670s\Desktop\importcsv\"
 
Upvote 0
Can you confirm that there are csv files in that folder?
 
Last edited:
Upvote 0
Yes, I can confirm that the CSV files are in importcsv folder on the Desktop. I have tried with a modified code
Code:
Public Const rootdir = "C:\Users\deb670s\Desktop\importcsv\"


    Sub import()
    Dim nr As Integer
    Dim file As String
    file = Dir$(rootdir & "*.csv")
    nr = 1
    Do While file <> ""
    DoCmd.TransferText acImportDelim, , "NewTableName-" & nr, rootdir & file, True, 1250
    file = Dir$
    nr = nr + 1
    Loop
    End Sub
However it gives me another error Run-time error '2521': You have specified a Transfer Type that doesn't support the HTML Table Name arguement.
 
Upvote 0
Where are you getting these arguments from that you keep adding?
The 6th argument for TransferText is HTMLTableName and is optional. You have a value of 1250 for it. Why? Try dropping it altogether, i.e.
Code:
DoCmd.TransferText acImportDelim, , "NewTableName-" & nr, rootdir & file, True
 
Upvote 0
I have finally figured out the workaround. Here is the code

Code:
Option Compare Database
Option Explicit


Function DoImport()


 Dim strPathFile As String
 Dim strFile As String
 Dim strPath As String
 Dim strTable As String
 Dim blnHasFieldNames As Boolean


 blnHasFieldNames = True


 strPath = "C:\importcsv\"


 strFile = Dir(strPath & "*.csv")


 Do While Len(strFile) > 0
       strTable = Left(strFile, Len(strFile) - 4)
       strPathFile = strPath & strFile
       DoCmd.TransferText acImportDelim, "importspec", strTable, strPathFile, blnHasFieldNames




       strFile = Dir()


 Loop


End Function

I have changed the location of the folder. I think it was causing a problem with Users as I'm using a German windows system. I'd like to thank you for the help :)
 
Upvote 0

Forum statistics

Threads
1,221,810
Messages
6,162,108
Members
451,743
Latest member
matt3388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top