Is there a way of deleting all code lines beginning with debug.print across all modules using VBA? (2 Viewers)

peskywinnets

Registered User.
Local time
Today, 06:52
Joined
Feb 4, 2014
Messages
582
Subject says it all really.

My coding sees a multitude of modules which are peppered with debug.print lines that are not required anymore, it would take me forever to go through each module manually...is there a slick way of deleting any/all lines starting with debug.print?
 
There are some tools that can do this, such as MZ-Tools.
In lieu of that, you can do a find and replace of "Debug.Print", and change it to "'Debug.Print" (note the single quote to introduce a comment)
 
is there a slick way of deleting any/all lines starting with debug.print?
Clean up after yourself is the professional solution. Once the code has been tested and works, delete the code or comment it so it doesn't run.
 
move.png
 
I tested. Works as long as Debug.Print is not multi-line (not using line continuation). Apparently, it matches until a CrLf is encountered. If your Debug's are all one-liner, then should be good.
 
I tested. Works as long as Debug.Print is not multi-line (not using line continuation). Apparently, it matches until a CrLf is encountered. If your Debug's are all one-liner, then should be good
Even so, after that all those other second lines should not compile. So it would be pretty easy to hit compile and find all bad lines.
 
Quick side note:
Compiler constants (Conditional compilation arguments) can also be helpful, allowing you to enable debug output when needed.

Example:
Set either project-wide or in the specific code module:
VB Project:
CompArg.png


Only in specific code module:
#Const DEVMODE = 1

Code:
#If DEVMODE Then
    Debug.Print "TryBuildSplitToArrayCriteria", FieldName, FieldDataType, RelationalOperator, FilterValue, IgnoreValue, Criteria
#End If
   If TryBuildSplitToArrayCriteria(FieldName, FieldDataType, RelationalOperator, FilterValue, IgnoreValue, Criteria) Then
      BuildCriteria = Criteria
      Exit Function
   End If

Instead of directly calling Debug.Print, it can also be useful to have your own function/class if you want to output to a file or WinDebug instead of Debug.Print.
 
Last edited:
5046 times debug.Print
I think that's a new world record. :cool:
It was actually 6500+ ... if you include also deleting debug.print without an ' at the front! (all those debug.prints amounts to 8yrs of my coding - I'm not a programmer, but have somehow kludged together something that I run my ecommerce business on...nice to be rid of them, I'm pretty sure they were actually slowing up things as random/unwanted debug.prints spewed out stuff when different parts of my code was called)
 
Last edited:
If you use regular expressions you can easily cater for multilingual instances.
 
Thank you for the kind offer but I have for more important things to do like sweeping my belly button.
 
As far as I know, regular expression can not be used in Find & Replace in vba.
I may be wrong though.

But this code can go through the whole project and delete Debug.Prints.
If you have multi line Debug.Prints, those will be deleted too.

I know nobody will use it, but if you do, Don't forget to back up your project before running it.

SQL:
Sub Remove_DebugPrints()
   
    Dim ao As AccessObject
    Dim mdl As Module
    Dim i As Long
    Dim lineText As String
    Dim totalLines As Long
    Dim deleteStart As Long
    Dim deleteCount As Long

    For Each ao In CurrentProject.AllModules
        DoCmd.OpenModule ao.Name
        Set mdl = Modules(ao.Name)
        totalLines = mdl.CountOfLines
        i = 1

        Do While i <= totalLines
            lineText = Trim(mdl.Lines(i, 1))
           
            If LCase(Left(lineText, 11)) = "debug.print" Then
                deleteStart = i
                deleteCount = 1
               
                Do While Right(Trim(mdl.Lines(i, 1)), 1) = "_" And (i + 1 <= totalLines)
                    i = i + 1
                    deleteCount = deleteCount + 1
                Loop
               
                mdl.DeleteLines deleteStart, deleteCount
                totalLines = mdl.CountOfLines
                i = deleteStart
            Else
                i = i + 1
            End If
        Loop
    Next ao
End Sub
 
As far as I know, regular expression can not be used in Find & Replace in vba.
I may be wrong though.
I'm sure it could be used in code similar to yours though, but without having to loop all the module lines.

However, to me, the REGEXP expression to be able to find blocks of code ignoring line continuation character as the end of what is being searched for is more complex than I can dream up without a lot of experimentation/testing (I'm not fluent in REGEXP!)

Since @DickyP described it as 'eas[il]y' I had hoped he could provide the answer without my having to waste hours mucking around with it.
 

Users who are viewing this thread

Back
Top Bottom