Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Line input # not working 1

Status
Not open for further replies.

JoeSmoe55

Programmer
Aug 1, 2005
38
US
I have a CSV file which has several hundred lines of data. However, I use "line input #" command to pull entire string of data. It only returns 4 lines of data before EOF is hit. I thought that "line input went to the end of the line" Here is my code:

Dim text, x As Integer, li As ListItem, ImpFile As Integer
ImpFile = FreeFile
Open fName For Input As #ImpFile

' x = 0
Do While Not EOF(ImpFile) ' Loop until end of file.
' x = x + 1
Line Input #1, text ' Read text data
Debug.Print text
Loop

Where does the debug output go? How do I view it? Is there something small that I am missing? The file is greater than 32k though does that matter? Thanks.
 
Debug.Print sends its output to the Immediate Window

What is the line terminator in the csv file?

The normal end of line marker is Chr(13) + Chr(10) (CarriageReturn/LineFeed or vbCrLf).

From memory VBA wont recognise vbLf as a line terminator by itself, but I think that Unix systems use this as the end of line marker - so if your CSV file is coming from a Unix system that could be the cause of the problem.

Hope this helps.

[vampire][bat]
 
[tt]Line Input #1, text [/tt]

is an error right?, should have been

[tt]Line Input #ImpFile, text [/tt]

though, I'm not particularly fan of the naming convention - isn't text a reserved word (List of reserved words in Access 2002 and Access 2003 and List of reserved words in Jet 4.0)? strText shouldn't break anything ...

Else, I thinke earthandfire is correct with regards to this method and EOL recognizion. I think that was the reason I started using FileSystemObject for such operations.

Roy-Vidar
 
I agree, Roy, however unless the program has any other open files, the call to FreeFile would have returned 1, so ImpFile would almost certainly have a value of 1. As far as using text as a variable name is concerned, again I agree with you, but the impression I got from JoeSmoe55's post was that the code was working (although not as hoped for).


Hope this helps.


[vampire][bat]
 
Hello guys,

First the Impfile, i won't necessarily say it is the same thing, because I am not sure, but I have tried both. I came accross and example and tried it. Like

dim impfile as integer

impfile = freefile

open fName for input as #impfile

I will do some research about the carr/line feed. my assumption was that is was from a windows platform but I will check on that. How would I "help" vba recognize the unix return characters? Would I have to go through the file manually? Thanks guys.

Joe
 
If you find what the terminator or delimitor is try this:

Sub Test()
Dim s As Variant
Dim hdle As Integer
Dim delimter As Variant

Filename = "TheFile.txt" 'might be best to fully qualify the file name.
hdle = FreeFile
Open Filename For Binary As #hdle
delimiter = Chr(13) & Chr(10) 'or you can use vbcrlf or any other delimiter you wish.
Do
s = freadline(hdle, delimiter)
'Debug.Print
Loop Until s = False
Close hdle
End Sub

Function freadline(ByVal hdle As Integer, ByVal delimiter As String) As Variant
On Local Error GoTo freadlineErr
Dim fptr, feof, tempfptr As Long

fptr = Seek(hdle)
feof = LOF(hdle)
If fptr >= feof Then GoTo freadlineErr
Do
stg = Input(1, hdle)
stgLine = stgLine & stg
n = InStr(stgLine, delimiter)
If n <> 0 Then
stgLine = Left(stgLine, n - 1)
Exit Do
End If
fptr = Seek(hdle)
Loop Until fptr > feof
freadline = stgLine
Exit Function
freadlineErr:
freadline = False
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top