Consulting

Results 1 to 5 of 5

Thread: Solved: ComboBox error: Invalid property value

  1. #1

    Solved: ComboBox error: Invalid property value

    Hi

    I've gtot a form with 30 comboboxes. During form initialization they recieve an array to their list property. I want to limit the entered values to list items, so I set the MatchRequired property to True.

    I also want to have the option to leave the combobox empty, so I always set the first element of the list to an empty string:
    [vba] ComboBox1.List(0)=""[/vba]
    Now, when I "accidentally" type something into a combobox, then delete it with Del key, I cannot leave the combobox: it gives me the error message indicated in the thread title. Even though the list does contain an empty string, and the current value of the combobox is also an empty string, I cannot leave the box. I need to select the empty string from the list, an only then can I leave.

    See what I mean in the attached workbook.

    I want to use the Del key, because data entry is lot faster and more convenient via keyboard than mouseclicking. How could I convince my comboboxes that a deleted string is really an empty string?

    Thanks in advance

    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  2. #2
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    If entry is required into that combo box, and they must select something from the list, why have the empty string as a choice?

    You can look at capturing the delete key presses ( IF KeyCode = 46 then...) or you can use on error for combo box selection/change event handling
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  3. #3
    XLGibbs,

    Entry is not required. I tried to say that the user should either leave the combobox empty, or, if data is entered, it must be selected from the list. Sorry for being unclear.

    I'm not sure how you meant to solve the problem with error handlers. The ComboBox_Change event never caused exception. I also tried this:
    [VBA]Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    On Error GoTo Err_h
    Exit Sub
    Err_h:
    ComboBox1.ListIndex = 0
    End Sub[/VBA]

    Unfortunately, the "Invalid property Value" message comes before the event code is activated, so it does not work.

    Your idea about capturing key presses is working, however, it means that I need to write 30 event handler for the 30 comboboxes... which I will if I must, but in the meantime I'm still looking for a simpler solution.

    Thank you for the tips, anyway

    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Userform

    [vba]

    Dim mcolEvents As Collection

    Private Sub UserForm_Initialize()
    Dim cComboEvents As clsUserFormEvents
    Dim ctl As MSForms.Control

    Set mcolEvents = New Collection

    For Each ctl In Me.Controls
    If TypeName(ctl) = "ComboBox" Then
    Set cComboEvents = New clsUserFormEvents
    Set cComboEvents.mComboGroup = ctl
    mcolEvents.Add cComboEvents
    End If
    Next

    End Sub
    [/vba]

    Class module

    [vba]

    Option Explicit

    Public WithEvents mComboGroup As MSForms.ComboBox


    Private Sub mcomboGroup_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    MsgBox mComboGroup.Name
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Thanks (again ), Bob.

    At first the code you gave didn't work, then I figured out that DEL key doesn't raise the KeyPress event, so KeyUp is needed instead. Then, again, I figured out that I do not need, at all, to check what key was pressed. The only important thing is that when the box becomes empty, it's value must change to the empty string from the list.

    So for benefit of future generations, I gladly attach the solution.

    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

Posting Permissions

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