Code to export all modules in a Microsoft Access Database

Code to export all modules in a Microsoft Access Database

The following is code to export all the modules, classes and forms in a Microsoft Access Database to a folder.

'---------------------------------------------------------'
' Copyright © 2019 William Max & Co. All rights reserved. '
'---------------------------------------------------------'

Option Compare Database
Option Explicit

Sub WMC_ExportAllCode(Optional vDest As Variant)

   '--------------------------------------------------'
   ' This code was originally written by Fredrated451 '
   ' and posted on the MSDN forums. Original version  '
   ' saved all code to a single text file. This       '
   ' heavily modified version saves each file         '
   ' separately and allows for specifying a specific  '
   'destination.                                      '
   '--------------------------------------------------'

   Dim sDest As String
   Dim sModule As String
   Dim poModule As Object
   Dim sExt As String
   
   '------------------------------------------------------'
   ' If a destination folder is not passed to subroutine, '
   ' then default to Code directory  in the same folder   '
   ' as the database. The directory *MUST* exists.        '
   '------------------------------------------------------'
   If IsMissing(vDest) Then
      sDest = gfReverseTail(CurrentDb.Name, "\") & "Code"
   Else
      sDest = vDest
   End If
   
   '---------------------------------------'
   ' For each component in the project ... '
   '---------------------------------------'
   For Each poModule In VBE.ActiveVBProject.VBComponents
      
      ' vbext_ct_StdModule     1  Standard module
      ' vbext_ct_ClassModule   2  Class module
      ' vbext_ct_MSForm        3  Microsoft Form
      ' vbext_ct_Document    100  Document Module
      
      Select Case poModule.Type
         Case 1
            sExt = "bas"
         Case 2
            sExt = "cls"
         Case 3
            sExt = "frm"
         Case 100
            sExt = "frm"
            
      End Select
         
      Call poModule.Export(sDest & "\" & poModule.Name & "." & sExt)
      
   Next
  
End Sub


Function gfReverseTail(ByVal sString As String, Optional sDelim)
   
   Dim iPos As Integer
   
   ' check for delimiter, if missing
   ' set delimiter to comma
   If IsMissing(sDelim) Then
      sDelim = ","
   End If
   
   ' remove trailing delimiter
   If Right(sString, Len(sDelim)) = sDelim Then
      sString = Left(sString, Len(sString) - Len(sDelim))
   End If
   
   ' start at end of string and move toward front
   ' until the deliminting character is found
   iPos = Len(sDelim)
   Do Until iPos > Len(sString)
      If Left(Mid$(sString, Len(sString) - iPos + 1), Len(sDelim)) = sDelim Then
         Exit Do
      End If
      iPos = iPos + 1
   Loop
   
   If iPos > Len(sString) Then
      gfReverseTail = ""
   Else
      gfReverseTail = Left$(sString, Len(sString) - iPos + Len(sDelim))
   End If
   
End Function