Results 1 to 8 of 8

Thread: Listbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    343

    Unhappy

    Can someone please help me with the code to populate a listbox with data in a table

    What I want to do is enter a name (txtname.text) and then search for all records in the table which matches that name and then display it in a listbox.

    public sub search()

    Dim strFind As String

    strFind = "surname like '" & txtClientSurname & "*' "
    rsRecord.Find strFind

    end sub


    How do I display all records in the recordset to the listbox from where the user can double click the record he wants.

    ANY help please!!


  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    You could try something like this:

    Code:
    While Not rsRecord.Eof
      list1.additem rsRecord.fields("surname")
      list1.itemdata(list1.newindex) = rsRecord.fields("ID")
      rsRecord.MoveNext
    Wend
    Hope this helps,

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ....

    Just some little amendments, Negative0. I hope you won't mind.

    Before the 'while' loop in your code put:

    Code:
    List1.Clear

    And whatever code you use to display the record, put it in the DoubleClick event of the listbox.

    Maybe you have already figured out that much...

    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    343
    Thanks for this code!! What does the 2nd line ( list1.itemdata(list1.newindex) = rsRecord.fields("ID"))
    in the while statement do?? If I take it out all surnames in the recordset is displayed on the listbox!!

    Does the second line handle the case where I want 2 or 3 columns?? ie

    Surname Tel Fax
    White 8072766 8070263
    Black 5465656 687634

    How do I do this??

    While Not rsRecord.Eof
    list1.additem rsRecord.fields("surname")
    list1.itemdata(list1.newindex) = rsRecord.fields("ID")
    rsRecord.MoveNext
    Wend

  5. #5
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    TO add the telephone and fax try this:
    Code:
    List1.clear
    While Not rsRecord.Eof
      list1.AddItem rsrecord.fields("surname") & Space(20 - Len(rsrecord.fields("SurName"))) & _
    rsrecord.fields("Tele") & Space(20 - Len(rsrecord.fields("Tele"))) & rsrecord.fields("FAX")
      list1.itemdata(list1.newindex) = rsRecord.fields("ID")
      rsRecord.MoveNext
    Wend
    The list1.Itemdata line allows you to store the ID from the record with the list box. You can then use this ID to reference the record.


  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    343
    Negative0,

    This works great - I now have the 3 columns.

    Sorry for being stupid, but I still cannot get this line to work....

    list1.itemdata(list1.newindex) = rsRecord.fields("ID")

    I get this error on the line:- "ADO could not find the object in the collection corresponding to the name or ordinal requested by the application"

    I do not have a field "ID" in my table!!?? Is this a variable which I must set or what.

    Thanks for your help.




  7. #7
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    The ID is an easy way to access the record you have selected, if you do not have one you can omit the line.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    343

    Cool

    NOW I've got you - it took a while, but I've got it!!!

    Thanks for the help Negative0.

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