vba - 如何列出 Access 数据库中的 DataMacro 对象?

标签 vba ms-access

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


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

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

最佳答案

此代码会将 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 文档中的每个表保存了有关表宏的信息。它工作得很好,支持编写它的人。

关于vba - 如何列出 Access 数据库中的 DataMacro 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16244608/

相关文章:

c# - 如何将参数从 Excel 中的 VBA 脚本作为参数传递到外部可执行文件 (C#)?

vba - 清除所有工作表内容的最快方法VBA

sql-server - 将数据从 SQL Server 移动到 MS Access mdb

vba - 打开 .rtf 附件并粘贴当前电子邮件正文中的内容

Java SQL 问题

vba - 精确的日期差异(以月和日为单位)

forms - 将 MS Access 表单中的文本框设置为数组?

excel - 使用单元构建范围

python - 我遇到错误 'Attempt to use a closed connection.'

ms-access - 如何修改已保存的 Microsoft Access 2007 或 2010 导入规范?