I am seeing behaviour regarding checkboxes on a form which seems really weird, and I'm wondering if anyone else has come across it and can explain it.
Here's the form, which is bound to a table called FrequencyCodes:
Although the form is bound, the checkboxes are unbound. On the Form's load event they are populated by a procedure which reads a comma-separated string (in the column "CleaningDays") of numbers from 1-7, checking boxes appropriately. Here's that code:
Dim ctl As Control For Each ctl In Me.Controls If TypeOf ctl Is CheckBox Then ctl.Value = InStr(1, Me.CleaningDays & ",", Right(ctl.Name, 1) & ",") End If Next ctl
The idea is that if a pattern of "One or more times per week" is chosen, the specific days can then be selected. For reasons of visual consistency with the option buttons above (for the cleaning pattern), I added an option group control to group the checkboxes together visually. I added the checkboxes, then so that I could select multiple options (which obviously isn't possible if they're part of an Option Group, I cut the checkboxes from the form, pasted them elsewhere on the form and then dragged them back into position. This resulted in a set of "standard" checkboxes, with possible values of true or false, rather than option values of 1 - 7 or whatever, had they been part of the option group. Here's the property sheet for one of them:
I then created code which, on the Click event of the OK button, loops through the checkboxes, and by using the last character of their names (they're called chk1 - chk7) builds a comma-separated list of the days when cleaning happens.
Here's the code:
Dim ctl As Control
Dim Output As String
For Each ctl In Me.Controls
If TypeOf ctl Is CheckBox
If (ctl = True) Then
Output = Output & Right(ctl.Name, 1) & ","
End If
End If
Next ctl
If Output <> vbNullString Then
Debug.Print "String calculated was " & Left(Output, Len(Output) - 1)
ReadCheckBoxes = Left(Output, Len(Output) - 1)
Else
ReadCheckBoxes = vbNullString
End If
The form wasn't working as expected, so I did some testing. The checkboxes weren't seeming to return Booleans (or 1 and 0) as expected.
You can see from the screenshot above that in this particular test, I'd selected options for Monday - Friday (chk2 - chk6 should have a value of true).
However, here's what I found from doing some testing in the immediate window:
? chk1
0
?chk2
-1
?chk3
-1
?chk4
1
? chk5
-1
?chk6
3
?chk7
0
Now, I know that any non-zero value equates to true, and that therefore these values should work. But they weren't. I was finding that on click on OK, only changed values would be recorded correctly - so, if, when the form was loaded, Monday - Friday were ticked, and I then tick Saturday before clicking OK, only chk7 (Saturday) would be recorded as being selected.
Can anyone explain this behaviour to me? I can't see the OptionValue property which I'd expect to see if these checkboxes were still part of an option group, yet this seems to be the value being returned.
Thank you!
Aucun commentaire:
Enregistrer un commentaire