MS Access VBA DoCmd.TransferText How to Separate Columns? - ms-access

The following line should import a semi-colon ; delimited .csv text file to a table.
DoCmd.TransferText TransferType:=acImportDelim, SpecificationName:=specNote, TableName:="tblImport", FileName:="K:\path\file.csv"
However this puts all the fields in one field.
What is going wrong here?
Any suggestions?
I specified
File format: Delimited
Field delimiter: ;
I set this through the import wizard and saved this specification. With the wizard all went good.
I want to share the specification but I dont know how to do that?
Many thanks.

Is specNote a variable or the name of your specification?
If the latter, try:
DoCmd.TransferText _
TransferType:=acImportDelim, _
SpecificationName:="specNote", _
TableName:="tblImport", _
FileName:="K:\path\file.csv"

Related

importing CSV file encoded as UTF-8 into Access

Further to an earlier question here
Fast import of csv file into access database via VB.net 2010
I tried using the following code in my .NET application (VB.NET 2010)
cmd.CommandText =
"SELECT F1 AS id, F2 AS firstname " &
"INTO MyNewTable " &
"FROM [Text;FMT=Delimited;HDR=No;CharacterSet=850;DATABASE=C:\__tmp].table1.csv;"
and it seemed to work, but when I opened the database in Access the table showed garbled characters.
I think maybe CharacterSet=850 is not the correct setting for my CSV file. I tried searching for a character set list, but I couldn't find it.
My .csv file uses UTF-8. What should I use for the CharacterSet number?
The CharacterSet number for UTF-8 is CharacterSet=65001, so your CommandText should be
cmd.CommandText =
"SELECT F1 AS id, F2 AS firstname " &
"INTO MyNewTable " &
"FROM [Text;FMT=Delimited;HDR=No;CharacterSet=65001;DATABASE=C:\__tmp].table1.csv;"
Note also that this approach requires that the UTF-8 file be saved without a BOM (byte order mark), which is unusual for the Windows platform. (If the file does include a BOM then the first record will be imported as blank fields.)

Import UTF-8 file in VBA

I'd like to import a pipe delimited file in a table but this file is in UTF-8 with dynamic structure.
I had tried with TranfertText and FSO but only ADODB.Stream seems to deal well with such an encoding however it only read the full text...
How can I read such a file line per line to add rows in an existing table ?
Thanks in advance.
You can read a line from an ADO Stream with its ReadText method.
strLine = objStream.ReadText -2 ' adReadLine
You may need to set your stream's LineSeparator property first.
After you read the line, you can split on the pipe character.
Split(strLine, "|")

Entering data from an excel spreadsheet into database?

I'm looking for a clever way to extract 500 plus lines of data from an excel spreadsheet and enter is into my database.
The spreadsheet is like this
My table 'tbl_foot_teams' is set out as
id | name | rating
Quite simply, I need to enter get the two columns from the spreadsheet into the database fields name and rating.
Is there any efficient way to achieve this?
Individually, it will take me a ridiculous amount of time!
Thanks
Save Excel file as CSV and use LOAD DATA INFILE command to import data.
Your excel file has no id field. Make id field in the table as AUTO_INCREMENT, and use command like this -
LOAD DATA INFILE 'file_name.csv' INTO TABLE tbl_foot_teams
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
-- IGNORE 1 LINES -- if csv file has column headers
(name, rating)
SET id = NULL; -- this will set unique value for each row
Also, have a look at GUI Data Import tool (Excel or CSV format) in dbForge Studio for MySQL.
In phpmyadmin you have an Import From Excel option.
If you don't have one, you may have Import From CSV, so just convert the spreadsheet to CSV.
If you have none of above, you can write a php function that opens a text file, makes explode by rows and then explode by values
If we are talking about 50 rows, you can create easily a new column on spreadsheet with a formula to concatenate your values to a insert statement. Something like:
=concat( "insert into tbl_foot_teams ( name , rating) values ( " , $b8 , " ...
then, copy paste calculate formula text result on your database.
You don't specify what database you're using, but an easy way to do this with MySQL would be to export the spreadsheet as a csv file and then import to MySQL with mysqlimport.
This is described in a comment on this MySQL page, from user Philippe Jausions:
If you are one of the many people trying to import a CSV file into
MySQL using mysqlimport under MS-Windows command/DOS prompt, try the
following:
mysqlimport --fields-optionally-enclosed-by=""" --fields-terminated-by=, --lines-terminated-by="\r\n" --user=YOUR_USERNAME --password YOUR_DATABASE YOUR_TABLE.csv
Between quotes " and backslashes \ it can really give you a hard time
finding the proper combination under Windows...
I usually run this command from the folder containing the
YOUR_TABLE.csv file.
If you have a header in your .csv file with the name of columns or
other "junk" in it, just add a --ignore-lines=X to skip the first X
lines (i.e. --ignore-lines=1 to skip 1 line)
If your fields are (optionally) enclosed by double-quotes " and which
themselves are doubled inside a value (i.e. a double double-quote "" =
1 double-quote ") then also use --fields-escaped-by=\ (default) and
NOT --fields-escaped-by="""
Working from the Excel end, you can use ADO, for example Excel VBA: writing to mysql database
Dim cn As ADODB.Connection
''Not the best way to get the name, just convenient for notes
strFile = Workbooks(1).FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
''For this to work, you must create a DSN and use the name in place of
''DSNName, however, you can also use the full connection string
strSQL = "INSERT INTO [ODBC;DSN=DSNName;].NameOfMySQLTable " _
& "Select AnyField As NameOfMySQLField FROM [Sheet1$];"
cn.Execute strSQL
After converting to a CSV file, you can import into any database that supports importing from CSV files (MySQL, PostgreSQL, etc.) but you would have to do this from the command-line:
Importing CSV files in PostgreSQL
Import CSV to Oracle table
MySQL 5.1 LOAD DATA INFILE syntax
Import CSV File into MSSQL
You can use a programming language such as Python and use a library for it such as the Excel spreadsheet reading library and then another library for interfacing with your SQL database.
You connect to the data, load the Excel file, and loop through each row and extract whichever column data you want. Then you take that data and execute the INSERT statement.
If you have an installation of phpMyAdmin on a server, you can use the Import from CSV option though you would first have to re-save your Excel spreadsheet as a CSV (Comma-Separated Values) file.

ms-access transfertext not functioning

i have already saved a recent import in data tasks and now i am trying to call it automatically:
DoCmd.TransferText acImportDelim, "import1", "temp", "C:\Documents and Settings\agordon\Desktop\ACTIVITYEX.csv"
the error that i am getting is:
the text file specification "import1" does not exist
does anyone know what this error means?
It's expecting the parameter "Import1" to be a specification name.
If "import1" is not an actual file, then you can just leave this parameter empty and the file should import into table temp (assuming temp is has the same number of fields as your CSV)

Import Tab delimited Text File to Access Database

I have a tab-delimited text file and want to import it in MS Access using VBA code.
I have created an MS Access form and have used the DoCmd.TransferText method:
DoCmd.TransferText(TransferType, SpecificationName, TableName, FileName, HasFieldNames, HTMLTableName, CodePage)
It works well for CSV File. I'm not sure how to do it in case of tab-delimited text files.
Any suggestions?
Do a manual import, changing the delimiter to TAB, save the import spec, and then specify that import spec in your TransferText command.