Thanks,
Scott
"Scott" <sco...@godingers.com> wrote in message
news:eXmzDKrg...@TK2MSFTNGP09.phx.gbl...
HTH
Van T. Dinh
MVP (Access)
>.
>
HS
"Van T. Dinh" <VanThi...@PlseUseNewsgroup.bigpond.com> wrote in message
news:16d901c3831b$2ef723d0$a301...@phx.gbl...
Here is the code I'm using:
*********
Dim strLine1 As String
Dim strLine2 As String
Dim strLine3 As String
Dim strLine4 As String
Dim strLine5 As String
Dim strLine6 As String
Dim strLine7 As String
Dim strLine8 As String
Dim strLine9 As String
Dim strLine10 As String
Dim strLine11 As String
Dim strLine12 As String
Dim strLine13 As String
Dim strLine14 As String
Dim strLine15 As String
Dim rst As DAO.Recordset
Dim FilesDir As String
Set rst = CurrentDb.OpenRecordset("MyTable")
FilesDir = Dir(Me.FilePath)
Do While FilesDir <> ""
Open Me.FilePath & FilesDir For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
Select Case gblSpec
Case 7
Input #1, strLine1, strLine2, strLine3, strLine4, strLine5,
strLine6, strLine7, strLine8, strLine9, strLine10, strLine11, strLine12,
strLine13, strLine14, strLine15
rst.AddNew
rst!MyField1 = Trim(strLine13)
rst!MyField2 = YYYYMMDD_To_Date(Left(strLine2, 8))
rst!MyField3 = Trim(strLine5)
rst!MyField4 = Trim(strLine6)
rst!MyField5 = gblMyVariable .
rst!MyField6 = Date .
rst.Update
End Select
Loop
Close #1
***************************************************
The above code was working good when I had no field header, but now I need
to start importing from the second line that contains the actual data and
not the first line, how do I do that.?
Thanks,
Scott
"HSalim" <OnlyIfYouM...@msn.com> wrote in message
news:Op1Y8Bx...@TK2MSFTNGP12.phx.gbl...
If MyCount = 1 then
'do nothing
Else
'do something
End if
>.
>
The following is pasted from the Access help system.
"The Line Input # statement reads from a file one
character at a time until it encounters a carriage return
(Chr(13)) or carriage return-linefeed (Chr(13) + Chr(10))
sequence. Carriage return-linefeed sequences are skipped
rather than appended to the character string."
Therefore, you should invoke this command once,
immediately before the start of the loop, and let your
remaining code (within the loop) read in the data -
starting at line 2.
Regards,
Peter.
>.
>
"HSalim" <OnlyIfYouM...@msn.com> wrote in message
news:OnruOGyg...@TK2MSFTNGP10.phx.gbl...
The attached code will help you import data into a specified table using
import-export specifications.
HS
'------Begin Code ----------------------------------------------
Function ImportFile( _
ByVal StrFileName As String, _
ByVal ImportSpec As String, _
ByVal DestinationTable As String, _
Optional ByVal SkipRows As Integer = 0) As StatusCode
' Imports a specified 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 ImportFile_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)
'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"
ImportFile = 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 acImportDelim, ImportSpec, DestinationTable, DestFileName
'Return a success code to calling procedure
ImportFile = Success
Exit Function
ImportFile_err:
' Return failure status and show error desc
MsgBox "Err # " & Err.Number & vbCrLf & Err.Description, vbCritical +
vbOKOnly, "MdlCommon.ImportFile Error!"
ImportFile = Failure
Exit Function
End Function
'----------------End Code----------------------
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
' 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--------------
"HSalim" <OnlyIfYouM...@msn.com> wrote in message
news:%23L$KfT7gD...@TK2MSFTNGP10.phx.gbl...
Scott
"HSalim" <OnlyIfYouM...@msn.com> wrote in message
news:ug1urY8g...@tk2msftngp13.phx.gbl...