Anyone know how to eliminate dead code without having to search line-by-line?
Anyone know how to eliminate dead code without having to search line-by-line?
How would you know? Even if you searched line by line? Wouldn't you have to operate the program to know that?
What do you mean by "dead code"?
*Procedures that are in the VBE that are not linked to object events?
*Actual code that's never used throughout?
If you're talking about the first option, yes you could use the Extensibility library to search that and delete the code out that's not linked to objects.
Dead code meaning code tied to cmdMyButton even though MyButton has been deleted.
I am new to VBA, so I am not sure what the 'Extensibility library' is or where to find it. Can you explain?
Thank you for your response!
if you're new to the language, you probably won't understand the ext. library at all. just being honest there.
as far as code tied to controls and objects, all code is deleted automatically when the object or control is deleted, so you don't have to worrry about that type of code. The only exception to this (I think) is when you write code behind an EVENT and then only delete the "event procedure" words in the property sheet. It might be the other way around though, not sure.
however, if you have code in a standard module such as:and that button has been deleted from the form, I would think it would be much trickier.Code:forms("form").controls("cmdButton").enabled=false
The basic qualifying characters (the characters that separate qualifiers and their collections) is the period. the only way I know how to do this is to use the extensibility objects in vba to create a dynamic array and populate it with every code module object in the vba project. That's relatively easy (10 lines of code). The tough part would be parsing all of your code out based on the "." delimiter. You can read the lines one by one in every code module, but some code syntax that can relate to form controls (for example) can use the "!" qualifier instead of the ".":You would have to look at those as well. The other things to consider is locating the code that's relevant. It's MUCH easier if you have named entities in your database using conventional naming techniques (tblCustomers, frmCustomers, cmdButton). If you haven't, doing this would probably be impossible.Code:forms!formname!control
Yes this is doable (and it would be a great way to clean up!), but to be honest with you, it might be a better idea to leave it be. I'm personally working with a FE file right now that could be 1MB smaller if I deleted all the code out that wasn't used!
another thing you might want to consider doing if you're concerned about broken references is to use the compiler. It will stop on all broken references in actual object code modules (the code behind forms and reports), but it won't in standard modules. You can use this method and the find/replace dialog in the VBE to quickly fix broken references, but I think you still might be stuck with the ones in modules and class modules.
If you're looking to delete entire Functions that are no longer bound to an existing Form or Report Object, then you can do that fairly easily. The process does require that you manually check every Form and Report in question though, so if you have experience with VBA, I would recommend you try implementing ajetrumpet's suggestion first.
- Open the Form or Report in question and switch to Design Mode.
- Open the Microsoft Visual Basic Editor by pressing [ALT]+[F11].
- Make sure you are browsing the "(General)" Object.
- Any Function that is listed in the Procedures List is a function that is not bound to a control. If any of these items are Events, they can most likely be be safely deleted.
See the attached image for the location of the Object and Procedure Lists in the Visual Basic Editor.
@ajetrumpet: Thank you for the information. I have a mixture of dead code in my database. I am revamping from an older one of mine that I created before I knew what I was doing. Lots of issues with it.
@Rawb: I used your method to get rid of a few lines. I was able to identify old code relatively quickly this way.
Thank you both very much for your help!
Unfortunately, I doubt there is a tool out there to do what I described. There probably isn't a market for it. In this country, if there's a market for anything the product will be there (whether it's good or not) within the blink of an eye.@ajetrumpet: Thank you for the information. I have a mixture of dead code in my database. I am revamping from an older one of mine that I created before I knew what I was doing. Lots of issues with it.
@Rawb: I used your method to get rid of a few lines. I was able to identify old code relatively quickly this way.
Thank you both very much for your help!
Good luck with this. At least you've got some knowledge that you can work with now!