Solved

auto-fill (like access) in dbcombo box

Posted on 1998-10-28
10
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 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