如何使用VBA覆盖MS Access中链接表的宏?

时间:2017-03-20 16:50:21

标签: vba ms-access access-vba

我有一个拆分访问数据库。驻留在后端的表有"在变更之前"内置的宏(在后端)。我需要用新的宏覆盖这些宏。我有一个更新的宏的XML文件,我试图使用下面的代码覆盖宏。但是,下面的代码将宏放在前端中的链接表上。我需要宏来更新后端中表格上的宏。非常感谢任何和所有帮助。

LoadFromText acTableDataMacro, "tblEXAMPLE", strXMLpath

1 个答案:

答案 0 :(得分:0)

我想出了如何做到这一点。

Dim fso As Object
Dim oFile As Object
Dim strXML As String
Dim strXMLpath As String
Dim strBE As String
Dim accessApp As Access.Application

strXML = "<?xml version=" & Chr(34) & "1.0" & Chr(34) & " encoding=" & Chr(34) & "UTF-16" & Chr(34) & " standalone=" & Chr(34) & "no" & Chr(34) & "?>" & _
"<DataMacros xmlns=" & Chr(34) & "http://schemas.microsoft.com/office/accessservices/2009/11/application" & Chr(34) & "><DataMacro Event=" & Chr(34) & "BeforeChange" & Chr(34) & "><Statements><ConditionalBlock><If>"

strXMLpath = Application.CurrentProject.Path & "\XML_File.xml"

Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(strXMLpath)
oFile.WriteLine strXML
oFile.Close
Set fso = Nothing
Set oFile = Nothing

strBE = CurrentDb.TableDefs("TableName").Connect
strBE = Replace(strBE, ";Database=", "")

Set accessApp = CreateObject("Access.Application")
accessApp.OpenCurrentDatabase strBE, True

accessApp.LoadFromText acTableDataMacro, "TableName", strXMLpath

Kill strXMLpath