Results 1 to 5 of 5

Thread: Listbox Selections from Recordset - RESOLVED

  1. #1

    Thread Starter
    Member Suidae's Avatar
    Join Date
    Nov 2001
    Posts
    52

    Listbox Selections from Recordset - RESOLVED

    Let me see if I can explain this without sounding like a total dolt.

    I have a list box that is populated with five options. I select two of the five options and these selections are written back to the database. No problems here.

    My question is if I want to edit this record, can I show the original five options with the two I’ve previously selected already selected / highlighted?

    Clear as mud?
    Last edited by Suidae; Aug 21st, 2003 at 12:27 PM.
    I'm a misanthropic philanthropist!
    Frog, the only white meat...

  2. #2
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    Yes you can. Just place the five options in the listbox. Then query your database for the appropriate records. Then you would use two for loops, the outer for loop you can use to loop through your recordset, and the inner to loop through your listbox. If any item in the listbox matchs the a field in the recordset then change the selected property for that item to equal true.
    Motto: Anything for a laugh.

    Getting second place only means you are the first loser to cross the finish line.

  3. #3
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    Here is an example, but obviously you would use your recordset to query a database instead of the way I did it and made it a disconnected recordset.

    VB Code:
    1. Dim rsList As New ADODB.Recordset
    2.  
    3.     'instead of appending fields you would do a query and open
    4.    'your recordset with the appropriate fields.  
    5.     rsList.Fields.Append "Name", adBSTR
    6.    
    7.     rsList.CursorType = adOpenStatic
    8.     rsList.LockType = adLockOptimistic
    9.     rsList.Open
    10.    
    11.     'I am adding two records for test data, but you would
    12.     'already have data from your query
    13.     rsList.AddNew
    14.     rsList.Fields("Name") = "Greg"
    15.     rsList.AddNew
    16.     rsList.Fields("Name") = "Joe"
    17.    
    18.    'now just loop through and highlight any items that are equal
    19.     rsList.MoveFirst
    20.     For i = 1 To rsList.RecordCount
    21.         For n = 0 To List1.ListCount
    22.             If List1.List(n) = rsList("Name") Then
    23.                 List1.Selected(n) = True
    24.                 Exit For
    25.             End If
    26.         Next
    27.         rsList.MoveNext
    28.     Next
    29.    
    30.     rsList.Close
    31.     Set rsList = Nothing
    Motto: Anything for a laugh.

    Getting second place only means you are the first loser to cross the finish line.

  4. #4

    Thread Starter
    Member Suidae's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Many thanks Maldrid.. worked like a charm
    I'm a misanthropic philanthropist!
    Frog, the only white meat...

  5. #5
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    Glad I could help
    Motto: Anything for a laugh.

    Getting second place only means you are the first loser to cross the finish line.

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