Hmmm… So it turns out you can’t just create a form using:
Dim aNewForm As New Access.Form
I was trying to simply pass a new form object to a class I eventually want to hook into it’s event forms.
So, next I tried to actually create a blank form in the database. Then I called it “TestForm”. It was created simply by creating a Blank Form in the Create Ribbon.
This also fails. I cannot declare an object of type: Form_TestForm.
I cannot set a new form object variable to: Forms(“TestForm”).
Frankly I find all this rather weird. I guess Access needs more to go on? Or is my database corrupt?
Ok, well, how about I add a control to the form? I’ll add an unbound text field.
Nope. I’m getting an error 2450: Cannot find the referenced form.
Perhaps I need a table in the database. So let’s create TestTable with an autonumber ID and a TestText field.
Nope, still getting the same error. Now let’s bind the form to the table and see if anything changes.
YES!!! Aha, now the test suddenly passes. VBA now sees the form. It has been internally “registered” with Access.
Uhh… so wait, now I have saved the form and reran the RubberDuck analysis and now even though I didn’t change anything, it’s still not finding the form.
Ohh… ok, so I realize what I’m doing wrong here. The form needs to be open for the code to work. If the form isn’t open yet the test will fail because it needs to be referencing an open form from the Forms collection.
That actually make sense. Realizing my silliness, I can now code the test module like this to get the code to pass:
'@TestMethod("FormListener")
Private Sub SetupFormListener()
On Error GoTo TestFail
'Arrange:
Dim FormListenerTest As New FormListener
Dim NewForm
'Act:
FormListenerTest.Setup NewForm
'Assert:
Assert.Succeed
TestExit:
'@Ignore UnhandledOnErrorResumeNext
On Error Resume Next
Exit Sub
TestFail:
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
Resume TestExit
End Sub