设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1102|回复: 1
打印 上一主题 下一主题

[模块/函数] back up all of your VBA code modules to individual files

[复制链接]
跳转到指定楼层
1#
发表于 2008-6-4 22:40:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[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( )
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2008-6-5 09:07:59 | 只看该作者
学习一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-22 18:09 , Processed in 0.079134 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表