Hold_it

Access DB Pipeline Export

Dec 10th, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 6.37 KB | Software | 0 0
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. '==============================================
  5. ' CONFIG
  6. '==============================================
  7. Private Const EXPORT_ROOT As String = "C:\VBAExport\"
  8.  
  9.  
  10. '==============================================
  11. ' ENTRY POINT
  12. '==============================================
  13. Public Sub ExportDatabaseProject()
  14.     CreateFolders
  15.  
  16.     ExportModules
  17.     ExportTableDefs
  18.     ExportQueryDefs
  19.     ExportRelationships
  20.     ExportReferences
  21.     ExportMacros
  22.     ExportForms
  23.     ExportReports
  24.  
  25.     MsgBox "Export Complete!", vbInformation
  26. End Sub
  27.  
  28.  
  29. '==============================================
  30. ' FOLDER CREATION
  31. '==============================================
  32. Private Sub CreateFolders()
  33.     Dim subFolders As Variant
  34.     Dim f As Variant
  35.  
  36.     subFolders = Array( _
  37.         "", _
  38.         "Modules\", "Tables\", "Queries\", _
  39.         "Relationships\", "References\", "Macros\", _
  40.         "Forms\", "Reports\")
  41.  
  42.     For Each f In subFolders
  43.         CreateFolder EXPORT_ROOT & f
  44.     Next f
  45. End Sub
  46.  
  47. Private Sub CreateFolder(path As String)
  48.     If Dir(path, vbDirectory) = "" Then
  49.         MkDir path
  50.     End If
  51. End Sub
  52.  
  53.  
  54. '==============================================
  55. ' EXPORT MODULES (BAS / CLASS / FORM / REPORT)
  56. '==============================================
  57. Private Sub ExportModules()
  58.     Dim comp As VBIDE.VBComponent
  59.     Dim proj As VBIDE.VBProject
  60.  
  61.     Set proj = Application.VBE.ActiveVBProject
  62.  
  63.     For Each comp In proj.VBComponents
  64.         Dim ext As String
  65.         Select Case comp.Type
  66.             Case vbext_ct_ClassModule: ext = ".cls"
  67.             Case vbext_ct_MSForm: ext = ".frm"
  68.             Case vbext_ct_StdModule: ext = ".bas"
  69.             Case vbext_ct_Document: ext = ".cls"
  70.         End Select
  71.  
  72.         comp.Export EXPORT_ROOT & "Modules\" & comp.Name & ext
  73.     Next comp
  74. End Sub
  75.  
  76.  
  77. '==============================================
  78. ' EXPORT TABLE DEFINITIONS
  79. '==============================================
  80. Private Sub ExportTableDefs()
  81.     Dim td As DAO.TableDef
  82.     Dim fld As DAO.Field
  83.     Dim idx As DAO.Index
  84.     Dim f As Integer
  85.     Dim out As String
  86.  
  87.     For Each td In CurrentDb.TableDefs
  88.         If Left(td.Name, 4) = "MSys" Then GoTo SkipTable
  89.  
  90.         out = "Table: " & td.Name & vbCrLf & String(40, "=") & vbCrLf
  91.  
  92.         ' Fields
  93.        out = out & "Fields:" & vbCrLf
  94.         For Each fld In td.Fields
  95.             out = out & "  " & fld.Name & " (" & fld.Type & ")"
  96.             out = out & IfNullText(fld.Properties("Description"), "")
  97.             out = out & vbCrLf
  98.         Next fld
  99.  
  100.         ' Indexes
  101.        out = out & vbCrLf & "Indexes:" & vbCrLf
  102.         For Each idx In td.Indexes
  103.             out = out & "  " & idx.Name & " (Unique=" & idx.Unique & "): "
  104.             For f = 0 To idx.Fields.Count - 1
  105.                 out = out & idx.Fields(f).Name & ", "
  106.             Next
  107.             out = Left(out, Len(out) - 2) & vbCrLf
  108.         Next idx
  109.  
  110.         SaveText EXPORT_ROOT & "Tables\" & td.Name & ".txt", out
  111.  
  112. SkipTable:
  113.     Next td
  114. End Sub
  115.  
  116.  
  117. '==============================================
  118. ' EXPORT QUERYDEFS
  119. '==============================================
  120. Private Sub ExportQueryDefs()
  121.     Dim qd As DAO.QueryDef
  122.     Dim out As String
  123.  
  124.     For Each qd In CurrentDb.QueryDefs
  125.         If Left(qd.Name, 1) = "~" Then GoTo SkipQuery
  126.  
  127.         out = "Query: " & qd.Name & vbCrLf & _
  128.               String(40, "=") & vbCrLf & vbCrLf & _
  129.               qd.SQL
  130.  
  131.         SaveText EXPORT_ROOT & "Queries\" & qd.Name & ".sql", out
  132.  
  133. SkipQuery:
  134.     Next qd
  135. End Sub
  136.  
  137.  
  138. '==============================================
  139. ' EXPORT RELATIONSHIPS
  140. '==============================================
  141. Private Sub ExportRelationships()
  142.     Dim rel As DAO.Relation
  143.     Dim fld As DAO.Field
  144.     Dim out As String
  145.  
  146.     For Each rel In CurrentDb.Relations
  147.         out = "Relation: " & rel.Name & vbCrLf & _
  148.               "From: " & rel.Table & vbCrLf & _
  149.               "To:   " & rel.ForeignTable & vbCrLf & _
  150.               "Attributes: " & rel.Attributes & vbCrLf & _
  151.               "Fields:" & vbCrLf
  152.  
  153.         For Each fld In rel.Fields
  154.             out = out & "  " & fld.Name & " -> " & fld.ForeignName & vbCrLf
  155.         Next fld
  156.  
  157.         SaveText EXPORT_ROOT & "Relationships\" & rel.Name & ".txt", out
  158.     Next rel
  159. End Sub
  160.  
  161.  
  162. '==============================================
  163. ' EXPORT REFERENCES
  164. '==============================================
  165. Private Sub ExportReferences()
  166.     Dim ref As Reference
  167.     Dim out As String
  168.  
  169.     out = "References" & vbCrLf & String(30, "=") & vbCrLf
  170.  
  171.     For Each ref In Application.References
  172.         out = out & ref.Name & " | " & ref.FullPath & " | Version " & _
  173.               ref.Major & "." & ref.Minor & vbCrLf
  174.     Next ref
  175.  
  176.     SaveText EXPORT_ROOT & "References\References.txt", out
  177. End Sub
  178.  
  179.  
  180. '==============================================
  181. ' EXPORT MACROS
  182. '==============================================
  183. Private Sub ExportMacros()
  184.     Dim obj As AccessObject
  185.  
  186.     For Each obj In CurrentProject.AllMacros
  187.         Application.SaveAsText acMacro, obj.Name, _
  188.                 EXPORT_ROOT & "Macros\" & obj.Name & ".txt"
  189.     Next obj
  190. End Sub
  191.  
  192.  
  193. '==============================================
  194. ' EXPORT FORMS (OPTIONAL HEAVY)
  195. '==============================================
  196. Private Sub ExportForms()
  197.     Dim obj As AccessObject
  198.     For Each obj In CurrentProject.AllForms
  199.         Application.SaveAsText acForm, obj.Name, _
  200.                 EXPORT_ROOT & "Forms\" & obj.Name & ".txt"
  201.     Next obj
  202. End Sub
  203.  
  204.  
  205. '==============================================
  206. ' EXPORT REPORTS (OPTIONAL HEAVY)
  207. '==============================================
  208. Private Sub ExportReports()
  209.     Dim obj As AccessObject
  210.     For Each obj In CurrentProject.AllReports
  211.         Application.SaveAsText acReport, obj.Name, _
  212.                 EXPORT_ROOT & "Reports\" & obj.Name & ".txt"
  213.     Next obj
  214. End Sub
  215.  
  216.  
  217. '==============================================
  218. ' UTILS
  219. '==============================================
  220. Private Sub SaveText(path As String, text As String)
  221.     Dim f As Integer
  222.     f = FreeFile
  223.     Open path For Output As #f
  224.     Print #f, text
  225.     Close #f
  226. End Sub
  227.  
  228. Private Function IfNullText(val, defaultText As String) As String
  229.     If IsNull(val) Then
  230.         IfNullText = ""
  231.     Else
  232.         IfNullText = " (" & val & ")"
  233.     End If
  234. End Function
Advertisement
Add Comment
Please, Sign In to add comment