Module: Modules in Category Microsoft Access : Macros and Modules from Total Visual SourceBook

Compiling, opening, retrieving, replacing, saving, and deleting Microsoft Access modules using Access VBA.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modModules module.
CompileAllModules Procedure Compiles all the modules in the project and saves it.
GetModuleLines Procedure Get all the lines of a module.
GetAllProcedureLines Procedure Get the lines of all procedures in a module.
IsModuleClass Procedure Determine if a module is a class or standard module (not used for form or report modules which are always classes).
GetProcedureLines Procedure Get all the lines for a procedure.
GetPropertyLines Procedure Get all the lines for a property.
GoToModuleProc Procedure Open the named module and places the cursor at the beginning of the specified procedure.
ModuleNamesToArray Procedure Loads an array with names of all modules.
ModuleNamesToArrayExternal Procedure Loads an array with names of all modules from an external database.
ModuleNamesToString Procedure Populate a string with names of all modules.
ModuleNamesToStringExternal Procedure Loads an array with names of all modules from an external database.
ProcedureNamesToArray Procedure Retrieves the list of procedure names from a module, form or report, and assigns it to an array.
ReplaceModuleText Procedure Searches and replaces all the specified text in the specified module or procedure.
SaveModuleToFile Procedure Save the module text to a file.
KillFile Procedure Delete the named file, handling errors if the file does not exist.
' Example of modModules
'
' To use this example, create a new module and paste this code into it.
' Then run the procedures by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

Private Sub Example_modModules()
  ' Comments: Example of using the modModules modules to work with Microsoft Access database modules in Access VBA.

  ' Name of a module in the current database
  Const cstrModule As String = "modModules"

  ' Name of a procedure in the module
  Const cstrProcedure As String = "IsModuleClass"

  Dim astrNames() As String
  Dim aintKind() As vbext_ProcKind
  Dim astrCode() As String
  Dim strNames As String
  Dim intProcs As Integer
  Dim intCount As Integer
  Dim intCounter As Integer

  ' Example of CompileAllModules
  Debug.Print "Compiling all modules..."
  If CompileAllModules Then
    Debug.Print "Modules compiled."
  Else
    Debug.Print "Modules could not be compiled."
  End If

  ' Example of GetAllProcedureLines
  intProcs = GetAllProcedureLines(acModule, cstrModule, astrNames, aintKind, astrCode)
  Debug.Print intProcs & " procedures: " & vbCrLf & "-----------------------------------"
  For intCounter = 0 To intProcs - 1
    Debug.Print astrNames(intCounter) & " (type " & aintKind(intCounter) & ") : " & _
                Left$(astrCode(intCounter), 25) & "..."
  Next intCounter

  ' Example of GetModuleLines
  Debug.Print "Module code: " & vbCrLf & "-----------------------------------"
  Debug.Print GetModuleLines(acModule, cstrModule)

  ' Example of GetProcedureLines
  Debug.Print "Proc code: " & vbCrLf & "-----------------------------------"
  Debug.Print GetProcedureLines(acModule, cstrModule, cstrProcedure)

  ' Example of GotoModuleProc
  Debug.Print "Opening " & cstrModule & "." & cstrProcedure
  GoToModuleProc acModule, cstrModule, cstrProcedure

  ' Example of IsModuleClass
  If IsModuleClass(cstrModule) Then
    Debug.Print cstrModule & " is a class."
  Else
    Debug.Print cstrModule & " is NOT a class."
  End If

  ' Example of ModuleNamesToArray
  intCount = ModuleNamesToArray(astrNames)
  Debug.Print "First 3 module names in this database:"
  For intCounter = 1 To 3
    Debug.Print intCounter & ": " & astrNames(intCounter - 1)
  Next intCounter

  ' Example of ModuleNamesToString
  intCount = ModuleNamesToString(strNames, ";")
  Debug.Print intCount & " modules in this database: " & strNames

End Sub

Private Sub TestExternalDatabase()
  ' Comments: Example of using the modModules modules to work with Microsoft Access database modules in another database

  Const cstrSample As String = "C:\Total Visual SourceBook 2013\Samples\sample.mdb"

  Dim intCount As Integer
  Dim astrNames() As String
  Dim strModule As String
  Dim strNames As String
  Dim intCounter As Integer
  Dim strProcedure As String

  If cstrSample <> CurrentProject.FullName Then
    ' Example of ModuleNamesToArrayExternal
    intCount = ModuleNamesToArrayExternal(cstrSample, astrNames)
    ' Save first module name
    strModule = astrNames(0)
    Debug.Print "First module in " & cstrSample & ": " & strModule

    If intCount > 0 Then
      ' Example of ModuleNamesToStringExternal
      intCount = ModuleNamesToStringExternal(cstrSample, strNames, ";")
      Debug.Print intCount & " modules in " & cstrSample & ": " & strNames

      ' Example of ProcedureNamesToArray
      intCount = ProcedureNamesToArray(acModule, strModule, astrNames)
      Debug.Print "Procedure Names:"
      For intCounter = 1 To intCount
        Debug.Print intCounter & ": " & astrNames(intCounter - 1)
      Next intCounter

      ' Get last procedure name
      strProcedure = astrNames(intCount - 1)

      ' Example of ReplaceModuleText
      If ReplaceModuleText(acModule, strModule, strProcedure, "True", "TRUE", True) Then
        Debug.Print "Text Changed."
      Else
        Debug.Print "Text could not be changed."
      End If
    Else
      MsgBox "No modules found"
    End If
  Else
    MsgBox "Cannot perform these module tasks on the same database. Set cstrSample to an external database."
  End If

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS