Solved

auto-fill (like access) in dbcombo box

Posted on 1998-10-28
10
309 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bit defender blocks good applications 2 94
Windows Script Host failed (Access is denied.) error 6 149
VB6 - Convert HH:MM into Decimal 8 56
Added a column screws up code 5 57
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

810 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