Solved

auto-fill (like access) in dbcombo box

Posted on 1998-10-28
10
305 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now