Ken Sheridan
Active member
- Local time
- Today, 23:21
- Joined
- Jul 10, 2025
- Messages
- 329
All I'm trying to do is figure which listbox is visible and then grab that for other events. What I'm struggling with is calling the above function. I've figured out I need to make it a string
By specifically naming the two possible controls your code is very restrictive. The following, on the other hand, allows you to reference any form which contains one visible list box control. The number of list box control's in the form is completely arbitrary form 1 to n, as long as only one is visible at any one time:
Code:
Public Function GetList(frm As Form) As Control
Dim ctrl As Control
For Each ctrl In frm.Controls
If ctrl.ControlType = acListBox Then
If ctrl.Visible Then
Set GetList = ctrl
End If
End If
Next ctrl
End Function
You can then call it by passing a reference to the form into the function, e.g. in the same form's module:
Code:
Dim ctrl As Control
Set ctrl = GetList(Me)
If you do need the name of the control as a string expression you can return the control object's Name property:
Code:
Debug.Print ctrl.Name