combo = ItemData(0) if listcount = 1, public sub

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to use the following code on several forms where 1 or 2 comobos
will sometimes have a listcount of 1. When i call this sub with
call SizeList1(Me)
i get a message saying 'object doesn't support this property or method'.

Is there any way that i can do this with a public sub?

Public Sub SizeList1(frm As Form)
'On Error GoTo SubErr
Dim ctl As Control
Set ctl = Screen.ActiveControl
With ctl
If .ListCount = 1 Then
.ctl = .ctl.ItemData(0)
End If
End With
Set ctl = Nothing
 
When are you calling this routine. The routine expects the combo box to have
the focus on the form and be on the form that has the focus at the time the
code is called. Instead of passing the form, you may want to pass the
control.
 
Thanks Wayne.
This routine was called from the GotFocus event of the combo.
Could you help with the syntax to pass the control instead of the form. I am
just learning how to write public routines and call them from form events.
 
Right now, you're presumable calling the routine as

SizeList1 Me

or

Call SizeList1(Me)

(or perhaps you're using Forms!MyFormName instead of Me)

To pass the control to it, you'd change the routine to

Public Sub SizeList1(ctl As Control)
'On Error GoTo SubErr
With ctl
If .ListCount = 1 Then
.ctl = .ctl.ItemData(0)
End If
End With

and invoke it using

SizeList1 Me.MyComboBox

or

Call SizeList1(Me.MyComboBox)

where MyComboBox is the name of the combobox.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top