2013-04-26 42 views
3

是否有可能以编程方式枚举Access 2010+数据库中的数据宏?如果是这样,怎么样?如何在Access数据库中列出DataMacro对象?


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

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

回答

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 

编辑:戈德指出,你想反对标准宏DataMacros。我发现了一些代码并进行了测试(它的工作原理)here

我测试了顶级函数,当您遵循该链接时,它保存了XML文档中每个表的关于表宏的信息。它很好地工作,支持写它的人。

+1

您是否尝试使用该方法列出数据宏(Access 2010中的新增功能),而不是“常规”宏?我做了,它不适合我...... – 2013-04-27 07:40:45

+0

@GordThompson其实,我只是用标准的宏来试用它。我刚刚发现了一些数据宏的代码,将用我发现的内容更新我的答案。 – Scotch 2013-04-27 08:06:00

+1

@Scotch:重写你的答案,只是解决数据宏(这是问题,而不是普通的宏),我将它标记为答案。对于未来的读者来说,让内容出错是件容易混淆的事,但链接是正确的 – 2013-04-29 12:38:59