Metamorphing Machine I rather be this walking metamorphosis
than having that old formed opinion about everything!

Got a VB6 legacy project. Now what? Part 1

Being assigned a legacy VB6 project without knowing the language is a difficult task. In this series (I plan to have at least one more installment about it), I will share what is my approach in dealing with an unfamiliar VB6 codebase.

The first thing I do is open a file - be it a form, a module, or a class module - in Visual Studio and scan it to check if it has an Option Explicit statement.
If it has not, then I'd write it as its very first line.
Then, I hit Ctrl+F5 and see what happens. You should do the same. If you get an error message saying "Compile error: Variable not defined", go to the procedure where the unknown variable is being highlighted and declare it.
Variable declaration usually takes place right bellow the Sub, Function, or Property declaration, like this:

Public Property Let Value(V As Variant)
Dim VT As Integer
'etc.

You see, by default, VB6 does not force you to declare your variables. It is a good thing when you are in that exploratory phase building your software. But once that phase is over, it is better to have mandatory variable declaration. It avoids a common type of mistake: Automatic creation of variables with typos.
Suppose you have a declared variable DefinitelyGood, but later on, you misspell it as DefinatelyGood. Without Option Explicit, VB6 will not complain about it, and it may remain unnoticed to you.

If you find a misspelled variable, you should use your best judgment about fixing the typo or declaring the misspelled variable. If the procedure has a logical error due to the misspelled variable, it is a no-brain fix. But if it is working, then you should consider declaring the misspelled variable, and maybe even changing its name to something different to avoid confusion.

When declaring a variable, if you can quickly figure out its data type, then declare it with that specific data type. But if you are unsure, you should declare it as Variant - it is the safer bet in this situation. Once you understand a little better the code base, you should be able to return there and narrow down that variable data type.

Caveats: But I digress. When you're done with one file, move to the next.

Once all files have been checked, you should go to the menu option ToolsOptionsEditor tab, Code Settings section, and check the Require Variable Declaration setting if it is not already checked.
It will ensure every new file will have an Option Explicit statement by default.

Then you can revisit every procedure - or the most important ones - and, if there are not many variable declarations there, comment all of them and Ctrl+F5 the project. Then uncomment every needed variable and Ctrl+F5 it again, until there are no more compile errors. Doing that you will be able to spot declared variables that are not being used. Deleting them decreases the mental burden of tracking many variables for that particular procedure.

Also, when trying to wrap your head around some procedures, you will be able to spot global variables. They are the ones being used but not declared in that procedure.

VB6 legacy projects usually have modules declaring several global variables. Commenting them, testing with Ctrl+F5, and uncommenting them as needed as mentioned above will help you get rid of some of them too. Sometimes you can even identify some variables that should not be global at all. They are there in the module but are being used only in one another file. In these cases, it is better to move them to the declaration section 2 of the file where it is being used. Decreasing the number of global variables is always a good thing.

Next in my chest of tricks is going after variable declarations in the declaration section that does not have Private or Public keywords; the ones using only Dim.
I change Dim to Private and test it with Ctrl+F5. If there's a compile error, then I change it to Public.
If the variable there is not being used by other parts of the project, it has no business being declared implicitly Public with Dim. Narrowing variables' scopes is the right thing to do.

If you follow these guidelines, you're up to a good start.

Andrej Biasic
2020-02-26
Update:
Second part is available here.

Andrej Biasic
2020-04-15
1 It depends whether the file has a Deftype or not. It's unusual to find one being used, but it is not impossible, either.
2 The region before any procedure declaration is called the declaration section. It is there that Option Explicit, Option Base, and Option Compare have place, as well as global or file scoped variables are declared.