简体   繁体   English

Access 2010-错误处理筛选器子窗体

[英]Access 2010 - Error Handling Filter Subform

how can I disable the filter of a Datagridview, using VBA, if after filtering there is no record? 如果过滤后没有记录,如何使用VBA禁用Datagridview的过滤器?

Here my first try: 这是我的第一次尝试:

Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
    If Me.RecordsetClone.RecordCount = 0 Then
        MsgBox ("Kein Datensatz gefunden. Filter wird entfernt.")
        Me.Form.FilterOn = False
        Me.Form.Requery
    End If
End Sub

Here is the second try: 这是第二次尝试:

Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
    Dim rs As DAO.Recordset

    Set rs = Me.RecordsetClone
    rs.Filter = Replace(Me.Filter, "[Tabelle1].", "")
    Set rs = rs.OpenRecordset()

    If rs.EOF Then
        Cancel = True
    End If
End Sub

In the second code I get the error 3061. The first code "works", but is finally not want I need. 在第二个代码中,我收到错误3061。第一个代码“有效”,但最终不需要。

Because: 因为:

I have 3 Forms. 我有3种表格。 MAIN, Sub1 and Sub2. 主,子1和子2。

In the MAIN are Sub1 , Sub2 and a single TextBox. 在MAIN中是Sub1,Sub2和单个TextBox。 The TextBox is call "psoudoID". 该文本框称为“ psoudoID”。 Sub1 is a normal Form to show the details of the different recordsets and is placed on the top of the MAIN. Sub1是普通表格,用于显示不同记录集的详细信息,并位于MAIN的顶部。 Below Sub1 is Sub2. Sub1下面是Sub2。 Sub2 is a Datagridview. Sub2是一个Datagridview。 When the user is clicking on a recordset on Sub2, the ID from Sub2 "goes" to the psoudoID and from there to Sub1. 当用户单击Sub2上的记录集时,Sub2的ID进入psoudoID,然后从那里进入Sub1。 You understand? 你明白?

Now is the problem, that the user can filter every column in Datagrid to find the recordset here needs and show all details above. 现在的问题是,用户可以过滤Datagrid中的每一列以找到此处需要的记录集并显示上面的所有详细信息。 But when the datagrid is empty after filtering , so Sub2 can't give an ID to the psoudoID and so on Sub1 is not displayed in MAIN much longer. 但是,当过滤后datagrid为空时,Sub2无法为psoudoID赋予ID,以此类推Sub1在MAIN中的显示时间不再更长。 The screen is empty on that place. 屏幕在那个地方是空的。 By clicking the Filter-Button in grid, Sub1 is on the screen again. 通过单击网格中的“筛选器按钮”,Sub1再次出现在屏幕上。

I want to disable the filter by clicking "Ok" of a MsgBox or automatically and not by clicking the Filter-Button on gridview. 我想通过单击MsgBox的“确定”或自动禁用筛选器,而不是单击gridview上的“筛选器按钮”来禁用筛选器。

I hope you can understand my discribtion. 希望您能理解我的看法。 And sorry for my bad English :-) 对不起,我的英语不好:-)

THX. 谢谢。

Vegeta 素食

I believe you're having problems with filters being Null , and with using Me.Filter before the filter is actually applied. 我相信您在使用过滤器Null以及在实际应用过滤器之前使用Me.Filter时遇到问题。 Also, re-assigning an object to a property/member of itself has caused problems for me before, so I try to avoid doing that. 同样,将对象重新分配给其属性/成员本身也给我带来了麻烦,因此,我尽量避免这样做。

The following should work: 以下应该工作:

Dim strOldFilter As String
Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
    Me.TimerInterval = 50
    strOldFilter = Nz(Me.Filter, "")
End Sub

Private Sub Form_Timer
        Me.TimerInterval = 0
        If Me.Filter = strOldFilter Then Exit Sub
        Dim rs As DAO.Recordset
        Dim strFilter As String
        strFilter = Nz(Me.Filter, "")
        If strFilter = "" Then
            'Handle this specific scenario
             Exit Sub
        End if
        Set rs = Me.RecordsetClone
        rs.Filter = Replace(strFilter, "[Tabelle1].", "")
        Dim rsf as DAO.Recordset
        Set rsf = rs.OpenRecordset

        If rsf.EOF Then
            Me.Filter = strOldFilter
        End If
End Sub

Note the potential to cause infinite loops that repeat every 50 msec if you change a filter resulting in 0 records to another filter resulting in 0 records (but you shouldn't be able to have a filter resulting in 0 records if the function works) 请注意,如果将一个过滤器将导致0个记录的记录更改为另一个过滤器将导致0个记录的记录,则可能会导致无限循环每50毫秒重复一次(但是,如果该函数起作用,则您将无法使过滤器导致0个记录)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM