I have a mdb named test and table prova and filed prova1.
Now how to autocomplete combobox text during the insertion in same combobox based record in prova1.
Tks.
Printable View
I have a mdb named test and table prova and filed prova1.
Now how to autocomplete combobox text during the insertion in same combobox based record in prova1.
Tks.
Moved From The CodeBank
see if this helps
Code:Option Explicit
Dim comboItemData As Integer
Private blnBackSpace As Boolean
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub Combo1_Change()
Dim i As Long, MyText As Long
'If firing in response to a backspace or delete, don't run the auto-complete
'complete code. (Otherwise you wouldn't be able to back up.)
If blnBackSpace = True Or Combo1.Text = "" Then
blnBackSpace = False
Exit Sub
End If
'Run through the available items and grab the first matching one.
For i = 0 To Combo1.ListCount - 1
If InStr(1, Combo1.List(i), Combo1.Text, vbTextCompare) = 1 Then
'Save the SelStart property.
MyText = Combo1.SelStart
Combo1.Text = Combo1.List(i)
comboItemData = Me.Combo1.ItemData(i)
'Set the selection in the combo.
Combo1.SelStart = MyText
Combo1.SelLength = Len(Combo1.Text) - MyText
Exit For
End If
Next i
End Sub
Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
If Combo1.Text <> "" Then
'Let the Change event know that it shouldn't respond to this change.
blnBackSpace = True
End If
End If
End Sub
Private Sub Command1_Click()
If Me.Combo1.Text <> "" Then
MsgBox Me.Combo1.ItemData(comboItemData)
Else
MsgBox "Please select an item from the list"
End If
End Sub
Private Sub Form_Load()
With Me.Combo1
Me.Combo1.AddItem ("Hello")
.ItemData(.NewIndex) = 0
Me.Combo1.AddItem ("Happy")
.ItemData(.NewIndex) = 1
Me.Combo1.AddItem ("birth")
.ItemData(.NewIndex) = 2
Me.Combo1.AddItem ("day")
.ItemData(.NewIndex) = 3
Me.Combo1.AddItem ("to")
.ItemData(.NewIndex) = 4
Me.Combo1.AddItem ("you")
.ItemData(.NewIndex) = 5
Me.Combo1.AddItem ("You")
.ItemData(.NewIndex) = 6
Me.Combo1.AddItem ("are")
.ItemData(.NewIndex) = 7
Me.Combo1.AddItem ("all")
.ItemData(.NewIndex) = 8
Me.Combo1.AddItem ("invited")
.ItemData(.NewIndex) = 9
Me.Combo1.AddItem ("the")
.ItemData(.NewIndex) = 10
Me.Combo1.AddItem ("party")
.ItemData(.NewIndex) = 11
Me.Combo1.AddItem ("while")
.ItemData(.NewIndex) = 12
Me.Combo1.AddItem ("coming")
.ItemData(.NewIndex) = 13
Me.Combo1.AddItem ("You")
.ItemData(.NewIndex) = 14
Me.Combo1.AddItem ("should")
.ItemData(.NewIndex) = 15
Me.Combo1.AddItem ("not")
.ItemData(.NewIndex) = 16
Me.Combo1.AddItem ("bring")
.ItemData(.NewIndex) = 17
Me.Combo1.AddItem ("any")
.ItemData(.NewIndex) = 18
Me.Combo1.AddItem ("food")
.ItemData(.NewIndex) = 19
End With
End Sub
by the way remove the me.Combo1 part appearing withinCode:With..End with
Here is a simple example.... Create a new form and add one combo to it and paste the entire code below it to test....
Hope this helps...Code:'~~> Needs 1 Form, 1 Combobox
Option Explicit
'~~> API Declarations
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) _
As Long
'~~> API Constants
Private Const CB_SELECTSTRING = &H14D
Private Sub Combo1_KeyPress(KeyAscii As Integer)
Dim Ret As Long
Dim sText As String
With Combo1
If Not (KeyAscii = vbKeyBack Or KeyAscii = vbKeyEscape Or _
KeyAscii = vbKeyReturn) Then
If .SelLength > 0 Then
sText = Left$(.Text, .SelStart) & Chr$(KeyAscii)
Else
sText = .Text & Chr$(KeyAscii)
End If
Ret = SendMessage(.hwnd, CB_SELECTSTRING, -1, sText)
If Ret <> -1 Then
.SelStart = Len(sText)
.SelLength = Len(.Text) - .SelStart + 1
Else
.Text = sText
.SelStart = Len(sText) + 1
End If
KeyAscii = 0
End If
End With
End Sub
Private Sub Form_Load()
'~~> Just a sample. You can add items from your table
Combo1.Clear
With Combo1
.AddItem "koolsid"
.AddItem "hack"
.AddItem "vbfnewcomer"
.AddItem "luca"
.AddItem "let me in"
.AddItem "blah blah"
.AddItem "kool"
.AddItem "hacker"
.AddItem "Etc..."
End With
End Sub