Problem with listbox selecting an item

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • is49460
    New Member
    • Jan 2007
    • 25

    Problem with listbox selecting an item

    Hello,

    I have a form with various controls but to simplify the problem let’s say I have 2 listboxes. The selection in listbox1 controls RowSource of the listbox2. For example lstCars contains car models, if I select car model X then lstColors should give me available colors for that particular model. The problem is if I select car model X in lstCars, I want the first color in lstColors to be selected by default.

    Here is a sample code:
    Code:
    Private Sub lstCars_AfterUpdate()
       Call SetColors()  
       
       ‘problem is in the following statements:
       If Me.lstColors.ListCount > 0 And Me.lstColors.ItemsSelected.count < 1 then
       	Me.lstColors.Selected(1) = True
       End If
    End Sub
    
    ‘function to change rowsource of lstColors to correspond to selection in lstCars
    SetColors()
       Me.lstColors.RowSource = “Select Color From [tbl Cars] Where Model = ‘”   &    me.lstCars.column(0) & “’”
    End sub
    After the code is executed. Item in lstColors locks up and no more changes can be done in that listbox. I think sub "lstCars_AfterU pdate" is being called if I click on any item in lstColors, always selecting the first item.
    I’m not sure what is going wrong but obviously I’m missing something, Please help.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32668

    #2
    I can't see a problem.
    Try using the debugging facilities (Debugging in VBA) to identify whereabouts your code is differing from what you expect it to do.

    Comment

    • is49460
      New Member
      • Jan 2007
      • 25

      #3
      I've changed event from AfterUpdate to BeforeUpdate and it appears to work fine... I need to look more into it. I dont have an expaination why, but that change solved the problem.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32668

        #4
        Well, good for you.

        I must admit, I can't see why that would have any effect.
        I'm glad it's working for you anyway :)

        Comment

        Working...