Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

So in order for the FormListener class to raise an event which passes all the bound field’s old and new values, or chooses the ones that have changed, I am feeling like I can create a testable object to do that.

Something like a FormIterator. It should be run in the “BeforeUpdate” instance of the form and will receive the form in that state. It will iterate through all the form fields and return a dictionary listing of form fields indexed by their name with an object containing the field name, old value, new value, and perhaps more… like we may eventually want to associate user labels with the field. Also associate the displayed value in a combo box with the bound value.

This is a precursor to being able to produce a list of changes for a particular BeforeUpdate Event which will then either be stored for consolidation and logging later, or at least logging.

Ok, so let’s use Test Driven Development to start creating the FormIterator class!

First I’m going to delete the tests:

  • FormListenerHearsFormBeforeUpdateEvent
  • FormListenerReturnsCountOfOneForTestTextFields
  • FormListenerReturnsCountOfZeroForTestTextField

The reason I’m deleting these tests is because I feel I was running down the wrong track with them. All I really want from this class is to get setup and listen to the form’s before update event.

Now I’m going to create a new test to instantiate the FormIterator class. This of course will fail because I have not created it yet.

Similar to the FormListener, here is my test code:

'@TestMethod("FormIterator")
Private Sub CreateFormIterator()
    Dim FormIteratorTest As New FormIterator
    Assert.Succeed
End Sub

Ok, since that fails, I will create my FormIterator class. Once the class is created with just 2 option lines (Option Compare Database, and Option Explicit) the database compiles and the test passes.

So far so good, now I create a test to setup the FormIterator. This will pass the FormIterator the Form it should be iterating. Here is the test code:

'@TestMethod("FormIterator")
Private Sub SetupFormIterator()
    Dim FormIteratorTest As New FormIterator
    FormIteratorTest.Setup NewForm
    Assert.Succeed
End Sub

This of course doesn’t compile because I need to add the Setup method to the FormIterator class.

Remember, I am using a setup method because VBA does not provide any way to pass parameters to an initialize routine. This means that we either have to use a factory pattern to build initialized objects with parameters, or add initializers to each of our objects. Next time we will get this test to pass and then start adding tests for iterating over the form control objects.