|
[size=-1][size=-1]Public Sub backupModules()
On Error GoTo ErrHandler
Dim sPath As String
sPath = "C:\Backup"
MsgBox "All VBA code exported = " & exportModules(sPath)
Exit Sub
ErrHandler:
MsgBox "Error in backupModules( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear
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
[size=-1] '--------------------------------------------------------------
' Export standard modules and classes.
'--------------------------------------------------------------
[size=-1]
sqlStmt = "SELECT [Name] " & _
"FROM MSysObjects " & _
"WHERE ([Type] = -32761);"
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
SaveAsText acModule, recSet.Fields(0).Value, _
sDestPath & recSet.Fields(0).Value & ".bas"
If (Not (recSet.EOF)) Then
recSet.MoveNext
End If
Next idx
End If
[size=-1] '--------------------------------------------------------------
' Export form modules.
'--------------------------------------------------------------
[size=-1]
sqlStmt = "SELECT [Name] " & _
"FROM MSysObjects " & _
"WHERE ([Type] = -32768);"
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
Exit Function
ErrHandler:
MsgBox "Error in exportModules( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear
exportModules = False [size=-1]' Failed.
[size=-1] GoTo CleanUp
End Function [size=-1]' exportModules( ) |
|