I had never heard of this keyword. I was perusing the Microsoft Learn web site and happened across this in the VBA reference here: Deftype statements (VBA) | Microsoft Learn
Here is the basic definition from that page:
Used at the module level to set the default data type for variables, arguments passed to procedures, and the return type for Function and Property Get procedures whose names start with the specified characters.
Before continuing further, just wanted to give a shout out to Eric Blomquist who replied to my NiceDLookup article with an updated version of his own. Check it out as bonus content at the end of the article: DLookup and “NiceDLookup” | Access JumpStart
Back to our regular programming. Basically, you can drop declarations for certain variables and they may be automatically set to a default data type based on the first letter of the variable name. Ok, let’s do an example:
Pre Deftype sample code (representing a module)
Output of PrintTypes in Immediate Window:
Now we can remove the type declarations and use DefStr S and DefBool B at the top:
Notice that I don’t need to declare the type of sVarName either, but it will still require a string because it starts with an “s”. Otherwise, if there is no deftype active, the default type is a Variant. Entering PrintTypes again in the immediate window will return the same output as before. Note that I have not assigned anything to any of the variables.
Output of PrintTypes in Immediate Window:
If I change DefBool B to DefInt B, that will change the variables starting with “b” to an Integer.
Ultimately, this makes prefixing variables consistently with a single character based on it’s type more than just a nice naming convention, but actually used in the code itself. Something to think about…