かざいむ日誌

IT関係で知ったことなどを記事としてあげていきます。内容に不備や質問などあればぜひコメントをよせてください。

Access帳票開発でレポート、クエリとかを複数人で触るための手順。

Access帳票を作るプロジェクトで、同じAccessに触れないのがもどかしかったので、VBAみたく、Export、Importでバージョン管理できないか調べてみた。どうも出来るらしい。手順はちょっと微妙だが、こんな感じ。

1.バージョン管理ツールで最新のAccessを取得

2.ローカルで帳票等を編集

3.編集したAccessからクエリ、レポート、フォームなどのオブジェクトを抜き出す。

4.Accessファイルは変更の取り消しを行い、最新のファイルを取得する。

5.Accessファイルから3.とは別のフォルダにオブジェクトを抜き出す。

6.WinMerge等でフォルダを比較し、変更を行ったファイルで、5.のフォルダ内のファイルを上書きする。

7.5.のオブジェクトファイルを取り込む

(オブジェクトがあってもエラーにならず上書きされるらしい、、、。)

 

上記の手順に合わせて、以下のようなVBAのコードを書きました。

Option Compare Database

Option Explicit

'Export
Public Sub ExportModules()
Dim outputDir As String
Dim currentDat As Object
Dim currentProj As Object

outputDir = GetDir(CurrentDb.Name)
Set currentDat = Application.CurrentData
Set currentProj = Application.CurrentProject
ExportObjectType acQuery, currentDat.AllQueries, outputDir, ".qry"
ExportObjectType acForm, currentProj.AllForms, outputDir, ".frm"
ExportObjectType acReport, currentProj.AllReports, outputDir, ".rpt"
ExportObjectType acMacro, currentProj.AllMacros, outputDir, ".mcr"
ExportObjectType acModule, currentProj.AllModules, outputDir, ".bas"
End Sub

'ファイル名のディレクトリ部分を返す
Private Function GetDir(FileName As String) As String
Dim p As Integer

GetDir = FileName
p = InStrRev(FileName, "\")
If p > 0 Then GetDir = Left(FileName, p - 1)
End Function

'特定の種類のオブジェクトをエクスポートする
Private Sub ExportObjectType(ObjType As Integer, _
ObjCollection As Variant, Path As String, Ext As String)

Dim obj As Variant
Dim filePath As String

For Each obj In ObjCollection
filePath = Path & "\dbObj\" & obj.Name & Ext
SaveAsText ObjType, obj.Name, filePath
Debug.Print "Save " & obj.Name
Next
End Sub

'import objects
Public Sub ImportModules()
Dim inputDir As String
Dim currentDat As Object
Dim currentProj As Object

inputDir = GetDir(CurrentDb.Name) & "\dbObj\"
Set currentDat = Application.CurrentData
Set currentProj = Application.CurrentProject
ImportObjectType (inputDir)
End Sub

'import all objects in a folder
Private Sub ImportObjectType(Path As String)

Dim currentDat As Object
Dim currentProj As Object

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim folder As Object
Dim myFile, objectname, objecttype
Set folder = CreateObject _
("Scripting.FileSystemObject").GetFolder(Path)
Dim oApplication

For Each myFile In folder.Files
objecttype = fso.GetExtensionName(myFile.Name)
objectname = fso.GetBaseName(myFile.Name)

If (objecttype = "frm") Then
Application.LoadFromText acForm, objectname, myFile.Path
ElseIf (objecttype = "bas") Then
Application.LoadFromText acModule, objectname, myFile.Path
ElseIf (objecttype = "mcr") Then
Application.LoadFromText acMacro, objectname, myFile.Path
ElseIf (objecttype = "rpt") Then
Application.LoadFromText acReport, objectname, myFile.Path
ElseIf (objecttype = "qry") Then
Application.LoadFromText acQuery, objectname, myFile.Path
End If
Next
End Sub

以下のURLを参考にさせていただきました。

ありがとうございます。

blogs.msdn.microsoft.com

d.hatena.ne.jp

 

d.hatena.ne.jp

stackoverflow.com