简体   繁体   English

访问 VBA 子窗体动态 SQL 父窗体计数慢

[英]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.

相关问题 单击父表单时,Access VBA子表单事件不会触发 - Access VBA subform event wont fire when parent form clicked 嵌入到导航表单中时,访问VBA子表单不会过滤其他子表单 - Access VBA SubForm Not Filtering Other Subform When Embbedded in Navigation Form 已解决:VBA Access 2010 执行 MySQL 从子表单中选择直通并将输出记录集发送到父表单 - SOLVED: VBA Access 2010 execute MySQL Select passthrough from a subform and send output recordset to a parent form 如何使用 vba 从子表单单击父表单导航选项卡 - How to click parent Form Navigation Tab from subform with vba 访问 VBA - 如何获取父子表单的属性,或者获取子表单的用户给定名称(不是 object 参考名称) - Access VBA - How to get the properties of a parent subform, or, get the user-given name for a subform (not the object reference name) Access VBA-我可以将表单实例分配给Subform控件吗? - Access VBA - Can I assign an instance of a form to a Subform control? 按钮搜索子窗体,然后在主窗体中查找记录(Access VBA) - Button searches subform, then finds record in main form (Access VBA) 连续形式的 MS Access VBA 代码在子表单中不起作用 - MS Access VBA code in continuous form not working while a subform 如何将子窗体链接到主窗体VBA访问中的列表 - how do I link a subform to a list in main form vba access 使用表单和子表单访问 VBA 代码格式更新表 - Access VBA code format updating table using form and subform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM