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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Text file into listbox with a twist 1

Status
Not open for further replies.

sandman2869

Technical User
Dec 19, 2001
15
0
0
US
I am reading a text file into a listbox and I want every line of text show except for the very last line in the text file, what I want to do with the last line is this: I want it to read it in to memory (for later use) and then delete it....after I am finished writing the rest of my text back to the textfile (in append mode) I want to copy the original last line back into the text file. Being such a novice in VB is very hard, is this even possible or am I following a pipe dream?
 
Hi try this .. .. you will have to change the file names.
John

Sub readText(sMode As String, sFileName As String)
' ********************************************
' MODE OPERATION
' ---- ---------
' READ Read from sFileName to Text box
' Saving but not displaying the last line.
' WRITE Write to sFileName from the txtBox
' and include the last line.
' *******************************************
' Read Example: call readText("READ","C:\NewText.txt")
' Write Example: call readText("WRITE","C:\NewText.txt")
' *******************************************************
Select Case UCase(sMode)
Case "READ"
Dim sReadLine As String
Dim sTmp As String
Dim sLastLine As String
Dim bFlg As Boolean
' **** clear text box
frmMain.txtMain.Text = ""
' **** open text file for input
Open sFileName For Input As #1

Do Until EOF(1)
Line Input #1, sReadLine
If Not EOF(1) Then
sTmp = sTmp & sReadLine & vbCrLf
Else
sLastLine = sReadLine
End If '»If Not EOF(1) Then
Loop '»Do Until EOF(1)
Close #1
' *** show in text box
frmMain.txtMain.Text = sTmp
' ********************************
' Save your last line (sLastLine)
Open "C:\LastLine.txt" For Output As #1
Print #1, sLastLine
Close #1
' ********************************
Case "WRITE"
Kill sFileName
Open sFileName For Input As 1#
Open "C:\LastLine.txt" For Input As #2
Print #1, Trim(frmMain.txtMain.Text)
Line Input #2, sReadLine
Print #1, sReadLine
Close #1
Close #2
Kill "C:'\LastLine.txt"
End Select '»Select Case UCase(sMode)
End Sub
 
IFELSETHEN
Thank you VERY much for the response, your code does what I was looking for, except it is reading the entire text file as one line for some reason, am I doing something wrong?

Thank you again!
 
Hi,
The 'Line Input' reads until it finds a CRLF. What type of text is in your text box? You must have noticed that I didn't save the last line to a database, but created a text file to save the last line into.

John
 
just a txt file with lines such as:

hello
how
are
you

but in the textbox it reads:
hellohowareyou
 
See the line ' sTmp = sTmp & sReadLine & vbCrLf' that adds a CRLF to the string. Do you have the multi line option turned on? Let me know. I have a few minutes this morning. Ask more questions if you want.

John (Las Vegas,Nv)
 
John,

I just wanted to say a big thank you for taking the time out of your day to help me out. It really is appreciated very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top