Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing part of file name into field in Access table

Status
Not open for further replies.

DataWife

Programmer
Jun 18, 2008
4
GB
I need to import several .csv files into one table in Access, but one of the key fields I need to use to identify the relevant records from the .csv file is only held in the filename and not within the data inside the file. Is there a way to extract part of the filename and add it as a field in Access when the file is imported?
 
Yes ... but you would need to be somewhat pedestrian about how you import the CSV. Normally you would use TransferText to import a CSV file but TransferText won't extract information from a filename and use it as part of the data to be loaded.

The following is just some skeleton code for how I would do it.

Code:
nH = FreeFile
Open "C:\SomeDir\MyFile.CSV" For Input As #nH
DataFromFileName = "???"

Do Until EOF(nH)
   Line Input #nH, Buffer
   Fields = Split(Buffer, ",")
   SQL = "INSERT INTO myTable (fld1, fld2, fld3, ...) " & _
         "VALUES ('" & DataFromFileName & "'," & Fields(0) & ", " & Fields(1) & "," & ...)"
   CurrentDb.Execute SQL
Loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top