6

I would like to be able to toggle on/off the option to enter break mode whenever my VBA code is called. The only way I know to do this is set breakpoints at all the "points of entry" into code or have literally every one of these methods call a separate function for purposes of debug.

The "points of entry" may be button clicks or worksheet events and there are a fair number of them.

For example, I could do:

Private Sub bt1Click()
    callThisOnEveryMethod
    'other code
End Sub
Private Sub bt2Click()
    callThisOnEveryMethod
    'other code
End Sub
'etc, repeat 100 times
Private Sub callThisOnEveryMethod()
    'set breakpoint on this method
End Sub

This is not really ideal since I am depending on me adding that to each method and every subsequent method. I don't really trust myself to get 100% of them this way and it's a lot of clutter for debug purposes only. I can add other code here too and even wrap it in an if MY_GLOBAL_DEBUG_BOOLEAN then type statement but I still need to add this code (or the calling method) to every method I write which could start VBA execution.

Imagine I might have 100 methods which could be the start of VBA code executing.

It is not ideal to setup and remove break points on EVERY method each time I want to do this, either, because of the number to turn on/off.

What I would like is to tell VBA somehow "whenever you start executing code, immediately break and go into debug mode regardless of breakpoints, assertions, and without requiring each method to have lots of extra code."

Is this possible?

Community
  • 1
  • 1
enderland
  • 13,825
  • 17
  • 98
  • 152
  • As @Jean-François_Corbett says why would you want to do this? This might be an XY Problem (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), meaning that there is a better way to solve your problem(s) than your proposed solution. – Caltor Aug 04 '15 at 14:24

3 Answers3

14

There are two options to break code and go into debug mode:

a) with simple Stop instruction:

Sub MyProcedure()
   '...any code here
   Stop     'execution will stop here, debugging will start here
   '...the rest of the code
End sub

b) with Debug.Assert False in this way:

Sub MyProcedure()
   '...any code here
   Debug.Assert False     'execution will stop here, debugging will start here
   '...the rest of the code
End sub

However, you could use any condition working with Debug.Assert Condition- each time condition will return False the code will stop. One sample:

Dim A
A=10
Debug.Assert A<>10   'stopping execution here... entering debugging mode
Andrew Keeton
  • 22,195
  • 6
  • 45
  • 72
Kazimierz Jawor
  • 18,861
  • 7
  • 35
  • 55
  • 1
    This isn't really what I'm looking for. I can get this exact functionality much easier simply by inserting an intermediate method and adding lots of `Stop` around my code is even worse than this in terms of maintenance or eventual deployment... – enderland Nov 05 '13 at 20:27
  • @enderland- So, use Debug.Assert then to switch between stop (`Debug.Assert False`) or go (`Debug.Assert True`) where `False and True` could be passed as parameters. Or...elaborate more precisely on what you need giving more accurate example. – Kazimierz Jawor Nov 05 '13 at 20:32
  • I clarified the question. – enderland Nov 05 '13 at 20:37
  • No way to do so simply. Therefore `Debug.Assert Condition=False` seems to be best option. – Kazimierz Jawor Nov 05 '13 at 21:57
3

I'm afraid the answer is no, there is no way to do this without inserting code or breakpoints in each point-of-entry procedure.

Why isn't there a way to do this? Well, the real question is, why should there be a way to do this? What's the point? Perpetual debug mode makes no sense. The idea of debug mode is to enter it if and only if you need to debug a specific procedure or error. I'm not sure what you're trying to achieve exactly?

Anyhow, the way you suggest, with callThisOnEveryMethod, is probably the closest you can get. Or actually, no need to define a new procedure: just use

Debug.Assert MY_GLOBAL_DEBUG_BOOLEAN

at the top of your point-of-entry procedures, where MY_GLOBAL_DEBUG_BOOLEAN is a public module-level constant that must be set to False in order to enter debug mode.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
0

I am facing this situation right now for I need to square my code semi dynamically to some changes in the functional layout of my data (what tables with what names with what data in it, also matching some table contents to structural data on the server), so I need to be easily able to execute some extra code when expecting some things to need adaptation. It would also be very handy to be able to detect if the code is running normally or in debugging mode, a kind of dynamic debugging mode.

I found a way to force entering "Debug mode" with a made up failure, but right after clicking debug and moving around in the code, the mode is actually back to "running" instead of "Break". so I ended up using a global "Pseudobreakmode" variable for the whole project - divided into global and local, setting up the global one from the start by using a "fake" procedure call that I only use for debugging mode, using assert points to be sure to stop at the right positions while running in test mode (F5 / F8) what allows me to enable / disable these blocks whenever needed.

Public Type tDebuggingMode
    Global As Boolean
    Local As Boolean
End Type

Public oDebuggingmode As tDebuggingMode

Private Sub DebugStart()
    oDebuggingmode.Global = msoTrue
    Call StartNormal
End Sub

Sub StartNormal()
    '.....' developments

End Sub

Sub SubprocedurewhateverThatneedCheckingafter()
    '..... whatever needs to be done

    If Not oDebugging.Global Then Exit Sub
'------------------ end of the normal code
    Debug.Assert False ' stopping here allows to switch dynamically the local debugging on or off below (in prevision of many repetitions until the global project works smooth)
    oDebugging.Local = True
    If Not oDebugging.Local Then Exit Sub
    '............ specific code for debugging
    oDebugging.Local = False
End Sub
General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • You tell a story of what you needed, did and experienced. I cannot clearly tell whether you are actually answering the question. Please [edit] to make that more obvious, ideally according to [answer]. Also, taking the [tour] is recommended. – Yunnosch Sep 07 '22 at 07:02