how can I disable the filter of a Datagridview, using VBA, if after filtering there is no record?
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.
Because:
I have 3 Forms. MAIN, Sub1 and Sub2.
In the MAIN are Sub1 , Sub2 and a single TextBox. The TextBox is call "psoudoID". Sub1 is a normal Form to show the details of the different recordsets and is placed on the top of the MAIN. Below Sub1 is Sub2. Sub2 is a Datagridview. When the user is clicking on a recordset on Sub2, the ID from Sub2 "goes" to the psoudoID and from there to 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. 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. The screen is empty on that place. By clicking the Filter-Button in grid, Sub1 is on the screen again.
I want to disable the filter by clicking "Ok" of a MsgBox or automatically and not by clicking the Filter-Button on gridview.
I hope you can understand my discribtion. And sorry for my bad English :-)
THX.
Vegeta
Me.FilterOn
toFalse
or modifying the filter instead of settingCancel
toTrue
? (Cancel
cancels the current filter action and reverts to the previous filter if any, whileMe.FilterOn = False
turns the filter off,Cancel
seems appropriate for what you want) – Erik ASet rs = rs.OpenRecordset()
withDim rsf As Recordset_ Set rsf = rs.OpenRecordSet
. Does that help? – Erik Ars.Filter = Replace(CStr(Nz(Me.Filter, "")), "[Tabelle1].", "")
help? That way you make sure Replace gets 3 strings and no NULLs. – Erik A