So far, the test
- Creates a new Form Listener object
- Creates a new Form object as a Test Form
- Opens the Test Form
- Runs the Setup method of the Form Listener object
- Updates a field on the Test Form
- Saves the Test Form Record
- Checks the BeforeUpdateTriggered Property of the Form Listener object which should now be true
I think this was the simplest next test I could have written to move my code along. Basically I have a setup method, although it doesn’t actually do anything. I’m not really sure I need a publicly available method on the Form Listener, but right now I need it for the test. Maybe I will remove it later… But for now this will force me to write the next bit of code I want to write.
The idea here is to write tests before you write code. The test should fail because the code doesn’t do what the test expects yet. Doing this consistently from beginning to end will hopefully yield a suite of tests that I will be able to run to make sure any particular arbitrary change I make doesn’t cause past tests to fail, thereby making sure my code is stable and not fragile.
So now in my Setup routine, I am going to start adding the code needed. Just enough to make the test pass.
My entire FormListener class so far just looks like this:
Option Compare Database
Option Explicit
Public Sub Setup(theForm As Access.Form)
End Sub
Very simple. Just enough to get my prior initialization test to pass.
Now I’ll need to add an Access.Form variable with events.
Then I will need to initialize the variable with the form passed to the Setup method.
Next I add a new event for my “frm” variable to hook the BeforeUpdate event.
In that event code I will need to set the new property to True, but first I need to add the Property. I’m going to use the MZ Tools New Method / Property Assistant to create it:
Then after I click Insert, the code will be added to my module.
So now after all my work, my entire class module looks like this:
Option Compare Database
Option Explicit
Private WithEvents frm As Access.Form
Private m_bBeforeUpdateTriggered As Boolean
Public Sub Setup(theForm As Access.Form)
Set frm = theForm
End Sub
Public Property Get BeforeUpdateTriggered() As Boolean: BeforeUpdateTriggered = m_bBeforeUpdateTriggered: End Property
Public Property Let BeforeUpdateTriggered(ByVal bNewValue As Boolean): m_bBeforeUpdateTriggered = bNewValue: End Property
Private Sub frm_BeforeUpdate(Cancel As Integer)
Me.BeforeUpdateTriggered = True
End Sub
And now the moment of truth! Will it the test pass with this code? The answer is no.
And why not? Probably because since I am trying to hook into an Access Form and the Test Form does not have the “BeforeUpdate” property set to “[Event Procedure]”, I will need to do that in my code also.
So I add the line frm.BeforeUpdate = “[Event Procedure]” to the Setup method. And now my full class code looks like this:
Option Compare Database
Option Explicit
Private WithEvents frm As Access.Form
Private m_bBeforeUpdateTriggered As Boolean
Public Sub Setup(theForm As Access.Form)
Set frm = theForm
frm.BeforeUpdate = "[Event Procedure]"
End Sub
Public Property Get BeforeUpdateTriggered() As Boolean: BeforeUpdateTriggered = m_bBeforeUpdateTriggered: End Property
Public Property Let BeforeUpdateTriggered(ByVal bNewValue As Boolean): m_bBeforeUpdateTriggered = bNewValue: End Property
Private Sub frm_BeforeUpdate(Cancel As Integer)
Me.BeforeUpdateTriggered = True
End Sub
And now the test passes!