3

有没有办法通过 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 语句,它首先检查表中是否存在数据宏。不过,我不知道该怎么写。

感谢在另一篇 SO 帖子中指出 SaveAsText 和 LoadFromText 函数的人们。这些功能似乎有很大的潜力。

4

2 回答 2

1

您可以使用简单查询来指示表是否具有数据宏:

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上的帖子和@Scotch对 SO上引用 UtterAccess 帖子的问题的回答。

于 2015-08-02T23:31:54.077 回答
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  
于 2015-07-31T22:15:18.210 回答