Thanks for your replyThis isn't so easy as you might think. See this article from Stack Overflow.
![]()
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 ...stackoverflow.com
Or you might research this yourself by doing a web search for "ms access vba import tab delimited file" and look at the articles you drag up. You can do it with an Import Text where you specify that your fields are tab-delimited and the SO article includes screen shots. Doing it via vba code might be a little trickier. The "all files in a particular folder with a particular extension" is EASILY within the abilities of using File System Object to find the folder, generate the collection of matching files, and iterate through the matched files. But that code-based Import of arbitrary files? Not so easy.
Sub SelectFolder()
Dim sFolder As String
' Open the select folder prompt
With Application.FileDialog(4)
If .Show = -1 Then ' if OK is pressed
sFolder = .SelectedItems(1)
End If
End With
If sFolder <> "" Then ' if a file was chosen
' *********************
' put your code in here
' *********************
End If
End Sub
Unless you have to be able to import other tab delimited files on the fly, use the import wizard to define THIS one so you can create an import spec with column names, if they are there and the correct data type for each column.
In the TransferText, select the link option since you don't really want to import the data. Then you can use a TransferSpreadsheet to copy the linked txt file to a spreadsheet.
TWO lines of code unless you need to customize the file names.
Option Compare Database
Dim sFolder As String
Private Sub Command0_Click()
SelectFolder
Dim strFileName As String
'TODO: Specify path and file spec
Dim strFolder As String: strFolder = sFolder
Dim strFileSpec As String: strFileSpec = strFolder & "*.*"
Dim FileList() As String
Dim intFoundFiles As Integer
strFileName = Dir(strFileSpec)
Do While Len(strFileName) > 0
ReDim Preserve FileList(intFoundFiles)
FileList(intFoundFiles) = strFileName
intFoundFiles = intFoundFiles + 1
strFileName = Dir
Dim objExcelApp As Excel.Application
Dim wb As Excel.Workbook
Set objExcelApp = Excel.Application
Set wb = objExcelApp.Workbooks.Open(strFileName)
With wb
ActiveWorkbook.SaveAs FileName:=strFileName & ".xls", FileFormat _
:=xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
False, CreateBackup:=False
objExcelApp.Visible = True
End With
Loop
End Sub
Sub SelectFolder()
' Open the select folder prompt
With Application.FileDialog(4)
If .Show = -1 Then ' if OK is pressed
sFolder = .SelectedItems(1)
End If
End With
End Sub
Connect | Database |
---|---|
Text;DSN=Testtab Link Specification;FMT=Delimited;HDR=NO;IMEX=2;CharacterSet=850;ACCDB=YES; | D:\Dev |
SELECT *
FROM
(SELECT * FROM [TEXT;DSN=Testtab Link Specification;FMT=Delimited;HDR=NO;IMEX=2;CharacterSet=850;ACCDB=YES;DATABASE=D:\Dev].testtab.txt)
AS txt;