Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2006

Tip: Looking for answers? Try searching our database.

save listbox contents to a string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Krakmup - 20 Mar 2006 21:19 GMT
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
Karl E. Peterson - 20 Mar 2006 21:24 GMT
> 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
Signature

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

Krakmup - 20 Mar 2006 22:16 GMT
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

> > 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
[quoted text clipped - 11 lines]
> recommend a stringbuilder-type solution.  Something like
> http://vb.mvps.org/samples/StrBldr
Karl E. Peterson - 20 Mar 2006 22:47 GMT
> The string "MyData", when writing to the text file, ends up on one line,

How are you determining that?
Signature

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

> G'Day;
> Tanx Karl
[quoted text clipped - 20 lines]
> > recommend a stringbuilder-type solution.  Something like
> > http://vb.mvps.org/samples/StrBldr
Krakmup - 21 Mar 2006 14:20 GMT
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/

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.