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

Save Listboxitems To .txt File, but the maximum is different everytime 1

Status
Not open for further replies.

ThaVolt

Programmer
Aug 12, 2002
34
NL
My program opens a log file in a listbox every time it opens but when it closes it needs to save the new items added.. i tried this code but it only saved 1 line out of the list box..

Private Sub Form_Unload(Cancel As Integer)
Dim list As ListBox, name As String
For i = 0 To List1.ListCount - 1
Data$ = List1.list(i)
Open "C:\BLABLA.text" For Output As #1
Print #1, Data$
Close 1
Next i
End
End Sub

So this will only save the first line of the list but it needs to save the entire list no matter how long it is.. cuz its a log..

Maybe this is a very novice question and the awnser is very simple but i could find the awnser on this forum so i thought i ask it myself :) well hope u guys can help me out..

Greetings
 
The problem is that your opening the file for output each time, which will always create a new file. For this to work as you expect, you would need to open the file for Append

For i = 0 To List1.ListCount - 1
Data$ = List1.list(i)
Open "C:\BLABLA.text" For Append As #1
Print #1, Data$
Close 1
Next i

That being said, that is not the most efficient way to accomplish this as your opening and closing the file for every item. It would be better to open the file one time, do all the prints, then close the file. Its also a good idea to use the FreeFile function to get the file handle number.

Dim FileID as Integer
FileID = FreeFile
Open "C:\BLABLA.text" For Output as #FileID
For i = 0 To List1.ListCount - 1
Data$ = List1.list(i)
Print #FileID, Data$
Next i
Close #FileID
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
i found a site with such info on output input put write append and such... but i forgot where.. can you by chance send me a link if you know of a site.. or explain them? thanks alot :) in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
Unborn: VBhelp

Processing Files with Older File I/O Statements and Functions
Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 

I belive CajunCenturion has the answer but to make thing faster (less file opening and closing) and to overwrite the file each time this code executes

Open "C:\BLABLA.text" For Output As #1

For i = 0 To List1.ListCount - 1
Data$ = List1.list(i)
Print #1, Data$
Next i

Close 1


also the list box has the same limitations that a vb integer value has (i.e. 32767) so that is the max entries you can have in a list box. I would check this each time you load your list box.

Good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top