|
-
Nov 17th, 2008, 04:38 PM
#1
Thread Starter
Frenzied Member
How to Save listbox items to text file
Hi all. could any one show me how i can save all contents of a listbox to a text file ? Thanks
-
Nov 17th, 2008, 04:39 PM
#2
Re: How to Save listbox items to text file
1. Do you know how to create & open a text file?
2. Do you know how to loop through a listbox?
Show us what you have so far.
-
Nov 17th, 2008, 04:42 PM
#3
Hyperactive Member
Re: How to Save listbox items to text file
He probably doesn't have anything because he doesn't know how. We could at least mention the process, which you touched on.
1. Open textfile for writing. (Open statement)
2.Loop through listbox items with a For Next loop
3.Save each list item to the file inside the loop (Print statement)
4.Close the file (Close statement)
Look into these, search the forums, (including code bank) and post back if you run into issues.
-
Nov 17th, 2008, 05:48 PM
#4
Frenzied Member
Re: How to Save listbox items to text file
Code:
Dim cdlg As Object
Public Sub xSaveList(List As ListBox, Optional clear As Boolean = False)
'self explanatory
On Error Resume Next
Dim lngSave As Long
Set cdlg = CreateObject("MSComDlg.CommonDialog")
cdlg.Filter = "Text Files (.txt)|*.txt"
cdlg.filename = ""
cdlg.ShowSave
cdlg.DialogTitle = "Save List"
If LenB(cdlg.filename) = 0 Then Exit Sub
Dim filename As String
filename = cdlg.filename
If clear Then List.clear
Open filename$ For Output As #1
For lngSave& = 0 To List.ListCount - 1
Print #1, List.List(lngSave&)
Next lngSave&
Close #1
End Sub
Public Sub xLoadList(List1 As ListBox, Optional clear As Boolean = True)
'self explanatory
Set cdlg = CreateObject("MSComDlg.CommonDialog")
cdlg.Filter = "Text Files (.txt)|*.txt"
cdlg.filename = ""
cdlg.DialogTitle = "Load List"
cdlg.ShowOpen
If LenB(cdlg.filename) = 0 Then Exit Sub
Dim filename As String
filename = cdlg.filename
Dim lstInput$, oneinput, twoinput As String
If clear Then List1.clear
On Error Resume Next
Open filename$ For Input As #1
While Not EOF(1)
Input #1, lstInput$
List1.AddItem lstInput$
Wend
Close #1
End Sub
Uses common dialog to load and save listboxes
-
Nov 18th, 2008, 10:02 AM
#5
Thread Starter
Frenzied Member
Re: How to Save listbox items to text file
Thanks all . I managed to loop the listbox now how i write it to a text file ?
2 Code:
Private Sub Command8_Click()
Dim I As Long
For I = 0 To List1.ListCount - 1
'If List1.Selected(I) = True Then
'MsgBox List1.Text
MsgBox List1.List(I)
'End If
Next
End Sub
Zach thanks for your code but can you tell me what controles and buttons i need to run it ? i want to learn this step by step.
Last edited by tony007; Nov 18th, 2008 at 10:05 AM.
-
Nov 18th, 2008, 12:43 PM
#6
Re: How to Save listbox items to text file
Here is how to both save the listbox, and reload it again.
Code:
Private Sub SaveLoadListbox(plstLB As ListBox, _
pstrFileName As String, _
pstrSaveOrLoad As String)
Dim strListItems As String
Dim i As Long
Select Case pstrSaveOrLoad
Case "save"
Open pstrFileName For Output As #1
For i = 0 To plstLB.ListCount - 1
plstLB.Selected(i) = True
Print #1, plstLB.List(plstLB.ListIndex)
Next
Close #1
Case "load"
plstLB.Clear
Open pstrFileName For Input As #1
While Not EOF(1)
Line Input #1, strListItems
plstLB.AddItem strListItems
Wend
Close #1
End Select
End Sub
Private Sub Form_Load()
Call SaveLoadListbox(List1, "c:\Listbox.txt", "load")
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call SaveLoadListbox(List1, "c:\Listbox.txt", "save")
End Sub
-
Nov 18th, 2008, 03:02 PM
#7
Addicted Member
Re: How to Save listbox items to text file
hack. this code is perfectooooo
-
Nov 18th, 2008, 09:34 PM
#8
Re: How to Save listbox items to text file
 Originally Posted by tony007
Hi all. could any one show me how i can save all contents of a listbox to a text file ? Thanks
Build a form with a list box and a command button. Then apply this code:
Code:
Private Sub Command1_Click()
' Save List Box Items in File
Open "MyTextFile.txt" For Output As #1
For I = 0 To List1.ListCount - 1
Print #1, List1.List(I)
Next
Close
End Sub
Private Sub Form_Load()
'Add some data to list box
For I = 1 To 10
List1.AddItem Str$(I)
Next
End Sub
Note: that will rebuild the text file each time, so you may wish to open it for Append.
Last edited by Code Doc; Nov 18th, 2008 at 09:38 PM.
Doctor Ed
-
Mar 23rd, 2013, 01:50 AM
#9
Registered User
Re: How to Save listbox items to text file
Hello ...I loaded a txt file 1204 kb with hundreds/thousands of rows into a listbox.
I added something to the listbox (15 lines) successfully but when I saved it (overwriting the orig txt file). The txt file becomes blank.
I used this code:
Dim AA As Integer,b as string ,v As Integer
b=data.txt
AA = FreeFile()
Open b For Output As AA 'To rewite the file replace "Append" with "Output"
For v = 0 To List3.ListCount - 1
Print #AA, List3.List(v)
Next
Close #AA
I tried using the same code above with other txt files and it worked perfectly.
Is the problem because the file I tried to overwrite is too large that it went from 1204kb to 0kb (blank file)?
-
Mar 25th, 2013, 01:05 PM
#10
Re: How to Save listbox items to text file
b = data.txt
Is this exactly how you are initializing b or is it actually b = "data.txt"
If b = "data.txt' and not b = data.txt then when you first open the file as Output it is set to 0 bytes. So, if you wind up with 0 bytes after the loop has finished then the only thing I see is the listbox has 0 entries in it otherwise your code looks OK
Maybe you should show the code you use to load the list3
.I loaded a txt file 1204 kb with hundreds/thousands of rows into a listbox.
Don't quite understand your statement here. You say "I loaded a txt file...." What do you mean by loaded? Are you saying you loaded a text file and then from the text file you loaded it into a listbox?
BTW: Next time you have a question please open a new thread and put a reference in that thread to the old thread instead of replying to a thread from Nov '08 which is 4 years old
Last edited by jmsrickland; Mar 25th, 2013 at 01:16 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Mar 25th, 2013, 06:39 PM
#11
Re: How to Save listbox items to text file
 Originally Posted by miczster
Hello ...I loaded a txt file 1204 kb with hundreds/thousands of rows into a listbox.
I added something to the listbox (15 lines) successfully but when I saved it (overwriting the orig txt file). The txt file becomes blank.
I used this code:
Dim AA As Integer,b as string ,v As Integer
b=data.txt
AA = FreeFile()
Open b For Output As AA 'To rewite the file replace "Append" with "Output"
For v = 0 To List3.ListCount - 1
Print #AA, List3.List(v)
Next
Close #AA
I tried using the same code above with other txt files and it worked perfectly.
Is the problem because the file I tried to overwrite is too large that it went from 1204kb to 0kb (blank file)?
You realize, of course, that you opened a thread that was five years old? Where have you been and what have you been doing for the last five years?
Please advise.
-
Mar 25th, 2013, 06:49 PM
#12
Re: How to Save listbox items to text file
The listbox has a limit of 32767 items.
Not sure if this is the reason why your file did not save (error), but your code looks fine.
-
Mar 26th, 2013, 08:05 AM
#13
Re: How to Save listbox items to text file
> You realize, of course, that you opened a thread that was five years old? Where have you been and what have you been doing for the last five years?
They've probably been waiting for that thousands-of-items list to load! ;-)
-
Mar 26th, 2013, 09:31 AM
#14
Re: How to Save listbox items to text file
This line, b=data.txt, doesn't make sense to me.
If data.txt is the text file to which you are attempting to write the contents of list3, then b should equal something like this:
app.path & "\data.txt" or
if you prefer, "C:\documents and settings\user\yourcomputerusername\mydirectory\data.txt" Or something similar where both the path and text file are in quotes.
And, what Max says can put a kabosh on your code.....limited number of entries in a listbox.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|