709-218-7927 The Landfall Garden House 60 Canon Bayley Road Bonavista, Newfoundland CANADA A0C 1B0 |
---|
Self-Testing Procedures
Well almost. I use several methods but here’s my most common:
I want to write a small function to return me yesterday’s date as a formatted string. A useful function, because I’m often to tired to write the report at night time, so I write it the next day, but by then I want yesterday’s date on the report.
The function itself is straightforward enough:
Public Function strYesterDate() As String strYesterDate = Format(Now() - 1, "YYYY/MM/DD") End Function
Stop mumbling! The “Function strYesterDate() As String” part tells us that the procedure will return a string as the value of its name.
The body of the code reads “Format a date value as a YYYY/MM/DD format. What date value? Now (or today) less one, that is, yesterday.
I think it will work, but before I invest any time in manipulating the code or developing application code based on the new procedure, I draft a short test routine.
Mechanically I double-click on the procedure name to select it, Ctrl-C to copy the selected name to the clipboard, Ctrl-End to go past the end of the procedure, then I type "Sub TEST" and Ctrl-V to paste in the name of the procedure to be tested.
In this case (and most cases returning a string or numeric value) it will be enough for me to see the result in a pop-up box, so in the empty space provided for code I type “MsgBox” and a space and Ctrl-V to paste in the procedure name.
Not a lot of typing, really, is it?
Since the cursor is already in the code of my TEST macro, I tap the F5 function key to execute it and observe the formatted date in the pop-up box. Swell.
I don’t want to clutter up my user interface with a zillion TEST macros, so I comment out the TEST routine:
Public Function strYesterDate() As String strYesterDate = Format(Now() - 1, "YYYY/MM/DD") End Function 'Sub TESTstrYesterDate() ' MsgBox strYesterDate 'End Sub
Then I drag it inside the procedure:
Public Function strYesterDate() As String strYesterDate = Format(Now() - 1, "YYYY/MM/DD") 'Sub TESTstrYesterDate() ' MsgBox strYesterDate 'End Sub End Function
As an added advantage, the TEST code will travel wherever the procedure goes; if I make a change to the procedure, I can drag the TEST code out, de-comment it, and check that the procedure still behaves as it should after modifications.
Over the next few sessions we’re going to have you create a utility library, so that the excellent procedures you write will be available to you for ever, and will accelerate your development of applications.
Once you’ve grasped the idea behind a utility library, I’m going to offer you some free samples of useful code.
709-218-7927 CPRGreaves@gmail.com Bonavista, Sunday, December 08, 2024 9:31 AM Copyright © 1990-2024 Chris Greaves. All Rights Reserved. |
---|