Wrong RecordCount on Filtered Form with SQL View D

2019-08-29 11:14发布

问题:

I am using an Access2010 project as frontend, referring to a MS SQL Server 2008 as backend. Within my Access project there is form frmKlientenÜbersicht. This form has a view abfKlientenÜbersicht as dataSource.

Now I am trying to get the current number of records showing up in my form by using this code:

Private Sub Form_Current()
    Debug.Print "Form_Current: " & anzahlDatensätze
    lblAnzahlDatensätze.Caption = anzahlDatensätze & " Klient(en)"
End Sub

Private Function anzahlDatensätze() As Integer
    Dim rs As Recordset
    Set rs = Me.RecordsetClone
    rs.MoveLast
    anzahlDatensätze = rs.RecordCount
End Function

This works fine until I am using some filters. If I am using any filter on my form, the number of records stays unchanged!

  • What do I have to change to get the current number of records showing up (if filtered or not)?
  • What is the reason why my code does not show the correct number of records?

EDIT: According to the given comments and answers I tried setting Count([pkKlient] onto a textbox (see new pic) and tried DCount("*", "abfKlientenÜbersicht", me.Filter) from within VBA Code.

Unfortunatelly it seems that the filterClause is not valid when using it as parameter value for DCount. (see pic for filterClause).

As you can see count(..) does not result in a correct number - and the filterClause (generated by access!!) seems not to be valid for use by DCount(..)

If someone wants to try it, on your own, just create an ADP, add a form, add a view, form dataSource is a view, set a filter, and try to get the number of records?!!

Looking forward for any comments/answers/hints!

回答1:

Some notes, there are a dozen things that could go wrong with this, it could hardly even be called tested. However, it was returning the correct count for me.

Apparently, the form filter just hides records, whereas this will apply a real filter. However, you need to get the format into the right shape for a valid filter. In the end, a WHERE statement would probably be easier.

Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)

    With Me.Recordset
        ''Filter
        If ApplyType = 1 Then
            ''Very, very roughly. Remove form name, use single quotes
            ''You will need a lot more code for safety
            sfilter = Replace(Me.Filter, "[" & Me.Name & "].", "")
            sfilter = Replace(sfilter, """", "'")

            .Filter = sfilter 

            MsgBox "Recordset : " & Me.Recordset.RecordCount & vbCrLf _
            & "Filtered : " & .RecordCount

        Else
            ''Remove filter - ApplyType 0
            .Filter = ""
        End If

    End With

End Sub

Additional note with similar caveats

You can also set a textbox to something on these lines:

=IIf([FilterOn]=True,DCount("id","ATable",
 Replace(Replace([Filter],"[" & [Name] & "].",""),"""","'")),Count([id]))

(Remove the break in the line, it is cosmetic)



回答2:

with VBA, DCount will give you what you need

DCount("*", "MyTable", Me.Filter)

If you want to put this on the form, there's an easier way. use an unbound box, and set it to =count([FieldName])

This count should remain correct, regardless of if it's counted filtered records or not.