In many compiled languages, calls to Debug.Assert
, or their equivalent, are left out of the compiled production code for performance reasons. However, calls to Debug.Assert still appear to execute in the /runtime
versions of MS Access applications.
To test this, I added the following to my startup form:
Private Sub Form_Load()
Debug.Assert UserOK()
End Sub
Function UserOK() As Boolean
UserOK = MsgBox("Is everything OK?", vbYesNo, "Test Debug.Assert") = vbYes
End Function
When I run this in a development environment and click [No] on the MsgBox, execution breaks at the Debug.Assert line (as I would expect).
When I run the same code with the /runtime
switch (using a full version of MS Access 2002) I still see the MsgBox, but clicking on [No] does not halt program execution. It seems VBA executes the line but ignores the result. This is not surprising, but it is unfortunate.
I was hoping that Access would skip the Debug.Assert line completely. This means that one must take care not to use Debug.Assert lines that would hurt performance, for example:
Debug.Assert DCount("*", "SomeHugeTable", "NonIndexedField='prepare to wait!'") = 0
Is this behavior documented somewhere? The official documentation in Access appears to be pulled verbatim from VB6:
Assert invocations work only within the development environment. When the module is compiled into an executable, the method calls on the Debug object are omitted.
Obviously, MS Access apps cannot be compiled into an executable. Is there a better alternative than the following workaround?
Private Sub Form_Load()
If Not SysCmd(acSysCmdRuntime) Then Debug.Assert UserOK() 'check if Runtime
End Sub
Function UserOK() As Boolean
UserOK = MsgBox("Is everything OK?", vbYesNo, "Test Debug.Assert") = vbYes
End Function