Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

It’s time to add a new test:

'@TestMethod("Initialize")
Private Sub WhenThereIsAForm_ThenItCanBeAssigned()
    Dim testFormAuditor As FormAuditor
    Set testFormAuditor = New FormAuditor
    testFormAuditor.Init Forms("TestForm")
End Sub
Visual Basic

This test is in my new RubberDuck test module “TestGivenFormAuditorInit”.

It won’t even compile yet because I don’t have an Init function.

Currently, the FormAuditor is linked specifically to the Test Form using the Class_Initialize event which looks like this:

Private Sub Class_Initialize()
    Set FormToAudit = Forms("TestForm")
    FormToAudit.BeforeUpdate = "[Event Procedure]"
    arrTrackedControlTypes = Array(acComboBox, acTextBox, acCheckBox, acOptionGroup)
End Sub
Visual Basic

I am going to change this from the Class_Initialize (which won’t take any parameters) to the Init function and then have the Class_Initialize function call the Init function like this:

Private Sub Class_Initialize()
    Init Forms("TestForm")
End Sub

Public Sub Init(theForm As Access.Form, Optional ArrayOfTrackedControlTypes As Variant)
    Set FormToAudit = Forms("TestForm")
    FormToAudit.BeforeUpdate = "[Event Procedure]"
    If IsMissing(ArrayOfTrackedControlTypes) Then ArrayOfTrackedControlTypes = Array(acComboBox, acTextBox, acCheckBox, acOptionGroup)
    arrTrackedControlTypes = ArrayOfTrackedControlTypes
End Sub
Visual Basic

So I have made the Array of tracked control types into an optional parameter and am assigning a default set of controls if they aren’t specified.

This code allows all my other tests to pass because they are relying on the Class_Initialize to setup this form. However, I am going to want to update my tests to specifically add the TestForm. I was hoping the only place I needed to do this was in the ModuleInitialize() function, but it turns out that is not the case. I am going to have to find any instance of FormAuditor I am declaring and refactor it to use the new init method.

Before I do that, I want the current test to pass and it will if I add the same form open commands to the ModuleInitialize function in the new module. So I’ll do that first so I am passing all tests. Then next time I can refactor the other tests.

Sign up For a Daily Email Adventure in Microsoft Access

Every business day (typically M-F), I'll send you an email with information about my ongoing journey as an advanced Access application developer. It will be loaded with my tips and musings.

    We won't send you spam. Unsubscribe at any time.