Combo Box Matching 
Author Message
 Combo Box Matching

Hi,

I have a combo box on a form that I have the style property set to 0 -
Dropdown Combo because I want the User to be able to add items to the list
if they are not already there.

I would also like the combo box to select Items that are already there as
the user types characters into the combo box (similar to the combo with the
style property set to 2 - Dropdown List).  For example, if the list
contained an item "Yellow", and the user typed a "y", I would like the item
"Yellow" to be selected in the list with the .SelStart and .SelLength
properties set such that "ellow" is selected.

I put code into the keypress event to implement this behavior, but it is not
working.  Stepping through the code, I noticed strange things going on with
the .SelText and .SelStart properties.

Has anyone done anything like this before?  Please Help.  Below is the code
I put in the Keypress Event.

Thanks,
Jeff

Private Sub Combo1_KeyPress(Index As Integer, KeyAscii As Integer)
    Dim lngID As Long
    Dim strText As String
    Dim strSelText As String
    Dim strStart As String
    Dim intLength As Integer
    Dim intSelLength As Integer
    Dim intSelStart As Integer
    With Combo1(Index)
 ' I grab the .sel Properties here because they seemed to change
 ' Between Here and when I did the Replace a few lines down
        strSelText = .SelText
        intSelStart = .SelStart
        intSelLength = Len(strSelText)
        strText = .Text
        ' First Figure out What the Combo's Text will look like
        ' After the the new keystroke is executed
        If intSelLength > 0 Then
            ' Remove Any Selected Text
            strText = Replace(strText, strSelText, vbNullString, intSelStart
+ 1)
        End If
        ' Insert the Character just entered where it belongs
        strText = Left$(strText, intSelStart) & Chr$(KeyAscii) &
Mid(strText, intSelStart + 1)
        ' Set KeyAscii to 0 so that the Char doesn't get inserted twice
        KeyAscii = 0
        strText = LCase(strText)
        ' Loop through list looking For the first Item That matches
        ' the text so Far
        For lngID = 0 To .ListCount - 1
            intLength = Len(strText)
            strStart = LCase$(Left$(.List(lngID), intLength))
            If strText = strStart Then
                ' If Match was found, Set The list index and Text Properties
                ' To Reflect The Selection
                .ListIndex = lngID
                ' Set The SelStart and SelLength Properties justincase
                ' First Match was not the one the User is looking for
                .SelLength = 0
                .SelStart = intLength
                .SelLength = Len(.Text) - .SelStart
                Exit For
            End If
        Next lngID
    End With
End Sub



Wed, 31 Jul 2002 03:00:00 GMT  
 Combo Box Matching

Quote:
>I would also like the combo box to select Items that are already there as
>the user types characters into the combo box (similar to the combo with the
>style property set to 2 - Dropdown List).  For example, if the list
>contained an item "Yellow", and the user typed a "y", I would like the item
>"Yellow" to be selected in the list with the .SelStart and .SelLength
>properties set such that "ellow" is selected.

Jeff,

Try the Autofill sample at http://www.softcircuits.com/

Mattias

__________________________________________________

     VB+ http://hem.spray.se/mattias.sjogren/
 Please send questions/replies to the newsgroups



Wed, 31 Jul 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Combo Box Matching

2. combo boxes and extended matching

3. Extended Matching with Combo Boxes

4. Matched entry combo box problem

5. Problem with Matched entry property of data bound combo box

6. enter data in a combo box based on value from another combo box

7. Limit subform combo box by selection in parent combo box

8. Want Access combo box to filter rowsource of another combo box

9. Populating a combo box from another combo box

10. Populate a combo box from another combo box selection

11. selecting records for 2nd combo box dependant on first combo box

12. Help filling second combo box from fist combo box

 

 
Powered by phpBB® Forum Software