Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 MS Access
 Automate the Import process in Access

Author  Topic 

jrdatla
Yak Posting Veteran

51 Posts

Posted - 2002-04-11 : 15:05:22
Hello:

I am new to MS Access.

Can any one let me know the method of automating process for the import of data from number of text files in a folder into an MS Access table.

All the text files are comma separated value files.

Every day a number of text files were created in a Folder X. The Process should read all the text files from folder X and load them into the MS Access table.

Thanking you

Jr. Datla

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2002-04-12 : 06:31:30

The command you're looking for is DoCmd.TransferText. It imports and exports data to/from text files. An example of this is:

DoCmd.TransferText acImportDelim,,"Table1","C:\Data\Table1.csv"

This imports the csv file C:\Data\Table1.csv to the table Table1.

There's loads more you can do with it - check out the Microsoft site for more info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D2/S5A246.asp





Go to Top of Page

SMerrill
Posting Yak Master

206 Posts

Posted - 2002-05-21 : 16:26:35
Access 97 and above:
You can also attach to a CSV file as if it were a table. That way, you do not have to import it at all! I know this is a little more advanced of a technique, but it should reward you greatly in execution speed.
First, you need to do a manual attachment. Use File|Get External Data|Link Tables and set the Files of type combo box to "Text Files (*.txt, *.csv, *.tab, *.asc)".
Then find the text file and attach to it.
This will do two things; (1) the table will show up in your tables list with a Notepad-looking icon, and (2) you will get an import/export specification.
So open the new attached table in design view and show the properties. You will see a connection string. Copy the connection string in it's entirety and close the table. Then go to this code piece below and paste the connection string in as a comment line above the line that I bolded the code. Notice that the concept of "Database" correlates to the folder that the file is in, and the concept of "Table" correlates to the CSV files themselves.


Public Function LinkToCSVFile(strFileName As String, _
strLinkName As String) As Boolean

'PURPOSE: Creates an attached table from a CSV text file.
'Returns True if error, False if no error

Dim db As Database
Dim tdf As TableDef
Dim fs As New FileSystemObject
Echo True, "Linking to file """ & strFileName & """"
LinkToCSVFile = False 'No error condition
If Not fs.FileExists(FileSpec:=strFileName) Then
Err.Raise vbObjectError + 3, , "The file does not exist."
End If

Set db = CurrentDb()
With db
On Error Resume Next
If .TableDefs(strLinkName).Name = strLinkName Then
'A quick test to see if the table exists
.Execute "DROP TABLE " & strLinkName
.TableDefs.Refresh
End If
On Error GoTo ErrHandler
End With

Set tdf = db.CreateTableDef(strLinkName)
With tdf
.Connect = "Text;DSN=" & strLinkName _
& " Link Specification;FMT=Fixed;HDR=NO;IMEX=2;" _
& "DATABASE=" & fs.GetParentFolderName(Path:=strFileName) & ";"


.Name = strLinkName
.SourceTableName = fs.GetFileName(Path:=strFileName)
End With

On Error Resume Next
DoCmd.TransferText 'Workaround for Access97 bug -- _
If this is not done, the _
"MaxData Link Specification" IMEX spec _
may or may not be visible to Access.
On Error GoTo ErrHandler

With db.TableDefs
.Append tdf
.Refresh
End With

ExitHere:
Echo True, ""
Set tdf = Nothing
Set db = Nothing
Set fs = Nothing
Exit Function

ErrHandler:
Select Case MsgBox(Err.Description, vbAbortRetryIgnore)
Case vbAbort
LinkToCSVFile = True 'Error condition
Resume ExitHere
Case vbRetry
Resume
Case vbIgnore
Resume Next
End Select
End Function

Compare your pasted comment line to the code in my module, and you will notice
that if your file name is Filename.CSV, you do not have to put "FILENAME#CSV" at the end of the DATABASE= spec.

Finally, to get this to compile, you will have to include a reference to the Microsoft Scripting Runtime (SCRRUN.DLL).

If you find you must tweak the columns a bit, tweak them in the Advanced section of the manual import. This memorizes them into the Import Specification for that filename.
--SMerrill
Seattle, WA

Edited by - smerrill on 05/21/2002 16:32:02

Edited by - smerrill on 05/21/2002 16:36:02
Go to Top of Page
   

- Advertisement -