Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

Here is a starter code kata… This is practically nothing in VBA since types get auto-converted, but how might you do this taking a variable of type integer and write a function to convert it to a string type. Here is the code kata requirements along with tests we can use to make sure it does what the kata is expecting at this URL: https://www.codewars.com/kata/5265326f5fda8eb1160004c8

Here is the description of the kata from that URL:


We need a function that can transform a number (integer) into a string.

What ways of achieving this do you know?

Examples (input –> output):

123  --> "123"
999  --> "999"
-100 --> "-100"

Ok, given this basic kata how would you code a function to convert an integer into a string?

So I started with a blank Access database and using the RubberDuckVBA addin (Download available here: https://rubberduckvba.com/) I created a new test module. I added the RubberDuck VBA reference in Tools -> References and added the Compiler Constant in Tools -> ConvertIntegerToString Properties: LateBindTests = 0

And here is my test module with the updates:

'@TestModule
'@Folder("Tests")

Option Compare Database 

Option Explicit 
Option Private Module 

#Const LateBind = LateBindTests 

#If LateBind Then 
    Private Assert As Object 
    Private Fakes As Object 
#Else 
    Private Assert As Rubberduck.AssertClass 
    Private Fakes As Rubberduck.FakesProvider 
#End If 

'@ModuleInitialize
Private Sub ModuleInitialize() 
    'this method runs once per module.
    #If LateBind Then 
        Set Assert = CreateObject( "Rubberduck.AssertClass") 
        Set Fakes = CreateObject( "Rubberduck.FakesProvider") 
    #Else 
        Set Assert = New Rubberduck.AssertClass 
        Set Fakes = New Rubberduck.FakesProvider 
    #End If 
End Sub 

'@ModuleCleanup
Private Sub ModuleCleanup() 
    'this method runs once per module.
    Set Assert = Nothing 
    Set Fakes = Nothing 
End Sub 

'@TestInitialize
Private Sub TestInitialize() 
    'This method runs before every test in the module..
End Sub 

'@TestCleanup
Private Sub TestCleanup() 
    'this method runs after every test in the module.
End Sub 

'@TestMethod("IntegerToString")
Private Sub Test123() 
    On Error GoTo TestFail 
    
    'Arrange:
    Dim paramIn As Integer 
    Dim funcOut As String 
    paramIn = 123 
    
    'Act:
    funcOut = IntegerToString(paramIn) 
    
    'Assert:
    Assert.AreEqual  "123",funcOut 

TestExit: 
    '@Ignore UnhandledOnErrorResumeNext
    On Error Resume Next 
    
    Exit Sub 
TestFail: 
    Assert.Fail  "Test raised an error: #" & Err.Number &  " - " & Err.Description 
    Resume TestExit 
End Sub 

'@TestMethod("IntegerToString")
Private Sub Test999() 
    On Error GoTo TestFail 
    
    'Arrange:
    Dim paramIn As Integer 
    Dim funcOut As String 
    paramIn = 999 
    
    'Act:
    funcOut = IntegerToString(paramIn) 
    
    'Assert:
    Assert.AreEqual  "999",funcOut 

TestExit: 
    '@Ignore UnhandledOnErrorResumeNext
    On Error Resume Next 
    
    Exit Sub 
TestFail: 
    Assert.Fail  "Test raised an error: #" & Err.Number &  " - " & Err.Description 
    Resume TestExit 
End Sub 

'@TestMethod("IntegerToString")
Private Sub TestNegative100() 
    On Error GoTo TestFail 
    
    'Arrange:
    Dim paramIn As Integer 
    Dim funcOut As String 
    paramIn = -100 
    
    'Act:
    funcOut = IntegerToString(paramIn) 
    
    'Assert:
    Assert.AreEqual  "-100",funcOut 

TestExit: 
    '@Ignore UnhandledOnErrorResumeNext
    On Error Resume Next 
    
    Exit Sub 
TestFail: 
    Assert.Fail  "Test raised an error: #" & Err.Number &  " - " & Err.Description 
    Resume TestExit 
End Sub 

The module is just boiler plate up to the tests themselves where I created three tests to test for the three integer to string conversion conditions of the kata. These tests start at the first line: ‘@TestMethod(“IntegerToString”)

These tests need a public function called IntegerToString to compile so here is a new module I created called KataModule with that function:

Option Compare Database 
Option Explicit 

Public Function IntegerToString(intVal As Integer) As String 
    IntegerToString = intVal 
End Function 

And this function passes all the tests. Kata complete!

Here is my database via zip file you can download. Remember, I used RubberDuckVBA to write the tests. Follow my instructions above after installing the add-in by going to the VBE environment (Alt-F11 or Ctrl-G to go to the Immediate Window) and using the Tools menu there.

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.