MS Access 是否允许在不打开表单本身的情况下获取表单的记录源值?我现在正在尝试优化我的代码,我所做的只是隐藏表单然后获取 Recordsource 表单查询,但是加载需要时间,因为某些表单在加载时触发代码。
由于您无法在设计视图中打开表单,并且定期打开表单会导致性能问题,因此还有一些解决方法:
根据您想要检查关闭表单的记录源的方式,您可以在单独的模块中按以下方式设置全局变量:
Public glb_getrecordsource As String
Run Code Online (Sandbox Code Playgroud)
之后,根据您调用代码的方式,您可以执行以下操作:
Private Sub Command1_Click()
glb_getrecordsource = "Yes"
DoCmd.OpenForm "Form1"
'... Do something
End Sub
Run Code Online (Sandbox Code Playgroud)
然后,作为最后一步,将以下内容放在表单的 OnLoad 事件的开头:
Private Sub Form_Load()
If glb_getrecordsource = "Yes" Then
glb_getrecordsource = Me.Form.RecordSource
DoCmd.Close acForm, "Form1", acSaveYes
Exit Sub
End If
'... Usual OnLoad events
End Sub
Run Code Online (Sandbox Code Playgroud)
这至少可以解决性能问题,因为您不会在表单的加载事件中触发任何耗时的事件。
另一种解决方法: 您可以将表单导出到 .txt 文件,然后在文本文件中搜索记录源。以下代码会将您的表单导出到指定文件夹中的 .txt 文件:
Dim db As Database
Dim d As Document
Dim c As Container
Dim sExportLocation As String
Set db = CurrentDb()
sExportLocation = "C:\AD\" 'Do not forget the closing back slash! ie: C:\Temp\
Set c = db.Containers("Forms")
For Each d In c.Documents
Application.SaveAsText acForm, d.Name, sExportLocation & "Form_" & d.Name & ".txt"
Next d
Run Code Online (Sandbox Code Playgroud)
部分代码借自本论坛。之后,您只需打开文件并搜索记录源即可。如果记录源为空,则不会导出,因此请记住这一点。另外,我怀疑这会提高性能,但谁知道呢!