VBA Importing long file names - csv

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

Related

How to remove double quotes from a CSV file that generated in Access by DoCmd.TransferText acExportDelim

I am using DoCmd.TransferText in MS Access to convert data from a query to a CSV file but there are double quotes around each field. The csv file will be used by an application and do not accept the format.
I know I can do it manually by Access Export wizard and set "Text Qualifier" to 'None' for removing double-quotes, but I need to do it in VBA. I would really appreciate if anyone can help me to do it with VBA
Here is the codes that I am using:
DoCmd.TransferText acExportDelim, , "tbl_ScrubCSV", "P:\0TPSScrub\" & Instrumentnu & ".csv", False
Expected result should be like:
E0A,M.V. KULTUS COVE,.,,,CA
but the CSV file exported like:
"E0A","M.V. KULTUS COVE",".","","","CA"

Access DoCmd.TransferText Specification Argument Location

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

MS Access unable to locate my excel range when importing

I have this code that works on one spreadsheet, but not another. I am just trying to automate the transfer of an excel data range to an access table, like so
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "UsysFastTrack", strFilePath, False, strRange
strFilePath and strRange are just strings that contain the full file path (including the worksheet name and extension) and the name of an excel range in the worksheet, respectively. This line causes an error
The Microsoft Access database engine could not find the object ...
This error appears a lot online and somewhere I saw the advice to try the import wizard to see what I get and lo and behold, right as I hit the last Next
This is the exact same error and it stops me dead in my tracks. What's funny is that you can see the range exists in the spreadsheet before your very eyes.
What's going on here?
Looks like the names of your named ranges are not valid, they are similar to regular ranges. Try to change names

import tab-delimited txt into Access table using VBA

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

Microsoft Access TransferText function: problem with codepage

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