[英]Access VBA Subform Dynamic SQL Parent Form Count Slow
I have a filters private function on a form, to dynamically generate the SQL for the record source on a subform, for searching records.我在表单上有一个过滤器私有 function,用于为子表单上的记录源动态生成 SQL,用于搜索记录。 I run this onload of the main form, and afterupdate of a series of text box (dates), comobo box, and check box controls.
我运行主窗体的这个 onload,以及一系列文本框(日期)、组合框和复选框控件的更新后。 Below is the Filters code.
下面是过滤器代码。 When the form loads, there are 15,247 records.
当表单加载时,有 15,247 条记录。 When I load the form, it is frozen for a while, getting the count.
当我加载表单时,它被冻结了一段时间,得到计数。
The subform is datasheet, and I wanted the counts bigger than the navigation buttons record count, so I put a textbox on the form footer of the main form.子表单是数据表,我希望计数大于导航按钮记录计数,所以我在主表单的表单页脚上放置了一个文本框。 To display the count of the subform records on the main form, I put a textbox in the form footer of the subform, and it's control source =count(*)
为了在主窗体上显示子窗体记录的计数,我在子窗体的窗体页脚中放置了一个文本框,它是 control source =count(*)
On the main form footer textbox, the controlsource is在主窗体页脚文本框中,控件源是
=[frmsubform].[Form]![txtCountrecs] & " Records" =[frmsubform].[Form]![txtCountrecs] & "记录"
It works, but it is so slow.它工作,但它是如此缓慢。 I'm wondering if there is anyway to make this less slow and laggy.
我想知道是否有办法让这变得不那么缓慢和滞后。
Private Sub Filters()
Dim fSQL As String
fSQL = "SELECT [fields] " & _
"FROM tbltable1 LEFT JOIN tbltable2 ON tbltable1.ID = tbltable2.FKtbl1ID " & _
"WHERE ((tbltable3.ID) Is Not Null) AND ((tbltable4.ID) Is Not Null)) "
If Nz(Me.cboFilterTo.Value, 0) <> 0 Then
fSQL = fSQL & " AND tbltable5.ID = " & Me.cboFilterTo.Column(0)
End If
If Nz(Me.cboFilterFrom.Value, 0) <> 0 Then
fSQL = fSQL & " AND tbltable6.ID = " & Me.cboFilterFrom.Column(0)
End If
If Nz(Me.txtDateOnOrAfter, 0) <> 0 Then
fSQL = fSQL & " AND tbltable4.dtdate >= " & Me.txtDateOnOrAfter
End If
If Nz(Me.txtDateOnOrBefore, 0) <> 0 Then
fSQL = fSQL & " AND tbltable4.dtdate <= " & Me.txtDateOnOrBefore
End If
If Nz(Me.chkUncompleted, 0) <> 0 Then
fSQL = fSQL & " AND nz(tbltable3.dtdatedone,0) = 0"
End If
'Debug.Print fSQL
Me.frmSubform.Form.RecordSource = fSQL
Me.frmSubform.Form.Requery
End Sub
If there is any way to make this faster, I'd love some suggestions.如果有任何方法可以加快速度,我会喜欢一些建议。
I found the answer, so I wanted to post the solution for anyone it may help.我找到了答案,所以我想为任何可能有帮助的人发布解决方案。
To make the form load faster, I converted the initial fSQL select, into a SQL view, and then made that initial fSQL be this:为了使表单加载更快,我将初始 fSQL select 转换为 SQL 视图,然后将初始 fSQL 设置为:
fSQL = "SELECT * FROM vw_MyNewView WHERE 1 = 1 "
Then, after I set the subform's recordsource and requery it, I have the following:然后,在我设置子表单的记录源并重新查询它之后,我有以下内容:
With Me.frmSubform.Form.RecordsetClone
If .RecordCount > 0 Then .MoveLast
ccount = .RecordCount
End With
Me.txtCountRecords = ccount & " Records"
The form loads way quicker, and this count tabulates quickly too.表单加载速度更快,而且这个计数也很快制表。
Hope this helps others!希望这对其他人有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.