Results 1 to 5 of 5

Thread: Autocomplete Combobox

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,939

    Autocomplete Combobox

    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.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Autocomplete Combobox

    Moved From The CodeBank

  3. #3
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Smile Re: Autocomplete Combobox

    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

  4. #4
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Autocomplete Combobox

    by the way remove the me.Combo1 part appearing within
    Code:
    With..End with

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Autocomplete Combobox

    Here is a simple example.... Create a new form and add one combo to it and paste the entire code below it to test....

    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
    Hope this helps...
    Last edited by Siddharth Rout; Feb 23rd, 2009 at 08:13 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width