How to import csv file to Access with VBA code? - vba

I have a csv file, that I need to import to Access using VBA.
I'm using the following code :
Call DoCmd.TransferText(acImportDelim, , TableName, SourceFile, HasFieldNames)
Where TableName, SourceFile and HasFieldNames are variables that store information about the file.
The import works but I have only one column imported in the table.
Does anyone have an idea please ?
The file is a csv with the separator ";" and has multiple columns.
Thank you.

"CSV" stands for "Comma Separated Values". Hence, the default import functionality looks for commas.
If you're using anything but commas, try this:
Start importing the file manually.
When you have specified the settings, just before clicking Finish, click Advanced...
Click "Save As" and specify a name (or accept the one proposed by Access).
Make note of the name you choose, let's say "Data Import Specification".
Click OK twice, then cancel the import.
Now use the import specification that you created in the code:
DoCmd.TransferText acImportDelim, "Data Import Specification", "Table1", "D:\Data.csv", False

You need to create (and use in .TransferText) an Import Specification where you specify the separator string. See e.g. these answers:
https://stackoverflow.com/a/3417067/3820271
https://stackoverflow.com/a/32806065/3820271

Related

Access VBA - Export table to CSV file

I know many variations of this question were already asked but I kinda hit a dead end. I am trying to export a table with 2 columns into a .csv file. I tried it using similar question with this line of code.
Once without specification:
DoCmd.TransferText acExportDelim, , "+SapDebExp", "U:\Desktop\sapDeb1.csv", True
Once with specification:
DoCmd.TransferText acExportDelim, a11, "TSapDebExp", "U:\Desktop\sapDeb.csv", True
But no matter what I try all the columns are compressed into a single column in the created .csv
I also tried a workaround with this:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "TSapDebExp", "U:\Desktop\sapDeb.xlsx", True
FileCopy "U:\Desktop\sapDeb.xlsx", "U:\Desktop\sapDeb.csv"
This worked and the columns were separated in the end file but when you first open the .csv there comes a pop-up message saying "you sure you want to trust this file". This wouldn't be a problem since you only need to click it once but I want to import this file somewhere else (so no one will open the .csv file between the export and import). Is there another workaround or am I doing something wrong with the first method?

Convert DBF to CSV in MS Access

In MS Access, is there a way using VBA to export an arbitrary DBF file to CSV format without needing to manually export it first / create a specification file? I just want to make a function that will work for any DBF. Is there a function that does this already?
Not an arbitrary file, but you can use
DoCmd.TransferDatabase acLink, ...
' and
DoCmd.TransferText acExportDelim, ...
to first link a specific dBase file, then export it to a text file. Using neither an import/link specification nor an export specification, the commands will use the default settings of Access.

Export Access Query WITHOUT Formatting

Relatively simple, but I can't seem to work it out. I want to export a query from access into a .csv (tab or comma delimited). When I do it manually through the wizard it works fine. But when I do it via vba, it comes complete with dash formatting that looks like the borders in the table!
I tried two methods and got the same results
DoCmd.OutputTo acOutputQuery, "Qry_GRADE", "MS-DOSText(*.txt)",_
"grade.csv", True, *ExportSpec*, , acExportQualityScreen
I used it with or without "ExportSpec", which is a specification I created when exporting manually.
This is the second method:
Dim testSQL As String
Dim qd As DAO.QueryDef
testSQL = "SELECT * FROM Qry_Grade"
Set qd = db.CreateQueryDef("tmpExport", testSQL)
DoCmd.TransferText acExportDelim, , "tmpExport",_
"C:\Users\Databoe\Documents\KidsTV\grade.csv"
db.QueryDefs.Delete "tmpExport"
This is a solution I've found which seems like overkill
And this is what the output looks like:
You can see it's not actually split any of the columns when opening the file in excel and that every second line is just a string of "-"'s
What about DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, myQueryName, myExportFileName, True for direct excel file export.
I tried your approaches, but I only get formated text with your first try DoCmd.OutputTo acOutputQuery, "Qry_GRADE", "MS-DOSText(*.txt)",_
"grade.csv", True, *ExportSpec*, , acExportQualityScreen which is as expected because it's a text export not csv.
With your second method I always get an excel compatible result. Maybe you have an error trap that hides an error and the first grade.csv is not overwritten. Use a different filename for the second method to prevent that.
Found a second trap. You don't specify full file path in first method, but in second. If C:\Users\Databoe\Documents\KidsTV is not your default document path, you have 2 grade.csv in different folders, but you maybe think that you only have one that gets overwritten.
I just ran into this problem myself, and found a great work around. It doesn't save as a .csv, but you can save as a comma delimited .txt file.
Use the export wizard on the External Data tab to export your query as a .txt file without formatting.
Once the file is exported you get a dialogue box asking if you want to save export steps. Click the box and save the export.
There is an action available in the Macro wizard called "Run Saved Import/Export." Select this action and choose your saved export from the dropdown menu.
Very frustrating that even now I cant seem to make Access export a simple csv file. I do not know why they think I need pretty formatting. Try this: open Excel, Click Get Data, From Database, From MicroSoft Access Database. Select the Access Database you wish to export from. Select the table/query we want saved as an csv. This will set up a link to this table. Once imported, save the Excel file to an csv file.

How to import text file into existing Access table using Visual Basic(button click) (Visual Basic 2015)) [duplicate]

I am trying to automate the adding of new text files, which all have the same (known) layout.
The columns are separated using tabs (the TAB button). My question is, is it possible to do this in VBA? Like, in the access wizard for importing text files?
I am using the DoCmd.TransferText method in VBA
You'll need to go through the wizard once to make your specification file. TO do this import your text file like normal but before you get too deep into the wizard click on the bottom left, the "Advanced..." button. This is where you make your spec file.
Make ll these columns match your input file, data types and all. Be sure to select the {tab} field delimiter and the appropriate text qualifier if you are using one.
Save your spec (which can later be edited by coming back to this same screen and clicking Specs... then saving over your old one)
Now you can use in VBA like this
DoCmd.TransferText acImportDelim, "your spec name", "destination table name", sourceFilePath
There is a parameter HasFieldNames that you'll have to decide if it is true or false based on your file.
With the import wizard the downside is that for even the slightest change in file format, you'll have to click through all those steps yet again to get the import working.
Check out #Remou's answer in ms Access import table from file in a query for a way to do it in straight SQL. I am actually using the same method in a project of mine. I use something like this (see my link for the details):
insert into MyTable (column-list...)
select (column-list...)
from [data-source-specifications].[file-name]
any-other-clauses...;
Just one caveat. If you put this SQL syntax into a normal Access query object, there's a good chance that Access will mangle it to the point where it won't even be able to open the query object. So compose and save the query in a text file while you try it out in Access. Once the query is tested and working, save it in a VBA subroutine so that Access will run it exactly as is, like so:
sub MyTableImport()
sqlStr = " insert into MyTable (column-list) " ' leave a space at the
sqlStr = sqlStr & "select (column-list...) " ' end of each line of the string
sqlStr = sqlStr & "from [data-source-specifications].[file-name] "
sqlStr = sqlStr & "any-other-clauses... ;"
DoCmd.RunSQL sqlStr
end sub

Importing a text with separators

I am trying to automate the adding of new text files, which all have the same (known) layout.
The columns are separated using tabs (the TAB button). My question is, is it possible to do this in VBA? Like, in the access wizard for importing text files?
I am using the DoCmd.TransferText method in VBA
You'll need to go through the wizard once to make your specification file. TO do this import your text file like normal but before you get too deep into the wizard click on the bottom left, the "Advanced..." button. This is where you make your spec file.
Make ll these columns match your input file, data types and all. Be sure to select the {tab} field delimiter and the appropriate text qualifier if you are using one.
Save your spec (which can later be edited by coming back to this same screen and clicking Specs... then saving over your old one)
Now you can use in VBA like this
DoCmd.TransferText acImportDelim, "your spec name", "destination table name", sourceFilePath
There is a parameter HasFieldNames that you'll have to decide if it is true or false based on your file.
With the import wizard the downside is that for even the slightest change in file format, you'll have to click through all those steps yet again to get the import working.
Check out #Remou's answer in ms Access import table from file in a query for a way to do it in straight SQL. I am actually using the same method in a project of mine. I use something like this (see my link for the details):
insert into MyTable (column-list...)
select (column-list...)
from [data-source-specifications].[file-name]
any-other-clauses...;
Just one caveat. If you put this SQL syntax into a normal Access query object, there's a good chance that Access will mangle it to the point where it won't even be able to open the query object. So compose and save the query in a text file while you try it out in Access. Once the query is tested and working, save it in a VBA subroutine so that Access will run it exactly as is, like so:
sub MyTableImport()
sqlStr = " insert into MyTable (column-list) " ' leave a space at the
sqlStr = sqlStr & "select (column-list...) " ' end of each line of the string
sqlStr = sqlStr & "from [data-source-specifications].[file-name] "
sqlStr = sqlStr & "any-other-clauses... ;"
DoCmd.RunSQL sqlStr
end sub