Reputation: 3
I am making an improved version of a userform I got working earlier. The purpose of it is to select business data from a certain period per account in our system, and basically I have this part already working.
Now I find myself lost with something that is probably simple, but I don't know whats wrong.
So on the userform, I have two buttons. The first is labeled "Unselect All Months" and it sets the value of 12 checkboxes (which are named after months) to FALSE. So basically it's an on click event like this
Private Sub UnselectAllMonths_Button_click()
Dim j As Integer
For j = 0 To TimeAndSubForm.TimePeriodFrame.Controls.Count - 1
TimeAndSubForm.TimePeriodFrame.Controls(j).Value = "False"
Next j
End Sub
Then I have another button, which is supposed to do the opposite and set the value TRUE for all those checkboxes. Only it doesn't. It does the exact same thing as the above script, and here's how it looks.
Private Sub SelectAllMonths_Button_Click()
Dim j As Integer
For j = 0 To TimeAndSubForm.TimePeriodFrame.Controls.Count - 1
TimeAndSubForm.TimePeriodFrame.Controls(j).Value = "True"
Next j
End Sub
What am I doing wrong here?
If I would write the code without the variable j I get what I want and the checkboxes will be checked as I click the button.
TimeAndSubForm.TimePeriodFrame.Controls(0).Value = "True"
TimeAndSubForm.TimePeriodFrame.Controls(1).Value = "True"
'etc
That's basically it.
Upvotes: 0
Views: 160
Reputation: 3
Ok with StureS's input I understand what is happening now. The code goes through all controls in the frame, and activates those controls. Well, too bad some of the controls set the checkbox states to FALSE.
I can use do until loop here and everything works fine.
Private Sub SelectAllMonths_Button_Click()
Dim j As Integer
j = 0
Do Until j > 11
TimeAndSubForm.TimePeriodFrame.Controls(j).Value = "True"
j = j + 1
Loop
End Sub
I feel silly now, but thanks
Edit: Someone upvote StureS, I can't with my low reputation :)
Upvotes: 0
Reputation: 237
As I cannot comment I'll ask here: Did you troubleshoot by single-step through the code? What is the value of For j = 0 To TimeAndSubForm.TimePeriodFrame.Controls.Count - 1
If it is 0 then the loop won't be executed. Can you check?
Reading your comment but I didn't ask what the first value was, I asked what the value of TimeAndSubForm.TimePeriodFrame.Controls.Count was. Can you check that?
Upvotes: 0