Boy, I'm on a roll with screwups on this answer.
Please ignore the previous message.
Here is the complete and corrected code. You will need to add these two
Enumerations to the declarations section of your module or make necessary
changes.
HS
---------Begin Code ------------
Enum StatusCode
Unknown = -1
Failure = 0
Success = 1
End Enum
Enum ID_ImportType
ImportDelimited = 0
ImportFixedWidth = 1
End Enum
'-----------------------------
Function ImportTextFile( _
ImportType As ID_ImportType, _
ImportSpec As String, _
DestinationTable As String, _
StrFileName As String, _
Optional SkipRows As Integer = 0) As StatusCode
' Imports a delimited or Fixed-width text file to specified table.
' Uses import export specifications
' Optionally Use SkipRows. By default, no rows are skipped
' uses a hardcoded temporary file to copy data -
' change path of temp file below. Can also add it as another parameter
On Error GoTo ImportTextFile_err
Dim fso, srcFile, DestFile ' Objects
Dim strMsg As String, Currline As String, i As Integer
Const ForReading = 1 'FSO Constants
Const ForWriting = 2
' Use a temporary file for the import operation
Const DestFileName = "C:\temp\Datafile.txt"
' Handle inputs
StrFileName = Trim(StrFileName)
DestinationTable = Trim(DestinationTable)
'instantiate file system object. We are using late binding here.
Set fso = CreateObject("Scripting.FileSystemObject")
'if sourceFile doesnt exist, quit now, returning a failure value
If Not fso.FileExists(StrFileName) Then
strMsg = "Sorry, the path and filename you entered is invalid. " &
vbCrLf
strMsg = strMsg & "Please check and reenter"
MsgBox strMsg, vbExclamation + vbOKOnly, "Check File Name"
ImportTextFile = Failure
Set fso = Nothing
Exit Function
End If
'create a new temp File - previous instances are automatically overwritten
Set DestFile = fso.opentextfile(DestFileName, ForWriting, True)
' open the source file in readonly mode
Set srcFile = fso.opentextfile(StrFileName, ForReading)
While Not srcFile.AtEndOfStream
i = i + 1 ' count lines
If i <= SkipRows Then
' just read the line to get move the pointer
Currline = srcFile.Readline
Else
DestFile.WriteLine (srcFile.Readline)
'Currline = srcFile.Readline
'DestFile.WriteLine Currline
End If
Wend
'close files and cleanup objects
srcFile.Close
DestFile.Close
Set srcFile = Nothing
Set DestFile = Nothing
Set fso = Nothing
' Now import the file
DoCmd.TransferText ImportType, ImportSpec, DestinationTable, DestFileName
'Return a success code to calling procedure
ImportTextFile = Success
Exit Function
ImportTextFile_err:
' Return failure status and show error desc
MsgBox "Err # " & Err.Number & vbCrLf & Err.Description, vbCritical +
vbOKOnly, "MdlCommon.ImportTextFile Error!"
ImportTextFile = Failure
Exit Function
End Function
----------End Code--------------