最后,我搜索了一些其他问题,并考虑以编程方式转储可能的违规信息。这是我为做我想做的事而混合的两个不同答案。对 VBA 中即时窗口的限制,它将从 Debug.Print 保留多少行,我使用 WScript 对象转储到日志文件。
代码基本上:
- 依次打开数据库中的每个表单
- 转储其 RecordSource 描述
- 然后对于其表单上的每个控件,以某种方式转储可能包含 SQL 的相关信息
- 注意:我使用导致错误的 Writeline(某些代码)将失败而不写入的事实,以避免对要转储或不转储属性的不同控件类型进行大量测试。
- 然后关闭表格
- 然后它遍历数据库中的所有查询,并转储 SQL 代码
(注1:最后这没有找到我的问题的答案 - 请参阅我最近的另一个问题!!)
(注意2:这是一个快速而肮脏的脚本。我注意到它第一次运行时 WriteStream 没有写任何东西,即使它在单步执行代码时它似乎正在做某事。无论如何,当我再次运行它时它工作.没有花时间调试为什么!)。
Function DumpFormsAndQueries()
Dim obj As AccessObject
Dim objctrl As Control
Dim frm As Form
Dim dbs As Object
Dim fsoSysObj As FileSystemObject
Dim filFile As Object
Dim txsStream As TextStream
Dim strPath As String
Set dbs = Application.CurrentProject
Set fsoSysObj = New FileSystemObject
' Return Windows Temp folder.
strPath = "C:\Temp\"
On Error Resume Next
' See if file already exists.
Set filFile = fsoSysObj.GetFile(strPath & "Database_Form_dump.Log")
' If not, then create it.
If Err <> 0 Then
Set filFile = fsoSysObj.CreateTextFile(strPath & "Database_Form_dump.Log")
End If
Debug.Print ">> dumping to: " & strPath & "Database_form_dump.log"
Set txsStream = filFile.OpenAsTextStream(ForAppending)
For Each obj In dbs.AllForms
DoCmd.OpenForm obj.name, acDesign
Set frm = Forms(obj.name)
Debug.Print ">>>> dump form: " & obj.name
txsStream.WriteLine "====================================================================="
txsStream.WriteLine "Form : " & obj.name
txsStream.WriteLine "RecordSource: " & frm.RecordSource
txsStream.WriteLine "====================================================================="
For Each objctrl In frm.Controls
txsStream.WriteLine " --------------------------------------------------"
txsStream.WriteLine " : " & objctrl.name & " Type = " & TypeName(objctrl)
txsStream.WriteLine " --------------------------------------------------"
On Error Resume Next
txsStream.WriteLine " >>>> Recordsource: (" & objctrl.RecordSource & ")"
txsStream.WriteLine " >>>> Controlsource: (" & objctrl.ControlSource & ")"
txsStream.WriteLine " >>>> Rowsource: (" & objctrl.RowSource & ")"
txsStream.WriteLine " >>>> Caption: (" & objctrl.Caption & ")"
txsStream.WriteLine " >>>> Text: (" & objctrl.Text & ")"
txsStream.WriteBlankLines 1
Next objctrl
DoCmd.Close acForm, obj.name, acSaveNo
txsStream.WriteBlankLines 3
Next obj
txsStream.WriteLine "====================================================================="
txsStream.WriteLine " Q U E R I E S - in database"
txsStream.WriteLine "====================================================================="
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Set db = CurrentDb()
For Each qdf In db.QueryDefs
txsStream.WriteLine "Query: " & qdf.name
txsStream.WriteLine "SQL (start) ---------------------------------------------------- "
txsStream.WriteLine qdf.sql
txsStream.WriteLine "SQL (end) ---------------------------------------------------- "
Next qdf
Set qdf = Nothing
Set db = Nothing
txsStream.Close
Debug.Print ">> ended"
End Function