Passing object to Subroutine

ino_mart

Registered User.
Local time
Today, 15:51
Joined
Oct 7, 2009
Messages
78
All

I have a form with a lot of listboxes. Each listbox got its own "Reverse All"-button in which selected items become unselected and vice-versa.

Instead of using the same code for each listbox, I try te do this with a subroutine which gets the name of the listbox as a parameter.

For some reason, I get Run-time error "2465" application defined or object defined error

The current code is
Code:
Private Sub cmdReverseCountry_Click()
ReverseListbox "cboReport"
End Sub
 
Private Sub cmdReverseRegion_Click()
ReverseListbox "cboRegion"
End Sub
 
Public Sub ReverseListbox(strX As Variant)
Dim intCounter As Integer
For intCounter = 0 To Form.strX.ListCount - 1
    Form.strX.Selected(intCounter) = Not Form.strX.Selected(intCounter)
Next intTeller
End Sub
 
I just found the solution. I forgot to add "controls"

Code:
Public Sub ReverseListbox(strX As String)
Dim intCounter As Integer
For intCounter = 0 To Form.Controls(strX).ListCount - 1
    Form.Controls(strX).Selected(intcounter) = Not Form.Controls(strX).Selected(intCounter)
Next intCounter
 
End Sub

Issue is solved
 
Just in case you form the wrong idea, adding Controls was not the fix:-

For intCounter = 0 To Form.strX.ListCount – 1
verses
For intCounter = 0 To Form(strX).ListCount – 1

.strX. uses strX as a string literal whereas (strX) uses strX as a variable.

Chris.
 

Users who are viewing this thread

Back
Top Bottom