I am trying to import a tab-delimited txt file into an Access table using VBA. In my code, I want to insert it into a table that has not yet been created.
Here is what I tried doing. Note - I was able to make this work with a CSV, and without including this: DataType:=xlDelimited, Tab:=True
Sub InsertData()
'import CSV into temp table
DoCmd.TransferText TransferType:=acLinkDelim, TableName:="tbl_TEMP", _
FileName:=FileNameVariable, HasFieldNames:=True, DataType:=xlDelimited, Tab:=True
End Sub
When I run this block, I get the following error on DataType:=xlDelimited, Tab:=True
Compile error: Named argument not found
How should I change this in order to pull in the tab-delimited txt file so each column from the txt has its own column in Access?
As you have seen from the other articles on the topic, there really isn't a generic way to import tab-delimited text files. All of the other solutions I've seen say that you should import the tab-delimited text file once, save the import specification, and then use that import specification for all subsequent imports. The problem there is that if you want to import a different tab-delimited file the specification may not match.
The only way I've found to do it generically (short of "rolling your own" code using FileSystemObject, Split(s, vbTab), etc.) is to create a completely generic specification for all 255 possible fields and use that. It requires a one-time setup as follows:
Copy the CSV data from the Pastebin here, paste it into your favorite text editor, and save it as GenericTabSpecification.csv.
Open that file in Excel, select all 256 rows and 4 columns, then hit Ctrl+C to copy.
In Access, start the import wizard for text files and choose any tab-delimited file. (We won't actually be importing it.)
When you get to the first page in the wizard, click the "Advanced..." button.
In the Import Specification dialog, verify the settings (Field Delimiter, Text Qualifier, etc.) then click the top-left corner of the Field Information grid so all rows are selected:
Hit Ctrl+V to paste the data from Excel into the grid. The grid should now contain 255 rows.
Click the "Save As..." button and name the specification GenericTabSpecification. Once that is done, cancel out of the wizard.
Now we can do a generic import from VBA using a statement like this
DoCmd.TransferText _
TransferType:=acImportDelim, _
SpecificationName:="GenericTabSpecification", _
TableName:="newTable", _
FileName:="C:\Users\Gord\Desktop\foo.txt", _
HasFieldNames:=False
Related
The following lines of code should export data from a query to a *.txt file, but results in an error.
strPath = "N:\apprais\targetDirectory\"
DoCmd.TransferText acExportDelim, "spcDataFile", "qryExportAppraisal", strPath & "DATAFILE.TXT"
Based on this https://msdn.microsoft.com/en-us/library/office/ff835958.aspx, I don't think it should be necessary to have a Schema.ini file (unless it's a fixed-width file). Also when I executed the command in an older version of the application, I was able to export the data from the query to the *.txt file without the Schema.ini file.
Ultimately, I need to know if I'm correct about the Schema.ini, and if I am, is there a default location that specification files are saved to in Access 2010? I can't seem to find where it's defined. I've looked in External Data > Saved Imports/Exports, but am not able to find anything resembling spcDataFile
I have a .csv file on my Desktop. The file name is very long and includes special characters such as brackets, e.g.:
[ABCD 012015] ACCT 1117 - Section A10 Grades-20150316_1937-comma_separated.csv
where the first 36 characters of the filename are a constant. The remainder of the file name changes upon each instance of a download.
I have tried VBA to import the file into an Access with a DoCmd.TransferText and I get errors. I have discovered that there is a limit to the length of a filename that the DoCmd.TransfertText can handle.
So, I need to first rename the file manually, then I can use VBA. Renaming a file is relatively easy with VBA, but I would like to use the info provided in the original filename such as Section A10 (which would make it unique, since there are other Sections labelled A01, A02, etc.) and rename the .csv file as A10.csv, so this means searching for a string AND replacing it. Since the Section can be different, how do I write the code and rename the file then import it with VBA?
So, far I have bits and pieces, but cannot put them together:
Name OldPathName As NewPathName
DoCmd.TransferText acImport, "AME_Grades", strTable, strPathFile, blnHasFieldNames
I am using an import specification AME_Grades to make it cleaner in the Access table.
Any suggestions? TIA
I'd suggest to use FileCopy function before you start importing data from csv file into MS Access.
Dim OldPathName As String, NewPathName As String
OldPathName = "FullPathToVeryVeryLonLongLongFileName.csv"
NewPathName = "FullPathToShortFileName.csv"
FileCopy OldPathName, NewPathName
DoCmd.TransferText acImport, "AME_Grades", strTable, NewPathName, blnHasFieldName
There are over 210,000 records in a txt file. One record has three columns with space split. To import this file into ms access, system replace spaces into comma. And then using insert into, system imports all at once. System needs to sort and search with these records. The whole processing time is so slow. It takes over 15 minutes. How to speed up?
Is it able to handle over 210,000 records in a table of ms access
2003?
Without replacing comma, could we import this txt file into access?
Thank you.
Access can import a text file which uses spaces instead of commas as the delimiter. On the first page of the Access 2003 "Import Text Wizard", choose the radio button next to "Delimited - Characters such as comma or tab separate each field". Click "Next", and on the next wizard page, select the radio button next to "Space" under the "Choose the delimiter that separates your fields" heading.
You can save your import choices as an "Import Specification" by click the "Advanced..." button to bring up the Import Specification dialog, then clicking the "Save As..." button on the right. Assign a Specification Name in the "Save Import/Export Specification" dialog and click OK.
If you want to do this type of import operation with code, you can use the TransferText method:
DoCmd.TransferText acImportDelim, "Your Named Specification", _
"Destination Table Name", "C:\somepath\yourfile.txt"
See Access' help topic for more details about the TransferText method.
I noticed your question includes a vb6 tag, but don't know how that fits in. Seems to me that using TransferText from VBA to import a text file of 210K rows of 3 columns should not take anywhere near 15 minutes.
I have written a simple web service that returns large volumes of csv data. I will to import this into Excel in a tabular format using Excel's "Data From Web" function.
Is there a way to get Excel to automatically parse the csv fields returned into individual columns as part of the import operation?
At present the only means I have for doing this is to first import the data into a single column and then write VBA code to select the data and split it using TextToColumns. This feels messy / error-prone.
The other alternative I have is to modify the web server to serve back the data as HTML. However, I'm reluctant to do this as adding tags around each csv field will greatly impact the volume of data returned.
Adamski,
Here is something that I use. I found the core somewhere on the internet, but don't know where.
What it does is it opens a tab separated file and reads the data in an excel sheet
If Answer1 = vbYes Then 'I asked prior if to import a tab separated file
Sheets("ZHRNL111").Select 'Select the sheet to dump the data
On Error Resume Next
With ActiveSheet
If .AutoFilterMode Then .ShowAllData 'undo any autofilters
End With
Sheets("ZHRNL111").Cells.Clear 'remove any previous data
On Error GoTo 0
Range("A1").CurrentRegion.Delete
Fname = MyPath & "\LatestReports\Report-111.tsv"
Open Fname For Input As #1
iRow = 1
Line Input #1, Record
On Error Resume Next
Do Until EOF(1)
P = Split(Record, vbTab)
For iCol = 1 To 14
Cells(iRow, iCol) = P(iCol - 1)
Next iCol
iRow = iRow + 1
Line Input #1, Record
Loop
On Error GoTo 0
Close 1
End If
Regards,
Robert Ilbrink
Depending on the version of excel you are running you should be able to open the .csv in excel and use the text to columns feature built into excel.
Also, if you could modify your csv to split columns based on commas "," instead of tabs excel would open it directly without the need to format it.
I know however this can sometimes be a problem depending on the data you are importing because if the data contains a comma it must be inside quotations.
In my experience the best way is to use quotations on every field if possible.
Hope this helps.
I am actually creating a product right now to do this in both XML and JSON for Excel. I know comma delimited does work in Excel, with some caveats. One way around it is to put some "" around the text in between the delimiters for the "Data From Web" feature. There are still issues with that however. I did find that despite it's increased size, XML was the best option for quick turn around. I was able to create the service and hand my project manager the Excel document which he could update at anytime.
I inherited a huge, bulky MS Access database and am assigned to solve a problem in it. The problem is as follow...
System A exports its data to a pipeline-delimited .txt file. The files has special characters working correctly, for example the value "Müller" shows when opening this file in notepad or Excel.
Next, the Access DB imports the .txt file and stores the result in an internal employees table. The last name field is of data type "memo". The method to import data from the .txt file to MS Access is as follow:
Call DoCmd.TransferText(acImportDelim, _
"tblEmployees", _
"tblEmployees", _
me.txtImportFile, _
True)
After running this import and viewing the employees table I noticed that names with special characters are screwed up. "Müller" becomes "M├⌐ller" for example. I investigated some online help and found out that can include a "codepage" parameter in the TransferText call, so I set it to 65001 (which appearantly is the codepage for unicode):
Call DoCmd.TransferText(acImportDelim, _
"tblEmployees", _
"tblEmployees", _
me.txtImportFile, _
True, _
, _
65001)
Now that I have ran the import script again, I see no difference whatsoever, the special characters are still misformed. I'm running out of steam so I hope one of you has some advise on how to resolve this...
Both versions of your TransferText operation are using a SpecificationName named tblEmployees. What Code Page is specified in that Specification?
Try importing the text file manually. Choose "Advanced" from the Import Text Wizard. Then select Unicode in the Code Page list box. You may need to test with different Code Page selections until you find which one imports your text correctly.
Which ever Code Page selection works, save your choices as a specification and use it in your TransferText command, without supplying a separate CodePage parameter.
Using CodePage=1200 (msoEncodingUnicodeLittleEndian) solved the issue in my case.
there is an unicode list to use in VBA:
http://msdn.microsoft.com/en-us/library/office/aa432511(v=office.12).aspx