Image of Navigational Panel mapped to Contents / Home / Search VB3 Combobox with Database
Tech Support Question

Image of Line Break

Q. I hope you can help me figure this out.

In Access 2.0 you can use a combobox to select data from table A and place that selected data into table B, and have the option show up in the combobox when going through records in table B.

I have been using listboxes in VB to show all the data in a field and allow the user to select this, but I want to give the user a list of options from a combobox.

Thanks,
From: William Davis
Email: bdavis4@ix.netcom.com

A. Let's see if I can help you, what you are asking is not difficult. In fact I do a lot of development work in VB and use comboboxes almost as often as textboxes for data entry.

Unfortunately actually binding comboboxes is not supported in VB3 so you have to write some code. However the code is quite simple. All you need to do is have the combobox change its current setting whenever the data control repositions.

Fortunately, the reposition event is perfect for this:

  sub datMyDataControl_Reposition
    Dim iNum as integer

    ' Synchronise the status combo box with the record.
    ' First reset the listindex, in case there are no matches
    ' cboStatus.ListIndex = -1

    ' Now look for the item in the list.
    for iNum = 0 to cboStatus.ListCount -1
      ' Perform a case insensitive string compare
      if StrComp(cboStatus.List(iNum), 
                 datMyDataControl.recordset!Status, 1) = 0 then
        cboStatus.ListIndex = iNum
      end if
    Next iNum
  ...

You will also need to write any changes back to the data control when the selected item in the combobox changes.

  Sub cboStatus_Click
    ' Write newly selected item to the data control
    datMyDataControl.RecordSet!Status = cboStatus.Text
    ...

That's basically it. The above code will get the job done, it can be refined significantly. For example: The SendMessage API call can be used to select an item in a combo based on the passed string. The following code is a function that I keep in my library of useful things:

  Declare Function SendMessageString Lib "User" Alias "SendMessage" 
          (ByVal hWnd As Integer, ByVal wMsg As Integer, 
           ByVal wParam As Integer, ByVal lParam As String) As Long
  Global Const GG_WM_USER = &H400
  Global Const CB_SELECTSTRING = (WM_USER+13)
  Global Const LB_SELECTSTRING = (WM_USER+13)
  Sub SelectItem (lstX As Control, ByVal sSelect As String)
    Dim iPos As Long

    ' Search for the exact string in the list box or combo and select it.
    lstX.ListIndex = -1
    If TypeOf lstX Is ComboBox Then
      iPos = SendMessageString(lstX.hWnd, CB_SELECTSTRING, -1, sSelect)
    Else
      iPos = SendMessageString(lstX.hWnd, LB_SELECTSTRING, -1, sSelect)
    End If
  End Sub

I hope this helps you out, it's not as easy as it is with Access. However, it's not all that complex either. Good luck.


Image of Arrow to Previous Article Image of Arrow to Next Article

[TECH SUPPORT TOC]
Image of Line Break
[HOME] [TABLE OF CONTENTS] [SEARCH]