标题: back up all of your VBA code modules to individual files [打印本页] 作者: wangling 时间: 2008-6-4 22:40 标题: back up all of your VBA code modules to individual files [size=-1][size=-1]Public Sub backupModules()
End Sub
[size=-1][size=-1]Public Function exportModules(sDestPath As String) As Boolean
On Error GoTo ErrHandler
Dim recSet As DAO.Recordset
Dim frm As Form
Dim rpt As Report
Dim sqlStmt As String
Dim sObjName As String
Dim idx As Long
Dim fOpenedRecSet As Boolean
[size=-1] '--------------------------------------------------------------
' Ensure that there's a backslash at the end of the path.
'--------------------------------------------------------------
[size=-1]
If (Mid$(sDestPath, Len(sDestPath), 1) <> "\") Then
sDestPath = sDestPath & "\"
End If
Set recSet = CurrentDb().OpenRecordset(sqlStmt)
fOpenedRecSet = True
If (Not (recSet.BOF And recSet.EOF)) Then
recSet.MoveLast
recSet.MoveFirst
For idx = 1 To recSet.RecordCount
sObjName = recSet.Fields(0).Value
DoCmd.OpenForm sObjName, acDesign
Set frm = Forms(sObjName)
If (frm.HasModule) Then
DoCmd.OutputTo acOutputModule, "Form_" & _
sObjName, acFormatTXT, sDestPath & _
sObjName & ".bas"
End If
DoCmd.Close acForm, sObjName
If (Not (recSet.EOF)) Then
recSet.MoveNext
End If
Next idx
End If
[size=-1][size=-1] '--------------------------------------------------------------
' Export report modules.
'--------------------------------------------------------------
[size=-1][size=-1] sqlStmt = "SELECT [Name] " & _
"FROM MSysObjects " & _
"WHERE ([Type] = -32764);"
Set recSet = CurrentDb().OpenRecordset(sqlStmt)
fOpenedRecSet = True
If (Not (recSet.BOF And recSet.EOF)) Then
recSet.MoveLast
recSet.MoveFirst
For idx = 1 To recSet.RecordCount
sObjName = recSet.Fields(0).Value
DoCmd.OpenReport sObjName, acDesign
Set rpt = Reports(sObjName)
If (rpt.HasModule) Then
DoCmd.OutputTo acOutputModule, "Report_" & _
sObjName, acFormatTXT, sDestPath & _
sObjName & ".bas"
End If
DoCmd.Close acReport, sObjName
If (Not (recSet.EOF)) Then
recSet.MoveNext
End If
Next idx
End If
[size=-1][size=-1] exportModules = True [size=-1]' Success.
[size=-1]
CleanUp:
If (fOpenedRecSet) Then
recSet.Close
fOpenedRecSet = False
End If
Set frm = Nothing
Set rpt = Nothing
Set recSet = Nothing