gpt4 book ai didi

ms-access - 我可以使用 Access VBA 来确定表是否具有数据宏吗?

转载 作者:行者123 更新时间:2023-12-04 18:03:05 25 4
gpt4 key购买 nike

有没有办法通过 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 .这些功能似乎有很大的潜力。

最佳答案

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

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

归功于 a post on UtterAccess@Scotch's answer to a question on SO引用了 UtterAccess 帖子。

关于ms-access - 我可以使用 Access VBA 来确定表是否具有数据宏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31755802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com