如何在Access数据库中列出DataMacro对象?

时间:2013-04-26 20:21:13

标签: vba ms-access

是否可以以编程方式枚举Access 2010+数据库中的数据宏?如果是这样,怎么样?


注意Data Macros是在表设计器UI的上下文中创建的类似触发器的过程。它们是Acces 2010中的新功能。它们与普通宏不同,它们很容易枚举。

他们有自己的新AcObjectType枚举值:acTableDataMacro,但我找不到引用它们的Access或DAO对象模型的其他方面。它们甚至没有出现在MSysObjects表中。

1 个答案:

答案 0 :(得分:2)

此代码将DataMacro元数据导出到XML文档(Source):

Sub DocumentDataMacros()

'loop through all tables with data macros
'write data macros to external files
'open folder with files when done

' click HERE
' press F5 to Run!

' Crystal
' April 2010

On Error GoTo Proc_Err

' declare variables
Dim db As DAO.Database _
, r As DAO.Recordset

Dim sPath As String _
, sPathFile As String _
, s As String

' assign variables
Set db = CurrentDb

sPath = CurrentProject.Path & "\"

s = "SELECT [Name] FROM MSysObjects WHERE Not IsNull(LvExtra) and Type =1"

Set r = db.OpenRecordset(s, dbOpenSnapshot)

 ' loop through all records until the end
Do While Not r.EOF
sPathFile = sPath & r!Name & "_DataMacros.xml"
'Big thanks to Wayne Phillips for figuring out how to do this!
SaveAsText acTableDataMacro, r!Name, sPathFile
'have not tested SaveAsAXL -- please share information if you do
r.MoveNext
Loop

' give user a message
MsgBox "Done documenting data macros for " & r.RecordCount & " tables ", , "Done"

Application.FollowHyperlink CurrentProject.Path

Proc_Exit:
' close and release object variables
If Not r Is Nothing Then
r.Close
Set r = Nothing
End If

Set db = Nothing
Exit Sub

Proc_Err:
MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " DocumentDataMacros"

Resume Proc_Exit
Resume

End Sub
编辑:Gord指出,您希望DataMacros与标准宏相对立。我找到了一些代码并对其进行了测试(它有效)here

我按照该链接测试了top函数,它为XML文档中的每个表保存了有关表宏的信息。它很好地工作,对任何人写道都是道具。