Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

So we have created our first test and it is failing.

Opening up the RubberDuckVBA Test Explorer and running all the tests shows us our failing test:

Now it’s time to make it pass. To do this I’m going to Create a new Class Module using the Insert -> Class Module menu in Access VBE. I will then rename the class module from Class1 to FormListener, Save the database, and resync RubberDuck.

In this case it is still telling me my test is failing. Here is what I am trying to do in code:

    'Arrange:
    Dim FormListenerTest As Object
    
    'Act:
    Set FormListenerTest = CreateObject("Database.FormListener")
    
    'Assert:
    Assert.Succeed

So the “Database.FormListener” object is not being created. I looked up an article on StackOverflow which is pointing to the fact that I can’t use CreateObject for this. I can only use CreateObject for a registered COM object from a DLL.
Here is the link to the article.

I guess one option here is to simply use the normal method of defining the instance and creating a new object like this:

    'Arrange:
    Dim FormListenerTest As FormListener
    
    'Act:
    Set FormListenerTest = New FormListener
    
    'Assert:
    Assert.Succeed

Before we create the FormListener class module, this results in a compiler error, which is a clear case of failing code, although I was hoping to be a little more elegant. So, is there a way to try to create an object from an Access Class Module using the text of the Class Module name which will fail if it doesn’t exist, but still compile so I can see a nicely failing test inside of the RubberDuck Test Explorer?

Frankly, just spent about 10 minutes researching, but it is not immediately obvious to me yet whether this is possible. So I am going to use method 2. Initially if I do a Debug->Compile without creating the class, the code will not compile and RubberDuck will not be able to run any tests at all in this case. However, once I’ve created the class, I can now compile and RubberDuck will Run and pass our test.