compugeek
04-08-2003, 03:53 PM
How do you write a listbox's list to a text file? The text file is all set up, I just need to write the list to it. For example, if the list box had this in it:
Car One
Red Car
Car Orange
...And to have the text file have:
Car One
Red Car
Car Orange
How would I do this? Thanks in advance! :)
chris00
04-08-2003, 03:55 PM
You would have to loop throught each item and write it to the file.
Hope this Helps :) :D :cool:
Robse
04-08-2003, 03:59 PM
Just traverse the listbox's items and output each
item to file:
For n = 0 To List1.ListCount-1
Print #FreeFileNum, List1.List(n)
Next n
compugeek
04-08-2003, 04:00 PM
I'll experiment with the loops (thanks, people! :) )!
-compgeek- :cool:
Robse
04-08-2003, 04:13 PM
What happened, did you get an error message?
You said the file is all set up? (FreeFileNum is just
a name that I made up)
Dim fn As Integer
fn = FreeFile
Open YourFile For Output As #fn
' Do the loop stuff (But now use #fn not #FreeFileNum)
Close fn
compugeek
04-08-2003, 04:49 PM
No error message... the file WAS set up; it just doesn't write to the file. I just tried that new code... it doesn't work. Do I need to replace fn=FreeFile with something?
I think you are going to need to post the code you're using, so we can see where it's going wrong.
compugeek
04-08-2003, 08:23 PM
I think you are going to need to post the code you're using, so we can see where it's going wrong.
Here it is:
Private Sub Command1_Click()
Dim fn As Integer
fn = FreeFile
'Open the text file.
Open (App.Path & "/horsenames.txt") For Output As #fn
'start the loop.
For n = 0 To List1.ListCount - 1
Print #fn, List1.List(n)
Next n
'close the file.
Close fn
'end the program (I want to do this)
End
End Sub
Why do you have parentheses around the path and filename? That's the only obvious problem I see, except for the unnecessary use of End which could cause a memory leak since it prevents any termination/cleanup code from running.
compugeek
04-08-2003, 08:31 PM
Well, without the parenthasees won't work, and the End is to end the program after it writes to the file. I tested this with a different situation, and it still wrote to the file.
Robse
04-09-2003, 03:31 AM
found a typo: ..."/horsenames...
Open App.Path & "\horsenames.txt" For Output As #fn
compugeek
04-09-2003, 03:01 PM
Hmm.... now I get a "Bad File name or Number" error.