I have a filters private function on a form, to dynamically generate the SQL for the record source on a subform, for searching records. I run this onload of the main form, and afterupdate of a series of text box (dates), comobo box, and check box controls. Below is the Filters code. When the form loads, there are 15,247 records. 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(*)
On the main form footer textbox, the controlsource is
=[frmsubform].[Form]![txtCountrecs] & " Records"
The count works, but it is so slow, even slower than the form itself. This means, the form will load and show the records, but be frozen, for about 30 seconds, before the count shows up and the form is usable.
I'm wondering if there is anyway to make this less slow and laggy. The below code is generalized, but enough to give the gist of what the filters code does, to dynamically build the subform's recordsource, based on no filters (onload or reset filters button) or with any number of the filters set.
If there is any way to make this faster, I'd love some suggestions.
misscrf
It is never too late to become what you could have been ~ George Eliot
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(*)
On the main form footer textbox, the controlsource is
=[frmsubform].[Form]![txtCountrecs] & " Records"
The count works, but it is so slow, even slower than the form itself. This means, the form will load and show the records, but be frozen, for about 30 seconds, before the count shows up and the form is usable.
I'm wondering if there is anyway to make this less slow and laggy. The below code is generalized, but enough to give the gist of what the filters code does, to dynamically build the subform's recordsource, based on no filters (onload or reset filters button) or with any number of the filters set.
Code:
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.
misscrf
It is never too late to become what you could have been ~ George Eliot