Today’s adventure was helping a customer who had moved from an on premise Exchange server at their business to Microsoft 365 email accounts in the cloud. They needed to update their Access application with the new email settings.
As I dove into the code, I found 6 different places that the email code had been copied and pasted and would need to be updated. Since each area of code was almost identical, I created a new module with a public subroutine to do the actual email code, rather than just simply change all the settings 6 times.
After starting to implement the subroutine, I found that some of the instances created Text Only bodies, and some used HTML bodies. So at this point I did end up making the original function SendEmail a private sub, then created 2 wrappers for it, one called “SendTextEmail” and one called “SendHTMLEmail” which took the same arguments, but passed them slightly differently to the original function which handled either scenario depending on whether an HTMLBody parameter or TextBody parameter were passed to it.
In this way, I applied the DRY (Do not Repeat Yourself) principle so that in the future, any global changes to email handling will be much easier and only require configuration changes in one function. I always try to think about how to make the code better any time I need to touch it. This is how code gets better rather than rotting!