Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

handle on listbox control

Status
Not open for further replies.

foreveryoung

Programmer
Sep 24, 2002
45
GB
Ideally I would like a drop down combobox with checkboxes in it but cant find that functionality. I am left with using the Microsoft Windows Forms Listbox 2 control with the liststyle set to "option".

I want to use the getselitems API on this control but do not know how to determine the handle to the control. If anyone knows how I can determine the handle of any Access control then this would be most useful, or is there a combobox out there that can use checkboxes on the drop down part.

Thanks Dave
 
Sorry to dissapoint you, but unlike VB, I don't believe that the individual controls on an Access form have an hwnd handle. I'm not entirely sure on this, but I believe that I am correct. Please remember to give helpful posts the stars they deserve! This makes the post more visible to others in need![thumbsup]
 
You don't need to go to the API on this one, unless you really want to.

Here's how I get the selected items from the MSForms.Listbox control (attached to a Commandbutton control, but could be used in other ways):

Code:
Private Sub cmdGetSelItems_Click()
  Dim strValues() As String
  Dim i As Integer
  
  ' lstCustomers is an MSForms.Listbox control
  strValues = GetSelItems(Me.lstCustomers)
  
  ' print the returned items
  For i = 0 To UBound(strValues)
    Debug.Print strValues(i)
  Next i
  
End Sub


' Have to use a late-bound reference here
Public Function GetSelItems(ByRef ListControl As Object) As String()
  
  Dim strValues() As String
  Dim lngCtr As Long
  
  With ListControl
    For lngCtr = 0 To .ListCount - 1
      If .Selected(lngCtr) = True Then
        ReDim Preserve strValues(lngCtr)
        strValues(lngCtr) = .List(lngCtr)
      End If
    Next lngCtr
  End With
  
  ' return the array of selected values
  GetSelItems = strValues
  
End Function
VBSlammer
redinvader3walking.gif
 
Correction on the function - I forgot to include a separate counter for the array:

Code:
Public Function GetSelItems(ByRef ListControl As Object) As String()
  
  Dim strValues() As String
  Dim lngCtr As Long
  Dim lngIndex As Long
  
  With ListControl
    For lngCtr = 0 To .ListCount - 1
      If .Selected(lngCtr) = True Then
        ReDim Preserve strValues(lngIndex)
        strValues(lngIndex) = .List(lngCtr)
        lngIndex = lngIndex + 1
      End If
    Next lngCtr
  End With
  
  GetSelItems = strValues
  
End Function
VBSlammer
redinvader3walking.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top