Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2004
    Posts
    364

    Unanswered: Automatically select a listbox row?

    I have a form with listboxes. The first listbox can display from two different row sources via ticking a checkbox.

    When the checkbox is ticked the listbox will display just one row, which is what I want. But is there a way to automatically select or highlight that single row?

    I need to automatically select the row, as a second listbox displays related information from the first listbox, but I have to keep selecting the row manually which is a pain.

    Thanks.

  2. #2
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    Assuming the name of your listbox is lstStuff then the code looks like:

    lstStuff.Selected(0) = True

    This will select the first item in the list.

  3. #3
    Join Date
    Aug 2004
    Posts
    364
    Thankyou, which event should the code be placed in?

  4. #4
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Place the code immediately after the code in the checkbox event that sets the rowsource for the first listbox. Assumming that some event for the first listbox sets the rowsource for the second listbox, you'll also have to explicitly Call that event, as a selection made thru code will not trip the event, as it would if you were physically making a selection.

    The question though, to my mind, is why, if the first listbox will only have one item in it, to you bother with it? Why not simply set the code for the second listbox based on the value of the checkbox directly?

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    Join Date
    Aug 2004
    Posts
    364
    Thanks it works, but only if I set the Column Heads to "no" in the listbox properties. If I turn the headings on, the code does not work, any ideas how to fix it?

  6. #6
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    Change 0 to 1?

  7. #7
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Do ya reckon?
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    Join Date
    Aug 2004
    Posts
    364
    Ok the row is now being highlighted in the first list box, but I still have to click on the row to make the related information in the second list box appear.

  9. #9
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    To repeat my first post, you'll also have to explicitly Call the event that sets sets the Rowsource of the second listbox, as a selection made thru code will not trip the event, as it would if you were physically making a selection.

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  10. #10
    Join Date
    Aug 2004
    Posts
    364
    Im having real difficulty with this. I can make the checkbox display and highlight the single row from the row source in the first listbox, but I cannot get the second listbox to display the related record automatically.

    Im having to use two seperate blocks of IF statements, as if I try to combine the Me.List2361.Selected(1) = True with the rowsource statement nothing gets highlighted.

    Code:
    Private Sub Check244_Click()
    'change row source to show all groups if check244 is true..
    Dim strRowSource As String
    If Me.Check244.Value = True Then
    strRowSource = "SELECT tblGroups.* FROM tblGroups;"
    Me.List2361.Requery
    
    Else
    'change row source to show single group if check244 is false..
    If Me.Check244.Value = False Then
    strRowSource = "SELECT * " & _
    "FROM tblGroups " & _
    "WHERE tblGroups.groupID = " & Me!groupID & " "
    Me.List2361.Requery
    
    End If
    End If
    
    Me.List2361.RowSource = strRowSource
    
    'highlight row of first listbox if check244 is false
    If Me.Check244.Value = False Then
    Me.List2361.Selected(1) = True
    Me.List21.Requery
    
    Else
    
    'do not highlight row of first listbox if check244 is true
    If Me.Check244.Value = True Then
    Me.List2361.Selected(1) = False
    
    End If
    End If
    
    End Sub

  11. #11
    Join Date
    Sep 2006
    Location
    Surrey, UK
    Posts
    995
    Provided Answers: 2
    Check boxes can only have two values (except when the triple state property has been enabled, which maddens me), so you have some redundant lines there.

    Code:
    Private Sub Check244_Click()
    'change row source to show all groups if check244 is true..
    Dim strRowSource As String
    If Me.Check244.Value = True Then
    strRowSource = "SELECT tblGroups.* FROM tblGroups;"
    
    Else
    'change row source to show single group if check244 is false..
    strRowSource = "SELECT * " & _
    "FROM tblGroups " & _
    "WHERE tblGroups.groupID = " & Me!groupID & " "
    
    End If
    
    Me.List2361.RowSource = strRowSource
    Me.List2361.Requery
    
    'highlight row of first listbox if check244 is false
    If Me.Check244.Value = False Then
    Me.List2361.Selected(1) = True
    Me.List21.Requery
    ***
    Else
    
    'do not highlight row of first listbox if check244 is true
    Me.List2361.Selected(1) = False
    
    End If
    
    End Sub
    Then, where I've put ***, you need to add code to dump the selected value from the list box into the second list box.

    HTH!
    10% of magic is knowing something that no-one else does. The rest is misdirection.

Posting Permissions

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