Export Modules to Text Files in Microsoft Access

by demtron on Sunday, October 12, 2008 09:12 PM

I was recently asked to review the code differences between two Access databases.  One was the then-current production version of the database.  The other was a version that was being modified with new business rules.  As a first pass, I decided to export all the form, report, and code modules from each database into separate text files in separate folders, then use a source code comparison tool to compare both sets of files for changes.

I may need to create some code that will compare other objects such as tables and queries, but for right now, comparing source code files has proved to be a good start for getting a handle on the changes.  Here's the code I used to export all the modules.

Option Compare Database
Option Explicit
Public Sub CreateModuleFiles()
'On Error GoTo Err_Handler
Dim accObj As AccessObject  'Each module/form/report.
Dim bWasOpen As Boolean     'Flag to leave form/report open if it was open.
Dim strDoc As String        'Name of each form/report
'Stand-alone modules.
For Each accObj In CurrentProject.AllModules
Call WriteOutModules(accObj.Name, True)
'Modules behind forms.
For Each accObj In CurrentProject.AllForms
strDoc = accObj.Name
bWasOpen = accObj.IsLoaded
If Not bWasOpen Then
DoCmd.OpenForm strDoc, acDesign, WindowMode:=acHidden
End If
If Forms(strDoc).HasModule Then
Call WriteOutModules("Form_" & accObj.Name, False)
End If
If Not bWasOpen Then
DoCmd.Close acForm, strDoc, acSaveNo
End If
'Modules behind reports.
For Each accObj In CurrentProject.AllReports
strDoc = accObj.Name
bWasOpen = accObj.IsLoaded
If Not bWasOpen Then
'In Access 2000, remove the ", WindowMode:=acHidden" from the next line.
DoCmd.OpenReport strDoc, acDesign, WindowMode:=acHidden
End If
If Reports(strDoc).HasModule Then
Call WriteOutModules("Report_" & accObj.Name, False)
End If
If Not bWasOpen Then
DoCmd.Close acReport, strDoc, acSaveNo
End If
End Sub
Private Function WriteOutModules(strModule As String, bIsStandAlone As Boolean)
Dim strCode As String
Dim bWasOpen As Boolean     'Flag applies to standalone modules only.
Dim lngLineNo As Long
If bIsStandAlone Then
bWasOpen = CurrentProject.AllModules(strModule).IsLoaded
End If
If Not bWasOpen Then
DoCmd.OpenModule strModule
End If
For lngLineNo = 1 To Modules(strModule).CountOfLines
strCode = strCode & Modules(strModule).Lines(lngLineNo, 1) & vbCrLf
strCode = strCode & vbCrLf & vbCrLf & vbCrLf
If Not bWasOpen Then
On Error Resume Next
DoCmd.Close acModule, strModule, acSaveNo
End If
Dim intFile As Integer
'*** Set to next free open number ***
intFile = FreeFile()
Open "C:\output\modules\" & strModule & ".txt"   
For Output As #intFile
Print #intFile, strCode
Close #intFile
Debug.Print strModule & " complete"
End Function

Powered by BlogEngine.NET
Theme by Mads Kristensen · Adapted by Demtron

Bookmark and Share


<<  May 2024  >>

View posts in large calendar
Log in

Milwaukee SEO Company

Milwaukee Access Programmer/Developer

Milwaukee Website Designer and Developer

Marketing / SEO

Blog Directory
blogarama - the blog directory
Milwaukee area SEO, SEM, ASP.Net