Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

Our next test will be to add an Option Group to the form and test that. Now Access has some weird caveats for option groups.

You can have radio buttons, checkboxes, or toggle buttons in an Option Group. Each button or box can be labeled, but can only have a numeric value, not a text string. And so when you bind the option group, you will only ever get numeric fields in your bound field, whatever it is. Furthermore, they must be integers and cannot exceed the long integer variable type range.

Kinda weird and pretty limiting as far as representing the Option Group in the database as something not abstracted to an integer. On the other hand, it could easily link 2 tables together when you have a table of limited options like statuses and they each have a type Long ID, you could hard code them to another table.

Anyway, this isn’t to wax poetic on the wonderful uses and features of the Access Option Group, but just to test that it works properly. So I’ve added the field to the table and a new option group to the form and bound it to the field.

Now I’ll create the test. So the test is just like our last test just changing some of the function parameters:

'@TestMethod("Verify Changes")
Private Sub WhenOptionGroupFieldChangesBeforeAndAfterValuesAreReturned()
    Dim dctInputs As New Scripting.Dictionary, colResults As VBA.Collection
    Set dctInputs = CreateAndAddToInputDict(FieldToChange:="TestOptionGroup", InitialValue:=1, ChangeTo:=2)
    Set colResults = SetFields_ChangeThem_ReturnNewListOfChanges(dctInputs)
    Assert.IsTrue FieldInputsMatchResults(dctInputs, colResults(1).FieldChanges)
End Sub

Now again, this doesn’t pass because I have specific field types I’m checking for and I haven’t added the acOptionGroup control type to the list yet. But as soon as I do that:

Private Sub FormToAudit_BeforeUpdate(Cancel As Integer)
    Dim Fld As Variant, ChangeDictionary As New Scripting.Dictionary, Ctl As Access.Control
    For Each Fld In FormToAudit
        Set Ctl = Fld
        Select Case Ctl.ControlType
            Case acComboBox, acTextBox, acCheckBox, acOptionGroup
            Case Else: GoTo SkipLoop
        End Select
        If FieldChanged(Ctl) Then ChangeDictionary.Add Ctl.Name, CreateAuditFieldChange(Ctl)
SkipLoop:
    Next Fld
    If ChangeDictionary.Count > 0 Then pListOfChanges.Add CreateAuditEventDetails(ChangeDictionary)
End Sub

Changing this function was all I had to do to get the test to pass.

I played a bit with a single radio button and you can bind it to a field and it acts like a checkbox. You can use it by itself in dual or triple state also. There is no difference between the displayed value of null or false though.

This also made me wonder what would happen if I bind 2 different fields to the same thing. What will the Form Auditor do. I will find out next time!