Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

Here we are, refactoring this function:

Private Function SetFields_ChangeThem_ReturnDictionary(dctFieldName_arrStartAndEndVals As Dictionary) As Dictionary
    Dim testFormAuditor As FormAuditor
    Dim itm As Variant
    For Each itm In dctFieldName_arrStartAndEndVals
        ChangeFields (itm(0))
    Next itm
    ChangeFields Array("TestText", "TextBeforeValue"), Array("TestCombo", "ComboBeforeValue")
    Set testFormAuditor = New FormAuditor
    ChangeFields Array("TestText", "TextAfterValue"), Array("TestCombo", "ComboAfterValue")
    
End Function

It turns out that when you use For Each to loop over a dictionary, it actually loops over the dictionary keys not the values. So I’m going to change itm to key to make that obvious. Then I’m going to update ChangeFields to do the heavy lifting rather than leave the loop here in this helper function. I’ll leave the looping and saving in ChangeFields, but update ChangeFields to loop over the dictionary.

Oh man, this is SOO much better. Check out the SetFields_ChangeThem_ReturnDictionary function and the ChangeFields function now:

Private Sub ChangeFields(FieldChanges As Scripting.Dictionary, arrIndexForChange As Integer)
    Dim key As Variant
    For Each key In FieldChanges
        NewForm(key) = FieldChanges(key)(arrIndexForChange)
    Next arr
    NewForm.Dirty = False
End Sub

Private Function SetFields_ChangeThem_ReturnDictionary(dctFieldName_arrStartAndEndVals As Dictionary) As Dictionary
    Dim testFormAuditor As FormAuditor
    ChangeFields dctFieldName_arrStartAndEndVals, 0
    Set testFormAuditor = New FormAuditor
    ChangeFields dctFieldName_arrStartAndEndVals, 1
End Function

Oops, I forgot to set the second function to return the dictionary, but this is much more elegant and I am happy with the name changes. I’ll add the Set line to the second function to return the dictionary and run the tests again. Here’s the new version of the second function. I also had to update the “Next arr” to “Next key” in the ChangeFields function.

Private Sub ChangeFields(FieldChanges As Scripting.Dictionary, arrIndexForChange As Integer)
    Dim key As Variant
    For Each key In FieldChanges
        NewForm(key) = FieldChanges(key)(arrIndexForChange)
    Next key
    NewForm.Dirty = False
End Sub

Private Function SetFields_ChangeThem_ReturnDictionary(dctFieldName_arrStartAndEndVals As Scripting.Dictionary) As Scripting.Dictionary
    Dim testFormAuditor As FormAuditor
    ChangeFields dctFieldName_arrStartAndEndVals, 0
    Set testFormAuditor = New FormAuditor
    ChangeFields dctFieldName_arrStartAndEndVals, 1
    Set SetFields_ChangeThem_ReturnDictionary = testFormAuditor.ListOfChanges(1).FieldChanges
End Function

Note that the “testFormAuditor.ListOfChanges” is indexed starting with 1 as a collection, while as the arrays start with index 0. This is a little odd I think and confusing. Also, then I have to return the FieldChanges dictionary.

Anyway, even though the internals of the functions have oddities, I know what they are supposed to do by their names, and the tests now pass. Next I will refactor the test that uses these functions to try to make it more self documenting and remove some of the repetition. And of course, that will be next time.