Monday, March 8, 2010

VBA - Suppressing CTRL+Break or CTRL+C in VBA Customizations

VBA is by far one of the best customization tools available to Microsoft Dynamics GP developers and as such it is widely used across a number of production environments to deliver functionality that ranges in various degrees of complexity.

However, one of the issues with VBA -- at least until now -- is the fact that an end user can inadvertly press CTRL+Break (older keyboards) or CTRL+C and stop the execution of a script. Now imagine if that script is say, some code developed to calculate 401K contributions when payroll is ran... the results will certainly not be pretty!

With this in mind, we can use a USER32.DLL library funcion to disable user input when critical VBA code is required to be executed in a block. The following shows how to implement such code:

BlockInput function

Private Declare Function BlockInput Lib "user32" (ByVal fBlock As Long) As
Long



We can now use this function across any of our VBA customizations, as follows:

Sample Window_BeforeOpen code

Private Sub Window_BeforeOpen(OpenVisible As Boolean)
BlockInput True
' All the code you need to run here
...
' MAKE SURE TO RE-ENABLE THE INPUT
BlockInput False
End Sub


There are several applications for this code, but whatever you do, don't forget to re-enable the user input, otherwise you will end up with one dead mouse and keyboard!

Until next post!

MG.-
Mariano Gomez, MVP
Maximum Global Business, LLC
http://www.maximumglobalbusiness.com/

3 comments:

Anonymous said...

lovely !

Anonymous said...

Probably should mention, re-enable before a msgbox else you will get stuck lol

Anonymous said...

Another caveat - don't step through your code in debugging mode!