Better Way to Code this?

D

DS

I'm using this code that works. Is there a better way to do this? It
checks 3 Listboxes and then acts accordingly.
Thanks
DS

Private Sub List66_Click()
Me.Text95 = 1
Dim I As Integer

For I = 0 To Me.List79.ListCount - 1
Me.List79.Selected(I) = False
Next I
For I = 0 To Me.List81.ListCount - 1
Me.List81.Selected(I) = False
Next I
For I = 0 To Me.List83.ListCount - 1
Me.List83.Selected(I) = False
Next I
End Sub
 
A

Amy Blankenship

What about
Dim ArrControls() As Variant, Control as Variant, i as Integer
ReDim ArrControls(Me.List79, Me.List81, Me.List83)



for each Control in ArrControls
for i = 0 to Control.Listcount-1
Me.Control(i) = false
Next i
Next Control

Note this is off the top of my head and I have no idea if you can pop
references to controls into an array like this.

HTH;

-Amy
 
D

DS

Amy said:
What about
Dim ArrControls() As Variant, Control as Variant, i as Integer
ReDim ArrControls(Me.List79, Me.List81, Me.List83)



for each Control in ArrControls
for i = 0 to Control.Listcount-1
Me.Control(i) = false
Next i
Next Control

Note this is off the top of my head and I have no idea if you can pop
references to controls into an array like this.

HTH;

-Amy
No, It came back with an Error Message, Method or Data Member not found,
it stopped on the word control.
Thanks
DS
 
S

SteveS

DS said:
I'm using this code that works. Is there a better way to do this? It
checks 3 Listboxes and then acts accordingly.
Thanks
DS

Private Sub List66_Click()
Me.Text95 = 1
Dim I As Integer

For I = 0 To Me.List79.ListCount - 1
Me.List79.Selected(I) = False
Next I
For I = 0 To Me.List81.ListCount - 1
Me.List81.Selected(I) = False
Next I
For I = 0 To Me.List83.ListCount - 1
Me.List83.Selected(I) = False
Next I
End Sub

You are using the "brute force" method (not that this is bad - I use it a lot);
setting looping thru each row, setting the "Selected" property to FALSE.

I don't know it this is a "better" way, but you could use the "ItemsSelected"
property and only set the rows that are selected to FALSE.

Here is the code:

'****************************
Private Sub Command11_Click()
Dim i As Integer
Dim varItm As Variant
Dim ctl As Control

For i = 1 To 3
Select Case i
Case 1
Set ctl = Forms!form2!List79
Case 2
Set ctl = Forms!form2!List81
Case 3
Set ctl = Forms!form2!List83
End Select

'MsgBox ctl.Name & " " & ctl.ItemsSelected.Count

For Each varItm In ctl.ItemsSelected
ctl.Selected(varItm) = False
Next varItm
Next i
End Sub
'****************************

Since listboxes generally have very few rows, there should be no apparent
difference in execution time between the two ways of clearing the list boxes.

HTH
 
D

DS

SteveS said:
You are using the "brute force" method (not that this is bad - I use it
a lot); setting looping thru each row, setting the "Selected" property
to FALSE.

I don't know it this is a "better" way, but you could use the
"ItemsSelected" property and only set the rows that are selected to FALSE.

Here is the code:

'****************************
Private Sub Command11_Click()
Dim i As Integer
Dim varItm As Variant
Dim ctl As Control

For i = 1 To 3
Select Case i
Case 1
Set ctl = Forms!form2!List79
Case 2
Set ctl = Forms!form2!List81
Case 3
Set ctl = Forms!form2!List83
End Select

'MsgBox ctl.Name & " " & ctl.ItemsSelected.Count

For Each varItm In ctl.ItemsSelected
ctl.Selected(varItm) = False
Next varItm
Next i
End Sub
'****************************

Since listboxes generally have very few rows, there should be no
apparent difference in execution time between the two ways of clearing
the list boxes.

HTH
Thanks
I knew there was a better way! I'll put it to good use!
Sincerely
DS
 

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

Top