Results 1 to 14 of 14

Thread: How to Save listbox items to text file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    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

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    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.

  4. #4
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    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

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    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:
    1. Private Sub Command8_Click()
    2. Dim I As Long
    3.  
    4. For I = 0 To List1.ListCount - 1
    5.     'If List1.Selected(I) = True Then
    6.         'MsgBox List1.Text
    7.         MsgBox List1.List(I)
    8.     'End If
    9. Next
    10. 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.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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

  7. #7
    Addicted Member
    Join Date
    Apr 2008
    Posts
    198

    Re: How to Save listbox items to text file

    hack. this code is perfectooooo

  8. #8
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: How to Save listbox items to text file

    Quote 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

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    2

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

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  11. #11
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: How to Save listbox items to text file

    Quote Originally Posted by miczster View Post
    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.
    Doctor Ed

  12. #12
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    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.

  13. #13
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    764

    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! ;-)

  14. #14
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    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
  •  



Click Here to Expand Forum to Full Width