Passing Arg Type of Control to a Public Sub

T

Terry

I have a public sub that references a control from the calling form, Dim ctl
as Control. The control happens to be a ListBox, however I have two of these
on the main form and would like to use the same Public Sub with each.

As both ListBoxes have different name properties how do I pass the reference
in the subs arguments so that I can use it in the ctl variable?

Regards
 
D

Douglas J. Steele

Pass the control to the sub:

Declare a Control parameter in the sub's declaration:

Sub MySub(Parameter3 As Control)

Pass a reference to your list box when calling the sub:

Call MySub(Me!Listbox1)

or

Call MySub(Me!Listbox2)
 
T

Terry

Hi Douglas,

Tried your suggestion and get "wrong number of arguments" thrown back.
Anything else?

Regards

Terry
 
T

Terry

Hi Douglas,

As a means to test whether it would fail with any other variable types I ran
the following, it would be neater to pass the control ref. Note that the
form is bound to a class as below. The Show_Invoice_Status sub will happily
run but will not compile giving the "wrong number of arguments, or invalid
property assignment" message!

The main form has a couple of listboxes on different tabs that show very
similar data, but have to have different names of course. There are
listboxes on subforms that also show similar data. The Show_Invoice_Status
sub can reference anyone of them depending on which form/subform the call
originates from through the bound form object. I'm starting to think I
should simplify things a bit more withn these listboxes.

-----
Private mfrmBound As Form ' The form bound to object
Private mlblStatus As Label ' Label to report to

Public Sub BindForm(FormRef As Form, LabelRef As Label)
' This procedure is used to bind a form to the CommonFormUpdates object
Set mfrmBound = FormRef
Set mlblStatus = LabelRef
End Sub


Public Sub Show_Invoice_Status(strControl As String)
Dim ctl As Control

' Set reference to list control
'Set ctl = mfrmBound!lstInvoicingProfileTxns
If strControl = "lstInvoicingProfileTxns" Then
Set ctl = mfrmBound.lstInvoicingProfileTxns
Else
Set ctl = mfrmBound.lstInvoicingProfile
End If
 
D

Douglas J Steele

You're not showing me how you're attempting to call Show_Invoice_Status. I
need to see the line of code that's raising the error when you try to
compile.
 
T

Terry

Hi Douglas,

I'm was using and get the error on the following (when the arg is of type
Control)

Call mfrmBound.Show_Invoice_Status(Me.lstInvoiceProfile)

Regards

Terry
 
T

Terry

Hi Douglas,

Fixed it thanks. Found I was referencing the bound form incorrectly. Thanks
for you help.

Regards

Terry
 
Top