Solved

auto-fill (like access) in dbcombo box

Posted on 1998-10-28
10
311 Views
Last Modified: 2013-12-26
I would like to get my dbcombo box to "auto-fill" .... as I type in the combo it finds the matching list item and completes it in the box.  I can do this in a regular combo box using the send message api and the list index property.  dbcombo doesn't have a list index property so I can't do it that way.  Any help would be appreciated.
0
Comment
Question by:hadermann
10 Comments
 
LVL 3

Expert Comment

by:traygreen
ID: 1487998
Put the following code in the combo...

Private Sub cboAccess_KeyUp(Index As Integer, KeyCode As _
                            Integer, Shift As Integer)
   Call cboAddChar(cboAccess(Index), KeyCode, Shift)
End Sub

and put the following code in a module ....

Public Function cboAddChar(pComboListBox As Object, pKey As Integer, pShift As Integer) As Long
' This function will check to see if the text value of the combo
' box is equal to any of the list items.  The list will be check
' only if the shiftstate is not control or alt and if the key is
' a control character

   Dim lResult As Long
   
   On Error GoTo Err_cboAddChar
   lResult = -1
   Select Case pKey
      Case 0 To 31
      Case vbKeyEnd, vbKeyHome, vbKeyLeft, vbKeyUp, _
           vbKeyRight, vbKeyInsert, vbKeyDelete

         If pShift = 2 Then 'control key pressed
            lResult = cboSelString(pComboListBox)
         End If

      Case Else
         If pShift < 2 Then 'shift key or none
            lResult = cboSelString(pComboListBox)
         End If
   End Select
   
   cboAddChar = lResult
   
   Exit Function
   
Err_cboAddChar:
   Select Case Err.Number
      Case Else
         cboAddChar = -1
         Exit Function
   End Select
End Function

Public Function cboSelString(pComboListBox As Object) As Long
'this function works like a quick fill in Quicken. This function is called
'in a change event of a combobox and finds the item in the list based on the
'first characters that the user types. If it finds a match, the user's input
'is left unhighlighted while the remaining characters are highlighted.

   Dim nIndex As Long
   Dim cSearch As String
   Dim cFound As String
   
   ' get the contents in input box
   cSearch = LTrim(pComboListBox.Text)  

   nIndex = SendMessage(pComboListBox.hwnd, CB_FINDSTRING, -1, _
                        cSearch)
   If (nIndex <> -1) Then           ' -1 means found nothing
      pComboListBox.ListIndex = nIndex 'display the found item
      cFound = pComboListBox.List(nIndex)
      If (Len(cFound) >= Len(cSearch)) Then 'highlight remainder
         pComboListBox.SelStart = Len(cSearch)
         pComboListBox.SelLength = Len(cFound) - Len(cSearch)
      End If
   End If
   cboSelString = nIndex     'returns -1 if not found, else returns index
End Function

0
 

Author Comment

by:hadermann
ID: 1487999
As noted, I'm using the dbcombo control.  this control does not have a listIndex property.  Therefore I get an error at: pComboListBox.ListIndex

This works with a standard combo but not a dbcombo.

Any way of doing this without the listindex property?
0
 
LVL 1

Expert Comment

by:zivf
ID: 1488000
You need to use the FindString API and sent it the hWnd property of the combo/list as a parameter.

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:hadermann
ID: 1488001
Could you give me a little more detail on how to go about using the FindString API ?
0
 
LVL 2

Expert Comment

by:kswinney
ID: 1488002
Set the "MatchEntry" property of the dbCombo to "1 - dblExtended Matching".

That's all you have to do.
0
 

Author Comment

by:hadermann
ID: 1488003
ok, as I type in the text area I want to have the text automatically filled out for me in the text area, not in the combo area.  For example, if I type 'Fr' and French Fries is in the list it would display 'French Fries' with the 'ench Fries' highlighted.  I can do this in a regular combo box, but because there is no listindex property in the dbcombo I'm looking for a way to do it without the listindex property.
0
 
LVL 2

Expert Comment

by:kswinney
ID: 1488004
hadermann,

Did you try my answer before you rejected it?  It does exactly as you want.  If you type something (not just one character) that's in the list, it highlights the correct item from the list.

You'll also need to set the style to dropdown list.

0
 

Author Comment

by:hadermann
ID: 1488005
If it's a DropDownList that kind of defeats the purpose of having a combo box.  I can only type in values that are on the list.  I need to be able to type in values that are not on the list.
0
 
LVL 2

Accepted Solution

by:
kswinney earned 100 total points
ID: 1488006
Try this:

if DBCombo1.MatchedWithList Then
    'They typed exactly something in the list
    Data1.Recordset.Bookmark = DBCombo1.SelectedItem
Else
    'Otherwise, I'll need to search for a match
    Data1.Recordset.FindFirst DBCombo1.ListField & _
        " LIKE '" & dbCombo1.Text & "'"
    If Data1.Recordset.NoMatch Then
        MsgBox "I didn't find a record with the text you typed."
    else
        'Otherwise, the bookmark is currently on the right rec
    End If
End If

This should do what you need.
0
 

Author Comment

by:hadermann
ID: 1488007
Used part of your answer.  Here's what I'm using now.  Thanks!!

Private Sub DBCombo1_KeyPress(KeyAscii As Integer)
    Dim FindString As String
   
    If KeyAscii < 32 Or KeyAscii > 127 Then Exit Sub
    If DBCombo1.SelLength = 0 Then
        FindString = DBCombo1.Text & Chr$(KeyAscii)
    Else
        FindString = Left$(DBCombo1.Text, DBCombo1.SelStart) & Chr$(KeyAscii)
    End If
    DBCombo1.Text = FindString
    If DBCombo1.MatchedWithList Then
       Data1.Recordset.Bookmark = DBCombo1.SelectedItem
    Else
       DBComboText = DBCombo1.Text
       DBCombolen = Len(DBComboText)
       Data1.Recordset.FindFirst DBCombo1.ListField & _
       " LIKE '" & DBCombo1.Text & "*'"
    End If
    If Data1.Recordset.NoMatch Then
       DBCombo1.Text = DBComboText
       DBCombo1.SelStart = Len(DBCombo1.Text)
    Else
       DBCombo1.Text = Data1.Recordset!RecordingArtist
       DBCombo1.SelStart = DBCombolen
       DBCombo1.SelLength = Len(DBCombo1.Text) - DBCombolen
    End If
       KeyAscii = 0
    End Sub
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

821 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