mwolfe02
mwolfe02

Reputation: 24237

Debug.Assert behavior in MS Access runtime

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

Upvotes: 4

Views: 2091

Answers (1)

RubberDuck
RubberDuck

Reputation: 12788

I don't know if it's any better for your particular use case, but there is an alternative that is better, if you're looking to do this in application agnostic VBA code. VBA has Conditional Compilation. Conditional Complication constants can be declared at the module level, but in this case, it will be better to declare it at the project level.

On the menu bar, click Tools>>Project Properties and type a DEBUGMODE = 1 into the "Conditional Compilation Arguments:" field. (Note: DEBUG won't work as it's a keyword.)

Project Properties Dialog Window

Now you can wrap all of your Debug.Assert() statements in code like this.

#If DEBUGMODE Then
    Debug.Assert False
#End If

When you're ready to deploy your project, just go back into the Project Properties dialog and change the argument to DEBUGMODE = 0.

Additional Information:

Upvotes: 4

Related Questions