Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 494
  • Last Modified:

Importing text file to Access - ADO preemptively interprets a text column as date datatype

We have an Access application with a SQL Server database for a testing laboratory. We are importing text files from an Agilent ICPMS instrument (sample attached). The Sample Name column is a text field of the format wwww-xxx-y. That matches a primary key in a main table in the SQL Server database. Examples:
1409-002-01, 1409-013-1, 1409-096-1. The first two look like dates to ADO and it reformats the data as such.

We're using this connection info:
    objconnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPathToTextFile & ";Extended Properties=""Text;HDR=YES;FMT=Delimited"""
    objRecordset.Open "SELECT * FROM [" & ThisFileName & "]", objconnection, adOpenStatic, adLockOptimistic, adCmdText 'select all text lines from the file

I have tried renaming the file as .txt. Same problem.

How can I force ADO to NOT HELP with interpreting the Sample Name values as dates?
G--GSAJDrive-InstrumentOutputFiles-ICPMS
0
Douglass MacLean
Asked:
Douglass MacLean
  • 5
  • 4
4 Solutions
 
Rey Obrero (Capricorn1)Commented:
have you tried using "Import Specification" to import the text file?


 To create the Import Specification
 1) Click on external data> text file which then opens another window called "Get external data - Text file"
 2) Use radio button to select "Import the source data into a new table in the current database"
 3) specify the source of the file using the browse procedure then click OK
 4) Choose radio button to select delimited format and then click next
 5) this window allows you to choose delimiter and text qualifier and if first row contains names - click next
 6) This window allows you to type the name of the field in the Field Name column, choose data type, and if you want field indexed - also can choose to skip field - then click next
 7) This window allows you to add primary key or designate field as such - click next
 8) Click on ADVANCED button
 9. in the import specification window
 type the name of the field in the Field Name column
 (here you can use the field names of the destination table, specify data type,
 check the box Skip if you do not want to import the column)
 10 click save as, give the specification a name <-- this is the specification name that you will use in the command line below

 DoCmd.TransferText acImportDelim, "ImportSpecificationName", "TableName", Returnvalue, blnHasFieldNames
0
 
Douglass MacLeanCTOAuthor Commented:
Interesting idea, Rey,

I'll try it and let you know. Thanks
Doug
0
 
Douglass MacLeanCTOAuthor Commented:
OK. The good news is that it produces the right results.

Now, the trick is that my VBA code loops through all the text files in a specified folder and imports each of them. So I need to dynamically modify the import specification details on the fly.

How can I find that actual import specification that I created and turn it into VBA code that I can make dynamic?

The context is that the user clicks a button that says "Process Files". The user's only responsibility is to have put one to n files from the ICPMS instrument into a specified folder ready to be processed.
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
Rey Obrero (Capricorn1)Commented:
you will need one specification file for "ALL" text file that have the same format.

how many different file format do you have?
0
 
Douglass MacLeanCTOAuthor Commented:
One format, exactly like the sample I attached
0
 
Rey Obrero (Capricorn1)Commented:
you can reuse the ImportSpecification for all your files, using this format

DoCmd.TransferText acImportDelim, "ImportSpecificationName", "TableName", strPathToTextFile & "\" & filename , True
0
 
Douglass MacLeanCTOAuthor Commented:
OK. That's good.
So, the import spec I saved is buried in the .accdb file somewhere and I don't need to see it and deal with it directly. Right?
0
 
Rey Obrero (Capricorn1)Commented:
that is correct, you just need the name of the import spec.
0
 
Douglass MacLeanCTOAuthor Commented:
This is a great solution. Thanks much for your guidance.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now