有没有办法通过VBA确定Access表是否包含数据宏?我的表的大多数上有数据宏,但如果遇到没有它的表,我的代码就会失败。
我没有收到错误消息。相反,代码保持运行,好像它处于无限循环中,但我必须强制Access退出以逃避。
具体来说,我正在尝试保存所有表和数据宏,以便我可以使用(未记录的)LoadFromText函数稍后重新创建它们。
我在下面的代码示例中突出显示了问题,** BUG **。
For Each td In db.TableDefs
If Left(td.Name, 4) <> "MSys" Then
'Save the table as a text file.
DoCmd.TransferText acExportDelim, , td.Name, sExportLocation & "Table_" & td.Name & ".txt", True
'Save the table's data macro as an XML file.
'** BUG **: If a table doesn't have a data macro, Access freezes/starts infinite loop.
Application.SaveAsText acTableDataMacro, td.Name, sExportLocation & "Table_" & td.Name & "_DataMacro.xml"
End If
Next td
我假设我想要某种嵌套的If语句,它首先检查表中是否存在数据宏。不过,我不知道怎么写。
感谢那些指出SaveAsText和LoadFromText函数in another SO post的人。这些功能似乎有很大的潜力。
答案 0 :(得分:0)
要查看数据库是否包含宏,您可以使用DAO中的文档化方法。以下是https://msdn.microsoft.com/en-us/library/office/ff191764.aspx的修改示例:
Sub ContainerObjectX()
Dim dbsNorthwind As Database
Dim ctrLoop As Container
Dim prpLoop As Property
Dim docItem As Document
' Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set dbsNorthwind = CurrentDb
With dbsNorthwind
' Enumerate Containers collection.
For Each ctrLoop In .Containers
Debug.Print "Properties of " & ctrLoop.Name _
& " container"
' Enumerate Properties collection of each
' Container object.
For Each prpLoop In ctrLoop.Properties
Debug.Print " " & prpLoop.Name _
& " = "; prpLoop
Next prpLoop
For Each docItem In ctrLoop.Documents
Debug.Print " docItem.Name = "; docItem.Name
Next docItem
Next ctrLoop
.Close
End With
End Sub
因此,您需要在“脚本”容器下检查文档。
我原来的回答: 我认为你可以使用ExportXML和ImportXML它更强大,并能够导出和导入所有访问对象。 例如:
ExportXML acExportTable, "tblMain", CM_GetDBPath() & "AccessFunc_Tbl.xml" _
, CM_GetDBPath() & "AccessFunc_TblShema.xml", CM_GetDBPath() & "AccessFunc_Tbl.xsl" _
, "Images", , acEmbedSchema
....
ImportXML CM_GetDBPath() & "AccessFunc_Tbl.xml", acAppendData
完整的例子在这里:http://5codelines.net/wp-content/uploads/xml_1_sampe.rar
您也可以使用ADODB库。
Public Function EportTblToXml(ByVal imTblFrom As String _
, ByVal imFileTo As String)
Dim rstData As ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
Set rstData = New ADODB.Recordset
rstData.Open "SELECT * FROM " & imTblFrom, cnn _
, adOpenKeyset, adLockOptimistic
Call SaveRstToXml(rstData, imFileTo)
rstData.Close
End Function
Public Function LoadXmlToRst(ByVal stFileName As String) As ADODB.Recordset
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.Open stFileName
Set LoadXmlToRst = rst
End Function
答案 1 :(得分:0)
您可以使用简单查询来指示表是否具有数据宏:
SELECT [Name] FROM MSysObjects WHERE Not IsNull(LvExtra) and Type =1
此宏可以应用于问题中的VBA代码,如下所示:
For Each td In db.TableDefs
If Left(td.Name, 4) <> "MSys" Then
'Save the table as a text file.
DoCmd.TransferText acExportDelim, , td.Name, sExportLocation & _
"Table_" & td.Name & ".txt", True
'Define a recordset to determine if the table has a data macro.
sql = "SELECT [Name] FROM MSysObjects WHERE Not IsNull(LvExtra) and " & _
"Type = 1 and [Name] = '" & td.Name & "'"
Set rst = db.OpenRecordset(sql, dbOpenSnapshot)
'If the table has a data macro, save the data macro as an XML file.
If rst.RecordCount <> 0 Then
Application.SaveAsText acTableDataMacro, td.Name, sExportLocation & _
"Table_" & td.Name & "_DataMacro.xml"
End If
'Close the recordset and clear its variable.
If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If
End If
Next td
信用转到引用UtterAccess帖子的a post on UtterAccess和@Scotch's answer to a question on SO。