Results 1 to 5 of 5

Thread: How to print the items from ListBox in a specific format

  1. #1
    Junior Member
    Join Date
    Aug 12
    Posts
    23

    How to print the items from ListBox in a specific format

    I'm making a windows form modul (from excels visual basic) which has various elements like "Author", "year of publication", "Title" and "Category"
    The task is, that after the user given a Value to every of these element theres a button, which saves them into a list, and deletes all the data that has been written into the elements (like the title name category etc)

    That was the first part of the main task.
    The secont part, is that I have to export the list content into a file, which is not a problem, instead the problem is that I have to export them in a given format!

    For example its exports it with my solution like this: (every element is in a new line)
    Author name
    Books name
    Year of publication
    Category


    Each of the in a new line, but I have to export then in this formation: (every element is in one line, but if I record a new one it should be in a new line like below)
    Author name1;Books name1;Year of publication1;Category1;
    Author name2;Books name2;Year of publication2;Category2;


    I just can't figure it out how to do this!


    Here's my code snippet from the export button:
    What you have to know about this is :
    lista = ListBox1

    Code:
    Private Sub CommandButton_export_Click()
    
    Dim i As Integer
    
    Open "D:\export.txt" For Append As #1 
    For i = 0 To lista.ListCount - 2
          lista.Selected(i) = True
          Print #1, lista.List(lista.ListIndex) & ";"
          'lista.List(lista.ListIndex)
    Next
    Close #1
    
    lista.Clear
    End Sub

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,625

    Re: How to print the items from ListBox in a specific format

    Welcome to VBForums

    Thread moved to the 'Office Development/VBA' forum... note that while it certainly isn't made clear, the "VB Editor" in Office programs is actually VBA rather than VB, so the 'VB6' forum is not really apt

  3. #3
    Junior Member
    Join Date
    Aug 12
    Posts
    23

    Re: How to print the items from ListBox in a specific format

    Thanks for the welcome, and sorry for the wrong forum

    Is there anyone who could help me out? It is very important to me make this little part of the program. I'm not good in visual basic language, but it is vital to me to do this task! Please someone help!
    Last edited by Gülredy; Aug 1st, 2012 at 02:11 PM.

  4. #4
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,592

    Re: How to print the items from ListBox in a specific format

    Print #1, lista.List(lista.ListIndex) & ";"
    the ; at the end of the line tells it to continue the next print statement on the same line, but you have appended it to the string to be printed
    try like
    Code:
    Print #1, lista.List(lista.ListIndex) ;
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    Junior Member
    Join Date
    Aug 12
    Posts
    23

    Re: How to print the items from ListBox in a specific format

    I've received a solution in another forum, I'm only writing this to others who have, or will have the same problem:
    The solution was this:

    fileba = ""
    For i = 0 To lista.ListCount - 2
    lista.Selected(i) = True
    fileba = fileba & lista.List(lista.ListIndex) & ";"
    Next
    Print #1, fileba


    It works like a charm!
    Thanks for the replys!

    PS:
    Don't bother with the image!
    Attached Images Attached Images  
    Last edited by Gülredy; Aug 2nd, 2012 at 02:11 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •