I found this function somewhere a while ago;
Stick this in a module;
Code:Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Const CB_FINDSTRING = &H14C Const CB_ERR = (-1) Public Function AutoFind(ByRef cboCurrent As ComboBox, ByVal KeyAscii As Integer, Optional ByVal LimitToList As Boolean = False) Dim lCB As Long Dim sFindString As String On Error GoTo Err_Handler If KeyAscii = 8 Then If cboCurrent.SelStart <= 1 Then cboCurrent = "" AutoFind = 0 Exit Function End If If cboCurrent.SelLength = 0 Then sFindString = UCase(Left(cboCurrent, Len(cboCurrent) - 1)) Else sFindString = Left$(cboCurrent.Text, cboCurrent.SelStart - 1) End If ElseIf KeyAscii < 32 Or KeyAscii > 127 Then Exit Function Else If cboCurrent.SelLength = 0 Then sFindString = UCase(cboCurrent.Text & Chr$(KeyAscii)) Else sFindString = Left$(cboCurrent.Text, cboCurrent.SelStart) & Chr$(KeyAscii) End If End If lCB = SendMessage(cboCurrent.hwnd, CB_FINDSTRING, -1, ByVal sFindString) If lCB <> CB_ERR Then cboCurrent.ListIndex = lCB cboCurrent.SelStart = Len(sFindString) cboCurrent.SelLength = Len(cboCurrent.Text) - cboCurrent.SelStart AutoFind = 0 Else If LimitToList = True Then AutoFind = 0 Else AutoFind = KeyAscii End If End If Err_Handler: End Function
Then, in the KeyPress event of the combobox in question - do this
Code:Private Sub cboText_KeyPress(KeyAscii As Integer) KeyAscii = AutoFind(cboText, KeyAscii, False) End Sub




Reply With Quote