Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto complete in a DataCombo?

Status
Not open for further replies.

Thead7

Programmer
Feb 2, 2002
7
GB
Does anyone know how to autocomplete a datacombo, currently i am using code that reads the first letter then auto completes using next closest word, the only problem is if you type another letter instead of blanking out the auto-completed text (everything except the 1st letter you typed) it just adds it onto the end of the word.
Any help would be much appreciated.
 
I tried to cut and paste this together as best I could, I think it is what you are looking for:

Option Explicit

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

Private Const CB_ERR = (-1)
Private Const CB_FINDSTRING = &H14C

Dim BackSpacedPressed As Boolean
Dim LengthOfText As Integer
Dim StrToFind As String
Dim Pos As Long
Dim SelStart As Integer

Private Sub cboComboBox_KeyPress(KeyAscii As Integer)

'Remember if last key pressed was the backspace
If KeyAscii = vbKeyBack Then

BackSpacedPressed = True

Else

BackSpacedPressed = False

End If

End Sub

Private Sub cboComboBox_Change()

'If backspace was pressed let the normal thing happen
If BackSpacedPressed = True Then Exit Sub
SelStart = Me.cboComboBox.SelStart 'Save the starting point
StrToFind = Left(Me.cboComboBox.Text, SelStart)
Pos = SendMessage(Me.cboComboBox.hwnd, CB_FINDSTRING, -1, ByVal StrToFind)

If Pos <> CB_ERR Then 'If item found

'Place the auto completed text in the text area
Me.cboComboBox.Text = Me.cboComboBox.List(Pos)
Me.cboComboBox.SelStart = SelStart 'Place the caret at the correct position
LengthOfText = Len(Me.cboComboBox.Text)
If LengthOfText > SelStart Then

'Highlight the auto-completed text so it can
'be deleted quickly if wanted
Me.cboComboBox.SelLength = LengthOfText - SelStart

End If

End If

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top