Cat On A Spreadsheet
In the previous post, we connected Excel VBA to external databases like SQL Server and Access, ran queries, and pulled results straight into worksheets.
That’s a great foundation — but the version we built was wide open. The connection string had credentials in plain text, and the SQL command was built by string concatenation (which is both unsafe and error-prone).
In this follow-up, we’ll improve security and reliability by:
Removing hard-coded passwords from your VBA code
Loading credentials from a hidden config sheet or external file
Using basic encryption to obscure sensitive data
Switching to parameterized SQL queries
Integrating error logging and email notifications
Instead of hardcoding credentials, we’ll store them on a hidden worksheet named “Config”:
| Field | Example Value |
|---|---|
| DBServer | SQLSERVER01 |
| Database | SalesDB |
| UserID | reportuser |
| Password | MyEncryptedPassword123 |
Then we’ll read these values through a helper function. To add a small layer of protection, we’ll use simple reversible encryption (not perfect, but better than plaintext).
Encrypt your password once manually using the same XOR logic and paste the encrypted string in the Config sheet. Never store plain text credentials.
Now we can use our settings to open a database connection safely.
Instead of building SQL like this:
We’ll use ADODB.Command and parameter objects — much safer and faster:
We’ll use a shared error handler that logs to a time-stamped file and emails the admin (for this example, DBAdmin@company.com).
You now have a secure, professional, and maintainable database integration in VBA. It:
Reads connection info from a protected source
Obscures passwords
Uses parameterized SQL for safety
Centralizes logging and alerting
This setup scales well across teams and avoids exposing sensitive info in your VBA codebase — a key step toward enterprise-grade Excel automation.
Cat On A Spreadsheet