Link to home
Start Free TrialLog in
Avatar of garyw1
garyw1

asked on

Smart search for combo box

What is the correct code for doing combo box smart searches in Access?

I have written VB 6.0 applications that use smart search logic for combo boxes.  I have the need to use this type of logic on a form in an Access 97 database application, but the logic isn't the same.  One issue is that the SendMessage call in the VB code requires the handle of the combo box, while access only has a property (hWnd) for at the form/report level, not the control level.  Another issue is the difference in the way the combo list items are referenced.

Below are some pieces of my modified VB code for Access that doesn't work.  It always goes to the first item in the list, based on the using the form's window handle?  Any ideas on how to fix this?

Public Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _
    Long, ByVal wParam As Long, ByVal lParam As Any) As Long

Global Const CB_FINDSTRING = &H14C


Private Sub cboRepNum_KeyPress(KeyAscii As Integer)
    Dim intIndex As Long
    Dim strTemp As String
   
    'Figure out the string prefix to search for
    If cboRepNum.SelStart = 0 Then
        strTemp = cboRepNum.Text & Chr(KeyAscii)
    Else
        strTemp = Left(cboRepNum.Text, cboRepNum.SelStart) & Chr(KeyAscii)
    End If
   
    Dim intWindowHandle As Long
    intWindowHandle = Screen.ActiveForm.hwnd
   
    intIndex = SendMessage(intWindowHandle, CB_FINDSTRING, -1, strTemp)
    '-1 return code indicates failure to find the string
    If intIndex <> -1 Then
        'SendMessage returns the index of the first occurrence
        cboRepNum.ListIndex = intIndex
       
        'Set the text selection appropriately for the suggested match
        cboRepNum.SelStart = Len(strTemp)
        cboRepNum.SelLength = Len(cboRepNum.Text) - Len(strTemp)
        KeyAscii = 0
    End If
End Sub
Avatar of nico5038
nico5038
Flag of Netherlands image

Hi,

I'm not sure what you mean with the "combo box smart searche".
Normally in access the combobox already has a feature called "autoappend". This will show matches as long as they are present in the underlaying table/query.

Can you explain what you want to do "functionally" ?

Nic;o)
Avatar of frankylew
frankylew

Yes, agree with nico.
ASKER CERTIFIED SOLUTION
Avatar of joekendall
joekendall
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of garyw1

ASKER

The GetFocus function did not fix my broken code problem, but it did answer the windows handle question for an Access form control.  As for my solution I will just stay with my original option and work around the default functionality of the combo box control.  Thank you for your time.