NeerDeth
asked on
ReadLine - 1
MS Access:
I'm importing a text file and bring in each line one-by-one. The problem is that I need to check ahead of the line I'm processing so I can be sure what to do with the current line. I want to do a readline command, check that line, and then come back to the original line. The program would be worlds more complicated if I read more than one line in at a time.
Is there a command like ts.readline but move Up the file (one line) instead of Down the file (one line)?
Thanks,
Neer
I'm importing a text file and bring in each line one-by-one. The problem is that I need to check ahead of the line I'm processing so I can be sure what to do with the current line. I want to do a readline command, check that line, and then come back to the original line. The program would be worlds more complicated if I read more than one line in at a time.
Is there a command like ts.readline but move Up the file (one line) instead of Down the file (one line)?
Thanks,
Neer
You simply have to store the current line in a variable and read the next one. Then you can process the lines as required.
Here is some example code:
Public Sub processFile()
Dim fHandle As Long
Dim currentLine As String
Dim nextLine As String
fHandle = FreeFile
Open "c:\test.txt" For Input As fHandle
Input #fHandle, currentLine
Do
Input #fHandle, nextLine
'Actions on current line based on next line
currentLine = nextLine
Loop Until EOF(fHandle)
'Actions that might be required on the last line
Close fHandle
End Sub
Here is some example code:
Public Sub processFile()
Dim fHandle As Long
Dim currentLine As String
Dim nextLine As String
fHandle = FreeFile
Open "c:\test.txt" For Input As fHandle
Input #fHandle, currentLine
Do
Input #fHandle, nextLine
'Actions on current line based on next line
currentLine = nextLine
Loop Until EOF(fHandle)
'Actions that might be required on the last line
Close fHandle
End Sub
To extend that idea, the question was, can I read the file backwards (up instead of down).
The only way I can imagine is to use the FileSystemObject library objects (FileSystem, TextStream) to load the file into an array of strings one line at a time, then use the array index to move around. It's pretty inelegant, but it would enable bi-directional movement around the file. Of course this approach brings its own set of challenges. I've just tested the concept and so I'm confident it should work, but I haven't tested to "implementation quality".
Hope this helps.
The only way I can imagine is to use the FileSystemObject library objects (FileSystem, TextStream) to load the file into an array of strings one line at a time, then use the array index to move around. It's pretty inelegant, but it would enable bi-directional movement around the file. Of course this approach brings its own set of challenges. I've just tested the concept and so I'm confident it should work, but I haven't tested to "implementation quality".
Hope this helps.
Wow. I always forget that there was a time before object libraries when this sort of file manipulation was commonplace.
Hat-tip, heer2351.
Hat-tip, heer2351.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Now that is interesting, I'll have to think about it though.
NeerDeth do you mind explaining why you only give points to Arthur Wood. He describes the technique which I already posted with example code.
ASKER
You're right, I screwed up. Very sorry
I have no problem with the Page Admin changing the award. After all, points are worth the paper they are printed on.
Contact Community Support, explain the error and ask for the error to be corrected.
AW
Contact Community Support, explain the error and ask for the error to be corrected.
AW
ASKER
alright, I'll give that a shot
ASKER
alright, I emailed them and got nothing bad....still waiting.
From the help file:
Reads an entire line (up to, but not including, the newline character) from a TextStream file and returns the resulting string.
Syntax
object.ReadLine
The object argument is always the name of a TextStream object.