
Signature
Working without a .NET?
http://classicvb.org/
G'Day
I started looking at what you meant by "determining" why the text file ends
up on one line. Inside the text file, the entire list ends up enclosed
inside quotation marks:
i.e.
"DO NOT WRITE ON THIS LINE!
12R-DJDS-4045T
4HT-DDS-4-2011
"
The above example shows how the quotation marks end up inside the text file.
This is the script to "write to the text file";
Private Sub WriteMe_Click()
Dim MyData As String
Dim i As Integer
For i = 0 To ListBox.ListCount - 1
MyData = MyData & ListBox.List(i) & vbCrLf
Next i
Open "p:\help files\prod\text.txt" For Output As #1
Write #1, MyData
Close #1
Close
End Sub
The list box is populated from the text file by:
Private Sub Form_Load()
entrydate.text = "[ " & Date & " ]"
Dim i As Long, strText As String, flnStream As String
flnStream = "P:\Help Files\Prod\Text.txt"
i = FreeFile
Open flnStream For Input As #i
Do While Not EOF(1)
Input #1, strText
ListBox.AddItem (strText)
Loop
Close #i
Close
End Sub
The listbox shows black lines between the listed text, and the text shows up
on one line, example
DO NOT WRITE ON THIS LINE! || 12R-DJDS-4045T || 4HT-DDS-4-2011||
If I edit the text file, and remove the quotation marks, or move the end
quotation mark to the end of the first line "DO NOT WRITE ON THIS LINE!",
then the listbox will be correct, and look just like the text file, until the
next time I write to the file.
I use this to remove line items inside the list box, before I write to it:
Private Sub RemoveItem1_Click()
On Error Resume Next 'in case the user forgets to select a line to
be removed
If ListBox.ListCount >= 1 Then
ListBox.RemoveItem (ListBox.ListIndex)
End If
End Sub
And this is to add to the text file: '(the text is written inside a text box
on the form)
Private Sub add_Click()
Open "p:\help files\prod\text.txt" For Append As #1
Print #1, entrydate
Print #1, text
ListBox.AddItem text
Close #1
Close
End Sub
While this seems like a lot to ask, I do not understand where the quotation
marks come from inside the text box, if I edit them out, and write to the
file using the "Write_Me" sub, the quotation marks come back.
Tanx
Krakmup
> > The string "MyData", when writing to the text file, ends up on one line,
>
[quoted text clipped - 23 lines]
> > > recommend a stringbuilder-type solution. Something like
> > > http://vb.mvps.org/samples/StrBldr
Jonathan West - 21 Mar 2006 14:52 GMT
Hi Krakmup
This line is your first problem
Write #1, MyData
it should be this
Print #1, MyData;
in order not to have the quotes in the written file.
Then your next problem is when reading the text file back. The following
code
Open flnStream For Input As #i
Do While Not EOF(1)
Input #1, strText
ListBox.AddItem (strText)
Loop
Close #i
should be changed to this
Open flnStream For Input As #i
strText = Input(LOF(i), #i)
ListBox.List = Split(strText, vbCrLf)
Close #i
There are three techniques worth noting here.
1. The semicolon at the end of the Print statement, which ensures there
isn't a trailing vbCrLf at the end of the file.
2. Using the Input function instead of the Input statement, and using LOF()
to find out how many characters there are in the file in order to read the
entire file into the string variable in one go.
3. Using the Split function to split the text file into a Variant containing
an array of strings, and assigning the whole array to the List property of
the ListBox. Note that this is something you can do with VBA Listboxes in
UserForms, but which you can't do in VB6 Forms.

Signature
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
> G'Day
> I started looking at what you meant by "determining" why the text file
[quoted text clipped - 114 lines]
>> > > recommend a stringbuilder-type solution. Something like
>> > > http://vb.mvps.org/samples/StrBldr
Krakmup - 21 Mar 2006 16:40 GMT
G'Day
This has resolved my problem. Thanks to both Jonathan and Karl for your help.
Krakmup
> Hi Krakmup
>
[quoted text clipped - 157 lines]
> >> > > recommend a stringbuilder-type solution. Something like
> >> > > http://vb.mvps.org/samples/StrBldr
Karl E. Peterson - 21 Mar 2006 22:06 GMT
> 3. Using the Split function to split the text file into a Variant
> containing an array of strings, and assigning the whole array to the
> List property of the ListBox. Note that this is something you can do
> with VBA Listboxes in UserForms, but which you can't do in VB6 Forms.
Yeah, that really _is_ pretty cool! I've written routines to encapsulate
that before in VB, but had no idea that VBA could do that automagically.
:-)

Signature
Working without a .NET?
http://classicvb.org/