我需要对 Microsoft Access 2007 数据库和应用程序进行版本控制。目前,所有内容都包含在单个 mdb 文件中。
该应用程序包括:
- 形式
- VBA 代码
- 实际数据库
我假设我需要将数据库与表单/代码分开。我希望能够将表单/代码作为文本进行版本控制,以支持版本差异。
目前我无法访问 SourceSafe(我听说可能有一些访问支持),所以我更喜欢一个可以与 subversion 或 git 一起使用的解决方案。
我需要对 Microsoft Access 2007 数据库和应用程序进行版本控制。目前,所有内容都包含在单个 mdb 文件中。
该应用程序包括:
我假设我需要将数据库与表单/代码分开。我希望能够将表单/代码作为文本进行版本控制,以支持版本差异。
目前我无法访问 SourceSafe(我听说可能有一些访问支持),所以我更喜欢一个可以与 subversion 或 git 一起使用的解决方案。
Access 2007 有一个功能,您可以将数据库拆分为其表/查询(后端)和表单/报告(前端)。由于您的问题仅提及控制表单和模块的版本,因此这可能是一个更优雅的解决方案。我不知道拆分后模块在哪里,所以这可能是一个绊脚石。
Microsoft 提供VSTO(Visual Studio Tools for Office),它可以让您在 VS 中开发并通过任何 VS 插件(CVS/SVN/VSS/等)运行版本控制。
最后,您可以直接连接到 Visual Source Safe。 这篇 MSKB 文章有一些很好的信息和背景可供阅读,而这篇 Office Online 文章旨在帮助您启动和运行。
最后,我建议尽可能不要将代码从 Access 中取出。假设 VBA 编辑器是您的主要开发环境,您将在开发过程中添加额外的步骤,这些步骤不容易自动化。您所做的每项更改都需要手动导出、比较和存储,并且没有可用于导出更改的 Application.OnCompile 事件。更难的是,您必须在其他开发人员签入时手动导入所有更改的源文件。
我使用下面的代码从 Excel 文件中提取 vba 代码,您可以修改它以从 Access 中提取。
Sub ExtractVBACode(strSource, objFSO, strExportPath, objLogFile)
Dim objExcel
Dim objWorkbook
Dim objVBComponent
Dim strFileSuffix
Dim strExportFolder
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = true
Set objWorkbook = objExcel.Workbooks.Open(Trim(strSource))
strExportFolder = strExportPath & objFSO.GetBaseName(objWorkbook.Name)
If Not objFSO.FolderExists(strExportFolder) Then
objFSO.CreateFolder(strExportFolder)
End If
For Each objVBComponent In objWorkbook.VBProject.VBComponents
Select Case objVBComponent.Type
Case vbext_ct_ClassModule, vbext_ct_Document
strFileSuffix = ".cls"
Case vbext_ct_MSForm
strFileSuffix = ".frm"
Case vbext_ct_StdModule
strFileSuffix = ".bas"
Case Else
strFileSuffix = ""
End Select
If strFileSuffix <> "" Then
On Error Resume Next
Err.Clear
objVBComponent.Export strExportFolder & "\" & objVBComponent.Name & strFileSuffix
If Err.Number <> 0 Then
objLogFile.WriteLine ("Failed to export " & strExportFolder & "\" & objVBComponent.Name & strFileSuffix)
Else
objLogFile.WriteLine ("Export Successful: " & strExportFolder & "\" & objVBComponent.Name & strFileSuffix)
End If
On Error Goto 0
End If
Next
objExcel.DisplayAlerts = False
objExcel.Quit
结束子
您可以将表单提取为 XML 吗?
我一直在努力解决同样的问题。我最初编写的代码非常像现有的答案。诀窍是将所有模块放到文件系统上,但这种方法有一些缺点。走这条路,您可以从 VBA 项目中获取表单和报告,但无法将它们重新获取。因此,我创建了一个库作为我们Rubberduck VBE 插件的一部分。我编写的库负责在您无缝推送、拉取和提交时将所有代码导入/导出到 VBA 项目到/从存储库中导出。这是一个免费的开源项目,所以请随时下载并安装最新版本。
这是一个如何使用该库的示例。我将在未来的版本中添加与 VBA 编辑器的实际集成。
Dim factory As New Rubberduck.SourceControlClassFactory
Dim repo As Rubberduck.IRepository
Dim git As ISourceControlProvider
Dim xl As New Excel.Application
xl.Visible = true
Dim wb As Excel.Workbook
Set wb = xl.Workbooks.Open("C:\Path\to\workbook.xlsm")
' create class instances to work with
Set repo = factory.CreateRepository(wb.VBProject.Name, "C:\Path\to\local\repository\SourceControlTest", "https://github.com/ckuhn203/SourceControlTest.git")
Set git = factory.CreateGitProvider(wb.VBProject, repo, "userName", "passWord")
' Create new branch to modify.
git.CreateBranch "NewBranchName"
' It is automatically checked out.
Debug.Print "Current Branch: " & git.CurrentBranch
' add a new standard (.bas) code module and a comment to that file
wb.VBProject.VBComponents.Add(vbext_ct_StdModule).CodeModule.AddFromString "' Hello There"
' add any new files to tracking
Dim fileStat As Rubberduck.FileStatusEntry
For Each fileStat In git.Status
' fileStat.FileStatus is a bitwise enumeration, so we use bitwise AND to test for equality here
If fileStat.FileStatus And Rubberduck.FileStatus.Added Then
git.AddFile fileStat.FilePath
End If
Next
git.Commit "commit all modified files"
' Revert the last commit, throwing away the changes we just made.
git.Revert