Search for possible declarations

Josef P.

Well-known member
Local time
Today, 14:01
Joined
Feb 2, 2023
Messages
1,050
I am currently trying to create an Access add-in that lists all declarations from the VBA code.

Background:
The idea was born from a discussion (msaccess-vcs-add-in: issue 599) about the behavior of the VBA editor, which adapts each existing declaration to the last written capitalization of the same word.
The code should make it possible to minimize the noise when checking into a version control system.

Now I am looking for possible syntaxes of declarations of procedures, variables etc.
If you have any variants that are not included in the following code, please leave a comment.
Many thanks in advance.

Example Code (Class):
Code:
Implements CodeModulGenerator

Dim AccUnitX As Long
Private m_AccUnitInfo As String
Public Field As String

Private Const Const1 As String = "abc"

Private Enum TestEnum: TestEnum_P1 = 2: End Enum
Private Type TestType1
   FldA As Long
   FldB As String
   FldC As Boolean
End Type

Private Enum TestEnum2
   TestEnum2_P1 = 2
   TestEnum2_P2 = 3
End Enum

Private Type TestType2
   Fld2A As Long
   Fld2B As String
   FldC As Date
End Type

Private WithEvents m_TextBox As TextBox

Public Event RaiseSomething(ByVal EventParam1 As Variant)

Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

Private Declare PtrSafe Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long) As Long

Public Event RaiseSomething2(ByVal EventParam1 As Variant, ByVal EventParam2 As Variant)

Private Sub Class_Initialize()
' Dim Class_Initialize_Xyz as String ... ignored!
End Sub

Public Function AccUnitTestFunct(ByVal FuncParam1 As Variant, FuncParam2() As String) As Variant

   Dim FuncVar1 As Variant, FuncVar2
   Dim FuncVar3()
  
   Dim FuncVar4 As Long: FuncVar4 = 5
  
   Dim Dim1 As Long: Dim Dim2 As Long

End Function

' Declaration of a property procedure in one line:
Friend Property Get Name1() As String: Name1 = "TestName": End Property

Friend Property Let Name2(ByVal NewValue As String)
'
End Property

Friend Property Set PropertySet(ByVal ObjRef As Object)
'
End Property

Private Sub TestMe() ' _
Private Sub ThisIsOnlyAComment(

End Sub

Public Sub VariableParams(ParamArray Args() As Variant)
'
End Sub

Private Static Sub MyStaticSub(Optional ByVal Reset As Boolean = False)
    Static Counter2 As Integer
End Sub

Note:
+ Global in a standars module

/edit:
+ WithEvents ... Thanks @KitaYama (#6)
 
Last edited:
If YOU wrote it, you would know whether you had any of these, but for general declarations from arbitrary sources, there is keyword STATIC which applies to a variable that retains its value even after you execute the EXIT SUB or END SUB, and keyword FRIEND which has a complex effect of scope/visibility.
 
If YOU wrote it, you would know whether you had any of these,..
Could you please clarify what you mean? I'm not sure I understand.

For better clarification: I am looking for declarations that I might not be aware of or may have overlooked, so I can consider them in the code.
I am using the code shown as test code to verify whether my 'name search' works correctly.

A few expressions that I check:
Dim, Private, Friend, Public, Global, Static,
Sub, Function, Property Get/Let/Set, Declare
ByVal, ByRef, ParamArray,
Enum, Type

However, it is not only the individual words that are relevant, but also their combination in order to read out the names of the variables, procedures, etc.
And since I always use similar declarations myself, I can imagine that I haven't considered everything yet. That's why I wanted to ask in this thread.

Last version of test codemodule: GitHub: DeclarationDictTestCodemodule.cls
 
Last edited:
If you wrote the code you are analyzing, you would be more likely to remember if you used either of two rare cases - a STATIC variable or a FRIEND variable. Both would be unusual. If it is code you picked up off the internet, there is no telling what you will find.
 
The code in #1 is code without logic. I just wrote down the variants that came to my mind for defining variables, procedures, etc.
I pass this code in a test to the class, which is supposed to read the names of the variables, procedures, etc., from it and verify the result.
 
I can't find WithEvents in your list.

Code:
Private WithEvents m_Textbox As Access.TextBox
Private WithEvents oForm As Form
Private WithEvents oReport As Report
 
Thank you very much! I added it in #1.
I use WithEvents a lot ... and yet I hadn't thought of it. :)

Note:
Implements also exists, but I don't have to pay attention to this, as it is directly linked to the upper/lower case of the class.
... Someone might come up with the idea of naming a variable like a class. This would also change this if the capitalization does not match.
 
Last edited:
Would you need to check for New keyword?
Private SomeObject as New SomeClass
 
Thanks for the tip.
In such a case, the text before “As” is enough for me, as I only need to find “SomeObject” in this example.
 
Someone might come up with the idea of naming a variable like a class.
Here 🙋‍♂️
I do that occasionally. But, IIRC, this would already trigger the capitalization issue if class name and variable name don't use the exact same capitalization. So, it would be tolerable if your code would change this unintentionally.
 

Users who are viewing this thread

Back
Top Bottom