import txt files in Access fails to read last line 
Author Message
 import txt files in Access fails to read last line

I have a VB routine to import textfiles into an Acess 2000 database.
Problem is the <EOF> command stops at the last carriage return in the text
file and missing the line after that (the last line) (which has quit often
no carriage return at the end)

Any idea how to force it to read all the caracters in the text file?

--
Regards,
Beert Schrijver



Thu, 17 Nov 2005 01:40:55 GMT  
 import txt files in Access fails to read last line

12inter.net says...

Quote:
> I have a VB routine to import textfiles into an Acess 2000 database.
> Problem is the <EOF> command stops at the last carriage return in the text
> file and missing the line after that (the last line) (which has quit often
> no carriage return at the end)

> Any idea how to force it to read all the caracters in the text file?

You should post the code that handles the eof condition of the file.
Depends on the looping code you use if it handles the last line or not.
As you can see, when importing text files there are few gotchas that you
have to deal with.

Linefeed codes are sometimes LineFeed, sometimes Carriage Return,
sometimes both. Some lines may be blank. Last line may contain a end of
line, or may not, etc etc etc.

--

Remove NOT from email address to reply. AntiSpam in action.



Thu, 17 Nov 2005 03:26:35 GMT  
 import txt files in Access fails to read last line

Quote:
> You should post the code that handles the eof condition of the file.
> Depends on the looping code you use if it handles the last line or not.
> As you can see, when importing text files there are few gotchas that you
> have to deal with.

code is:

in the module:
==========

Property Get EOF() As Boolean
    If Me.IsOpen Then
        EOF = VBA.EOF(Me.Handle)
    End If
End Property

in the form:
========

Private Sub cmdReadAll_Click()
  Dim MyName As String
  Dim MyDB As Database
  Dim rs As Recordset
  Dim objFile As clsTextFile

'open de poemtabel voor lezen en schrijven
  Set MyDB = CurrentDb()
  Set rs = MyDB.OpenRecordset("Poem", dbOpenDynaset)
  rs.MoveFirst

'1st record in dir
  MyName = Dir(MyPath)

  Do While MyName <> ""
    If InStr(1, MyName, ".txt") > 0 Then
'get the ID
      rs.FindFirst "[ID]=" & Left(MyName, InStr(1, MyName, ".") - 1)
'zoek het bijbehorende record in de tabel poem
      If Not rs.NoMatch Then
'open file
        Set objFile = New clsTextFile
        objFile.Path = MyPath & MyName
        If objFile.FileOpen() Then
          With rs
            .Edit
            Do Until objFile.EOF
              !txtPoem = !txtPoem & objFile.Text & vbCrLf
              objFile.ReadNext
            Loop
            objFile.FileClose
'write file to tabel
            .Update
          End With
        End If
        Set objFile = Nothing
      Else
'open debugwindow  (Ctrl-G)
        Debug.Print "Fout: " & Left(MyName, 1)
      End If
    End If
'next file
    MyName = Dir
  Loop

  Me.Subformulier_Poem.Requery
  Me.cmdReadAll.Caption = "R E A D Y !!"
End Sub



Thu, 17 Nov 2005 03:45:06 GMT  
 import txt files in Access fails to read last line
I am guessing but:

     Do Until objFile.EOF
        !txtPoem = !txtPoem & objFile.Text & vbCrLf
        objFile.ReadNext
     Loop

When you do the ReadNext, the "text" buffer is loaded, and eof becomes
true!. So, your code will always miss the last line. You need to add one
line code to grab that Left over line. So, you actually need:

     Do Until objFile.EOF
        !txtPoem = !txtPoem & objFile.Text & vbCrLf
        objFile.ReadNext
     Loop

     if len(objfile.txt) > 0 then
        !textPoem = !txtPoem & objFile.Text
     endif

--
Albert D. Kallal     (MVP)
Edmonton,  Alberta Canada

http://www.attcanada.net/~kallal.msn



Thu, 17 Nov 2005 05:54:29 GMT  
 import txt files in Access fails to read last line
That was a very good guess.
Thank you !



Quote:
> I am guessing but:

>      Do Until objFile.EOF
>         !txtPoem = !txtPoem & objFile.Text & vbCrLf
>         objFile.ReadNext
>      Loop

> When you do the ReadNext, the "text" buffer is loaded, and eof becomes
> true!. So, your code will always miss the last line. You need to add one
> line code to grab that Left over line. So, you actually need:

>      Do Until objFile.EOF
>         !txtPoem = !txtPoem & objFile.Text & vbCrLf
>         objFile.ReadNext
>      Loop

>      if len(objfile.txt) > 0 then
>         !textPoem = !txtPoem & objFile.Text
>      endif

> --
> Albert D. Kallal     (MVP)
> Edmonton,  Alberta Canada

> http://www.attcanada.net/~kallal.msn



Thu, 17 Nov 2005 06:05:42 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Reading the last line of a file

2. read last line of a file

3. Reading the last few lines only from a text file

4. Access 97 import large txt file

5. NEWBIE, URGENT:Please Help : Importing TabDelim txt file into Access Database

6. Importing a Fixed Width *.txt file to a Access DB

7. Problem Importing txt files into Access

8. last Line of .txt

9. retrieving txt file contents line by line

10. retrieving txt file contents line by line

11. Reading a txt File into Access

12. Reading a txt File into Access

 

 
Powered by phpBB® Forum Software