• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

auto-fill (like access) in dbcombo box

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
hadermann
Asked:
hadermann
1 Solution
 
traygreenCommented:
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
 
hadermannAuthor Commented:
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
 
zivfCommented:
You need to use the FindString API and sent it the hWnd property of the combo/list as a parameter.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
hadermannAuthor Commented:
Could you give me a little more detail on how to go about using the FindString API ?
0
 
kswinneyCommented:
Set the "MatchEntry" property of the dbCombo to "1 - dblExtended Matching".

That's all you have to do.
0
 
hadermannAuthor Commented:
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
 
kswinneyCommented:
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
 
hadermannAuthor Commented:
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
 
kswinneyCommented:
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
 
hadermannAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now