All working beautifully now:
Public Function FileFromPath(ByVal strFullPath As String, _
Optional bExtensionOff As Boolean = False) _
As String
Dim FPL As Long 'len of full path
Dim PLS As Long 'position of last slash
Dim pd As Long 'position of dot before exension
Dim strFile As String
On Error GoTo ERROROUT
FPL = Len(strFullPath)
PLS = InStrRev(strFullPath, "\", , vbBinaryCompare)
strFile = Right$(strFullPath, FPL - PLS)
If bExtensionOff = False Then
FileFromPath = strFile
Else
pd = InStr(1, strFile, ".", vbBinaryCompare)
FileFromPath = Left$(strFile, pd - 1)
End If
Exit Function
ERROROUT:
On Error GoTo 0
FileFromPath = ""
End Function
Sub Text2Access(strTextFile As String, _
Optional strMDBPath As String, _
Optional strTableName As String, _
Optional bLink As Boolean)
Dim appAccess As Object
Dim lImportType As Long
If Len(strMDBPath) = 0 Then
strMDBPath = Replace(strTextFile, Right$(strTextFile, 3), "mdb", 1, 1)
End If
If Len(strTableName) = 0 Then
strTableName = FileFromPath(strTextFile, True)
End If
If bLink Then
lImportType = 5
Else
lImportType = 0
End If
Set appAccess = CreateObject("Access.Application")
With appAccess
.NewCurrentDatabase strMDBPath
.DoCmd.TransferText lImportType, , strTableName, strTextFile, True
.CloseCurrentDatabase
.Quit
End With
Set appAccess = Nothing
End Sub
Sub tester()
Dim strTextFile As String
strTextFile = "C:\TempTables\ResultFile.txt"
Text2Access strTextFile, , , True
End Sub
RBS
"Dirk Goldgar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "RB Smissaert" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)
>> Want to automate this simple sequence from Excel VBA:
>> Start Access, but no need to be visible.
>> File
>> Open text file.
>> Comma delimited, first row are field names.
>> Save as .mdb in specified folder.
>>
>> Seems simple, but I can't see anything that does it.
>> I understand that unfortunately Access doesn't have the option to
>> record a macro.
>>
>> Thanks for any advice.
>>
>> RBS
>
> Try something like this:
>
> '----- start of example code -----
> Dim strTextFilePath As String
> Dim strMDBPath As String
> Dim strTableName As String
> Dim I As Integer
>
> Dim appAccess As Object
>
> '*** Here, I've hard-coded the name\path of the file to be imported.
> strTextFilePath = "C:\Temp\TestImport.csv"
>
> strTableName = Dir(strTextFilePath) ' initially
>
> ' Allow for possible file name without extension.
> I = InStrRev(strTextFilePath, ".")
> If I = 0 Then
> strMDBPath = strTextFilePath & ".mdb"
> Else
> strMDBPath = Left(strTextFilePath, I - 1) & ".mdb"
> strTableName = _
> Left(strTableName, InStrRev(strTableName, ".") - 1)
> End If
>
> Set appAccess = CreateObject("Access.Application")
>
> With appAccess
> .NewCurrentDatabase strMDBPath
> .DoCmd.TransferText 0, , strTableName, strTextFilePath, True
> .CloseCurrentDatabase
> .Quit
> End With
>
> Set appAccess = Nothing
> '----- start of example code -----
>
> --
> Dirk Goldgar, MS Access MVP
> www.datagnostics.com
>
> (please reply to the newsgroup)
>
>