Module: ComboBox in Category Microsoft Visual Basic 6 : Controls from Total Visual SourceBook

Routines to extend the functionality of a standard Visual Basic 6 (VB6) ComboBox control.

Some of these procedures expose features which are inherent in the Windows control, but are not exposed by VB6 (such as the ability to set the dropdown list width). Other procedures add entirely new functionality such as loading a DAO recordset into an unbound combo box.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modComboBox module.
AddItemDataComboBox Procedure Add an item to a combo box, and the itemdata value associated with that item, in a single function call.
ADORecordSetToComboBox Procedure Display the contents of an ADO recordset in a standard unbound combo box. Create a recordset object and pass it to this procedure. Your recordset can be based on a table, query or SQL statement. You may designate one field in the recordset to display in the combo box, and another field to store in the ItemData property. This lets you display a friendly field for the user to see, while storing an internal value like an ID number the user never sees. The value for the ItemData must be a storable in a long integer, not text values.
BinarySearchComboBox Procedure Use a binary search to find a value in a combo box. Combo box must have the Sorted property set to true. A binary search works by dividing the values to be searched in half. If the search value is less than the value at the halfway point, then the top half is searched. If the value is greater than the value at the halfway point, then the bottom half is searched. This process is repeated until the value is either found, or no more values remain to be searched. This technique requires that the combobox be sorted.
DropComboBoxList Procedure Cause a combo box to show or hide the list portion. This function can be used to automatically dropdown the list for a combo box when the user tabs into the combo box.
FindStringComboBox Procedure Find the line on a combo box containing the search string. This function uses the Windows API SendMessage command to quickly find a string in a standard combo box control. You may search for exact matches or partial matches on the beginning of the combo box item. Note that case is not considered in doing the search, even if the fExact argument is true.
IncrementalSearchCombo Procedure Allow incremental searching of a combo box (on the KeyPress event of the ComboBox). As the user types in the text portion of a combo box, this code finds the first item in the combo that matches the value the user has typed so far. If a value is found, the contents of the combo box text field is replaced. The portion of the text that the user did not type is selected so that it is immediately replaced as the user continues typing. This procedure must be called from the KeyPress event of the combo box. Pass the KeyAscii argument of the KeyPress event directly through to the intKeyAscii argument. The style property of the ComboBox must be "0 - Dropdown Combo" for this to work.
IsComboBoxDropped Procedure Determine if the list portion of a combo box is currently visible.
RecordSetToComboBox Procedure Display the contents of a DAO recordset in a standard unbound combo box. Create a recordset object and pass it to this procedure. Your recordset can be based on a table, query or SQL statement. You may designate one field in the recordset to display in the combo box, and another field to store in the ItemData property. This lets you display a friendly field for the user to see, while storing an internal value like an ID number the user never sees. The value for the ItemData must be a storable in a long integer, not text values.
SetComboBoxItemHeight Procedure Set the height of items in a combo box. VB6 normally uses the standard height for items in a combo box based on the type and size of the font in the combo box. Using Windows API calls you can specify a different height.
SetComboBoxListItems Procedure Set the number of items in the combo box list. This procedure lets you change the number of items that are displayed in the dropdown list portion of a combo box. Windows normally shows a default number of items. By changing this value you can designate the number of items that you want to appear. This might be useful if you have a combo box that displays the months of the year, for example, and you want to have twelve items displayed on the list. The number cannot be set to less than eight, or the actual number of items on the list, whichever is less.
SetComboBoxListWidth Procedure Set the width of the dropdown list portion of a combo box. The dropdown list in the standard VB6 Combo Box control is the same width as the combo box. This procedure sets the list portion wider than the combo box.
' Example of modComboBox
'
' To try this example, do the following:
' 1. Create a new form
' 2. Create a combo box named 'cboEmployee'
'    Set the following properties
'    Sorted True
' 3. Create a command button 'cmdTest'
' 4. Create a command button 'cmdTestData'
' 5. Create a command button 'cmdRecordsetData'
' 6. Create a command button 'cmdADORecordsetData'
' 7. Paste all the code from this example to the new form's module.

' This example assumes that the sample files are located in the folder named by the following constant.
Private Const mcstrSamplePath As String = "C:\TVSBSamp"
Private Const mcstrConnect As String = "Provider=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mcstrSamplePath & "\sample.mdb"

Private Sub cboEmployee_KeyPress(KeyAscii As Integer)
  ' Hook into the KeyUp event of the combo

  IncrementalSearchCombo cboEmployee, KeyAscii

End Sub

Private Sub Form_Load()
  ' Initialize the form

  cmdTest.Caption = "Test"
  cmdTestData.Caption = "Test Data"
  cmdRecordsetData.Caption = "Recordset Data"
  cmdADORecordsetData.Caption = "ADO Recordset Data"

  Call LoadSampleData

  ' Example of SetComboBoxItemHeight
  ' Increase height of items in combo box to 1.5 times the normal height
  SetComboBoxItemHeight cboEmployee, 1.5

  ' Example of SetComboBoxListItems
  ' Make combo box show 15 items
  SetComboBoxListItems cboEmployee, 15

  ' Example of SetComboBoxListWidth
  ' Double the current width of dropdown portion of a combo box
  SetComboBoxListWidth cboEmployee, 2

End Sub

Private Sub cboEmployee_GotFocus()
  ' Example of DropComboBoxList

  DropComboBoxList cboEmployee, True

End Sub

Private Sub cmdTest_Click()
  Dim intLine As Integer

  ' Example of BinarySearchComboBox
  intLine = BinarySearchComboBox(cboEmployee, "Ellis")

  If intLine >= 0 Then
    MsgBox "'Ellis' Found on line: " & intLine
  Else
    MsgBox "'Ellis' Not Found"
  End If

  ' Example of FindStringComboBox
  intLine = FindStringComboBox(cboEmployee, "Krusty", True)

  If intLine >= 0 Then
    MsgBox "'Krusty' Found on line: " & intLine
  Else
    MsgBox "'Krusty' Not Found"
  End If

  ' Example of IsComboBoxDropped
  If IsComboBoxDropped(cboEmployee) Then
    MsgBox "List is dropped"
  Else
    MsgBox "List is closed"
  End If

End Sub

Private Sub cmdTestData_Click()
  Call LoadSampleData
End Sub

Private Sub cmdRecordsetData_Click()
  Call LoadRecordsetData
End Sub

Private Sub cmdADORecordsetData_Click()
  Call LoadADORecordsetData
End Sub

Private Sub LoadSampleData()
  ' Simultaneously add a value for the Text portion and the ItemData portion of a combo box using AddItemDataComboBox

  cboEmployee.Clear

  AddItemDataComboBox cboEmployee, "Smith", 100
  AddItemDataComboBox cboEmployee, "Nehru", 101
  AddItemDataComboBox cboEmployee, "Proctor", 102
  AddItemDataComboBox cboEmployee, "Olsen", 103
  AddItemDataComboBox cboEmployee, "Davenport", 104
  AddItemDataComboBox cboEmployee, "Larue", 105
  AddItemDataComboBox cboEmployee, "Paulsen", 106
  AddItemDataComboBox cboEmployee, "Smithers", 107
  AddItemDataComboBox cboEmployee, "Chung", 108
  AddItemDataComboBox cboEmployee, "Haught", 109
  AddItemDataComboBox cboEmployee, "Ferguson", 110
  AddItemDataComboBox cboEmployee, "Ellis", 111
  AddItemDataComboBox cboEmployee, "Juth", 112
  AddItemDataComboBox cboEmployee, "Martin", 111
  AddItemDataComboBox cboEmployee, "Levy", 114
  AddItemDataComboBox cboEmployee, "Clinton", 115
  AddItemDataComboBox cboEmployee, "Belzer", 116

End Sub

Private Sub LoadRecordsetData()
  ' Example of RecordSetToComboBox

  Dim dbs As DAO.Database
  Dim rst As DAO.Recordset
  Dim strSQL As String

  strSQL = "SELECT ProductID, ProductName " & _
           "FROM Products " & _
           "ORDER BY ProductName "

  Set dbs = DAO.OpenDatabase(mcstrSamplePath & "\sample.mdb")

  Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)

  Screen.MousePointer = vbHourglass
  RecordSetToComboBox rst, cboEmployee, "ProductName", "ProductID"
  Screen.MousePointer = vbDefault

End Sub

Private Sub LoadADORecordsetData()
  ' Example of ADORecordSetToComboBox

  Dim rst As ADODB.Recordset
  Dim strSQL As String

  strSQL = "SELECT ProductID, ProductName " & _
           "FROM Products " & _
           "ORDER BY ProductName "

  Set rst = New ADODB.Recordset
  rst.Open strSQL, mcstrConnect

  Screen.MousePointer = vbHourglass
  ADORecordSetToComboBox rst, cboEmployee, "ProductName", "ProductID"
  Screen.MousePointer = vbDefault

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS