Using Assertions
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
It is easy to tell when code is broken because an error occurs. It is often harder to tell that your code is using or creating invalid data. Just because a procedure runs without generating an error does not mean that there are no bugs in that procedure.
You use assertions to test for certain conditions in your code. Using an assertion is similar to making a statement about some condition in your code. If the statement is true, nothing happens. If the statement is false, your code enters break mode with the line containing the false statement highlighted. Using assertions to test the validity of expressions or variables in your code is similar to adding custom rules to Microsoft® Visual Basic® for Applications (VBA) itself that will cause the code to stop executing if one of the rules is violated.
The VBA Debug object has an Assert method that you can use to test the truth of a condition or statement in your code. Using the Assert method is similar to setting a watch expression that will break when some statement is true. If you are using the Assert method, the break will occur when the statement asserted is false.
Although the Assert method is a valuable tool for debugging and testing your code, it is not very useful in a distributed application because it forces execution to stop when an assertion is false. Eventually you will remove the Debug.Assert statements and replace them with error handlers. After your code has been debugged and thoroughly tested, you can remove the assertions by searching your project for Debug.Assert and deleting those lines from your final code.
See Also
Writing Error-Free Code | Design-Time Tools | Run-Time Tools | Script Debugging Tools | Understanding Conditional Compilation | Creating Custom Assertions | Handling Errors