save listbox contents to a string

K

Krakmup

G'Day

I am trying to change a text file, using a listbox for display. I can use
"AddItem" to list the text file, "Append" to update the text file, and
"RemoveItem" to update the listbox, but cannot figure out how to save the new
listbox contents into a string (with vbCrLf) so that I can overwrite the text
file on the hard drive.

Tanx
Krakmup
 
K

Karl E. Peterson

Krakmup said:
I am trying to change a text file, using a listbox for display. I
can use "AddItem" to list the text file, "Append" to update the text
file, and "RemoveItem" to update the listbox, but cannot figure out
how to save the new listbox contents into a string (with vbCrLf) so
that I can overwrite the text file on the hard drive.

Just loop through the items...

For i = 0 To .ListCount - 1
MyData = MyData & .Item(i) & vbCrLf
Next i

That's not going to be fast, if the list is long. In that case, I'd
recommend a stringbuilder-type solution. Something like
http://vb.mvps.org/samples/StrBldr
 
K

Krakmup

G'Day;
Tanx Karl
The string "MyData", when writing to the text file, ends up on one line,
instead of a list. I tried "CHR(13)" as well as vbCrLf, and get the same
result.

Any idea's
Krakmup
 
K

Karl E. Peterson

The string "MyData", when writing to the text file, ends up on one line,

How are you determining that?
 
K

Krakmup

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
 
J

Jonathan West

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.

--
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




Krakmup said:
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

Karl E. Peterson said:
How are you determining that?
 
K

Krakmup

G'Day

This has resolved my problem. Thanks to both Jonathan and Karl for your help.

Krakmup

Jonathan West said:
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.

--
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




Krakmup said:
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

Karl E. Peterson said:
The string "MyData", when writing to the text file, ends up on one
line,

How are you determining that?
--
Working without a .NET?
http://classicvb.org/


G'Day;
Tanx Karl
The string "MyData", when writing to the text file, ends up on one
line,
instead of a list. I tried "CHR(13)" as well as vbCrLf, and get the
same
result.

Any idea's
Krakmup

:

Krakmup wrote:
I am trying to change a text file, using a listbox for display. I
can use "AddItem" to list the text file, "Append" to update the
text
file, and "RemoveItem" to update the listbox, but cannot figure out
how to save the new listbox contents into a string (with vbCrLf) so
that I can overwrite the text file on the hard drive.

Just loop through the items...

For i = 0 To .ListCount - 1
MyData = MyData & .Item(i) & vbCrLf
Next i

That's not going to be fast, if the list is long. In that case, I'd
recommend a stringbuilder-type solution. Something like
http://vb.mvps.org/samples/StrBldr
 
K

Karl E. Peterson

Jonathan said:
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.
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top