Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Smart search for combo box

Posted on 2002-06-26
4
Medium Priority
?
441 Views
Last Modified: 2008-03-17
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
0
Comment
Question by:garyw1
4 Comments
 
LVL 54

Expert Comment

by:nico5038
ID: 7112411
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)
0
 
LVL 1

Expert Comment

by:frankylew
ID: 7112768
Yes, agree with nico.
0
 
LVL 11

Accepted Solution

by:
joekendall earned 300 total points
ID: 7113986
This is how you get the hWnd of a control in Access. I also agree with Nico though that Access automatically will find an item in a combobox based on what you type in in the edit control of the combobox.

'-------------------------------------------------------------
'Taken from Dev Ashish web site www.mvps.org/access
'
'When an Access control on a form receives the focus,
'it becomes a true window and it's possible to retrieve
'it's handle by using the GetFocus API.   Note that
'because Access controls do not behave like VB controls,
'there's not a whole lot that we can do with the hWnd.
'
'Joe Kendall 8/13/2001
'-------------------------------------------------------------

Private Declare Function GetFocus Lib "user32" () As Long
         
Public Function fhWnd(ctl As Control) As Long
    On Error Resume Next
    ctl.SetFocus
    If err Then
        fhWnd = 0
    Else
        fhWnd = GetFocus
    End If
    On Error GoTo 0
End Function

Thanks!

Joe
0
 

Author Comment

by:garyw1
ID: 7146631
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.


0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question