Results 1 to 15 of 15

Thread: [RESOLVED] Load text to a list box?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Resolved [RESOLVED] Load text to a list box?

    Hi guys I was wondering I use the following code to load to a texbox from a textfile

    VB Code:
    1. Open App.Path & "\status.txt" For Append As #1
    2.         Write #1, "Manual"
    3.         Close #1

    but how do I load to a listbox?

    Thanks!!

  2. #2
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Load text to a list box?

    Code:
    Private Sub Command1_Click()
    dim line as string
    fno = FreeFile()
    Open App.Path & "\yourfile.txt" For Input As #fno
    Do While Not EOF(fno)
       Line Input #fno, Line
       ListBox.AddItem Line
    Loop
    End Sub
    Last edited by Fazi; Sep 22nd, 2007 at 01:38 AM.

  3. #3
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Load text to a list box?

    sorry, 2 lines have been altred in my code.
    1. declaration of line variable
    2. an unwanted endif

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Load text to a list box?

    Thank you

    Can list boxes on QueryUnload Events write to a textfile as well?

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Load text to a list box?

    Listbox doesn't write anything to anywhere - you do that.
    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Dim i As Integer
    
        Open App.Path & "\status.txt" For Append As #1
            For i = 0 To List1.ListCount - 1
                Print #1, List1(i)
            Next i
        Close #1
    
    End Sub

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Load text to a list box?

    Sorry to bump this, but instead of making a new thread about a followup question I had about this I was just wondering..

    I have a form called ignore and the query unload is part of the main form, form2. the list1 is on the ignore form. So my question is how do I get whatever is written to the list1 on the ignore form to be wirtten to the text file when the program is closed?

    Thanks1!!

  7. #7
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Load text to a list box?

    When dealing with controls from another form in the same application just simply call the forms name first
    Code:
    For i = 0 To Form1.List1.ListCount - 1
                Print #1, Form1.List1(i)
    Next i
    Casey.

  8. #8
    Lively Member
    Join Date
    Nov 2007
    Location
    Rochester, NY
    Posts
    111

    Re: Load text to a list box?

    I'm guessing that the ignore form is still running while the main form is. If so, you can simply use the above code, and modify it to print the ignore forms list.

    Example:

    Code:
        Open App.Path & "\status.txt" For Append As #1
            For i = 0 To IgnoreForm.List1.ListCount - 1
                Print #1, IgnoreForm.List1(i)
            Next i
        Close #1
    Or use the query unload on the ignore form, so that any time it's closed, it prints to the designated text file.
    Please use the search function prior to posting a question and see if someone's already answered it.
    -If I helped you, please rate me, as I'd do the same for you =)
    -Remember to select the Resolved option for your post when you've gotten the answers you need.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Load text to a list box?

    Quote Originally Posted by Neato
    I'm guessing that the ignore form is still running while the main form is. If so, you can simply use the above code, and modify it to print the ignore forms list.

    Example:

    Code:
        Open App.Path & "\status.txt" For Append As #1
            For i = 0 To IgnoreForm.List1.ListCount - 1
                Print #1, IgnoreForm.List1(i)
            Next i
        Close #1
    Or use the query unload on the ignore form, so that any time it's closed, it prints to the designated text file.
    Thanks, I put in the code, but I get an error stating "Wrong number of arugments or invalid property assignment" and it highlights the ".List1" in the line Print #1, IgnoreForm.List1(i)

    ????

  10. #10
    Lively Member
    Join Date
    Nov 2007
    Location
    Rochester, NY
    Posts
    111

    Re: Load text to a list box?

    Did you change 'IgnoreForm' to the name of your actual form?
    Please use the search function prior to posting a question and see if someone's already answered it.
    -If I helped you, please rate me, as I'd do the same for you =)
    -Remember to select the Resolved option for your post when you've gotten the answers you need.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Load text to a list box?

    yes, the actual name is just ignore

  12. #12
    Lively Member
    Join Date
    Nov 2007
    Location
    Rochester, NY
    Posts
    111

    Re: Load text to a list box?

    Sorry about the delay, phone call.. Anyways.. Replace that line with:

    Print #1, Ignore.List1.List(i)

    And that should take care of your problem.
    Please use the search function prior to posting a question and see if someone's already answered it.
    -If I helped you, please rate me, as I'd do the same for you =)
    -Remember to select the Resolved option for your post when you've gotten the answers you need.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Load text to a list box?

    Thank you so much !!!!!

    Edit: Sorry to both you again but I was having a small problem with loading text to the list1 listbox.

    The following code was posted in this thread
    VB Code:
    1. dim line as string
    2. fno = FreeFile()
    3. Open App.Path & "\yourfile.txt" For Input As #fno
    4. Do While Not EOF(fno)
    5.    Line Input #fno, Line
    6.    ListBox.AddItem Line
    7. Loop

    Now while I use this code I get the error file is already open. I think that is because the load the textfile to the listbox is used before the write to listbox later on.

    Does the textfile in the textfile to listbox get closed when it opens? Would that explain why when I get the error file is already open when It comes to the listbox to textfile code?
    Thansk1!1
    Last edited by Justin M; Dec 10th, 2007 at 05:07 AM.

  14. #14
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: [RESOLVED] Load text to a list box?

    When you open a file you should close it after you have finished with it so you dont get the error every time you try to open it again.
    Code:
    fno = FreeFile()
    Open App.Path & "\yourfile.txt" For Input As #fno
       Do While Not EOF(fno)   
            Line Input #fno, Line   
            ListBox.AddItem Line
       Loop 
    Close #fno
    Casey.

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: [RESOLVED] Load text to a list box?

    Thank you Casey, I changed the code a bit so it looks like this
    VB Code:
    1. fno = FreeFile()
    2. Open App.Path & "\ignorelist.txt" For Input As #fno
    3.    Do While Not EOF(fno)
    4.         Line Input #fno, line
    5.         Ignore.List1.AddItem line
    6.    Loop
    7. Close #fno

    When I press the command button, I get the error arugment not optional and it highlights the "line" in the "Ignore.List1.AddItem line" line.

    Would that mean I can't load to a list1 on another form this way?

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