Get Filter By Form Data

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Get Filter By Form Data

Post by agibsonsw »

Hello. Access 2007.
If someone uses the in-built Filter by Form feature is it possible to then press a button to produce a report based on the same criteria?
Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get Filter By Form Data

Post by HansV »

Try something like the following. It assumes that you have already created a report based on the same table or report as the form.

Code: Select all

Private Sub cmdReport_Click()
  Dim strFilter As String
  On Error GoTo ErrHandler

  If Me.FilterOn Then
    strFilter = Me.Filter
  End If
  DoCmd.OpenReport ReportName:="rptMyReport", View:=acViewPreview, WhereCondition:=strFilter
  Exit Sub

ErrHandler:
  If Not Err = 2501 Then
    MsgBox Err.Description, vbExclamation
  End If
End Sub
cmdReport is the name of the command button and rptMyReport is the name of the report.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Get Filter By Form Data

Post by agibsonsw »

Thank you. That's looks good. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Get Filter By Form Data

Post by agibsonsw »

Hello again.
If someone uses the Or tab the procedure doesn't seem to work. Should Me.Filter also pick up the Or criteria? Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get Filter By Form Data

Post by HansV »

It should. Here is a screenshot of filter by form with something entered in the Or tab:
x170.png
And this is the filter after applying it:
x171.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Get Filter By Form Data

Post by agibsonsw »

Thanks.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Get Filter By Form Data

Post by agibsonsw »

Hello again.
If there are no records found when using Filter by Form is it possible to produce a MsgBox rather than
the empty resultset? Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get Filter By Form Data

Post by HansV »

You can use the On Apply Filter event of the form. Let's say the record source of the form is named tblProducts, as in the above example.

Code: Select all

Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
  If ApplyType = acApplyFilter Then
    If DCount("*", "tblProducts", Me.Filter) = 0 Then
      MsgBox "There are no records satisfying the filter conditions.", vbExclamation
      Cancel = True
    End If
  End If
End Sub
This version cancels applying the filter, i.e. Filter by Form remains open. If you prefer to return to the form itself, with the filter turned off, you could use

Code: Select all

Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
  If ApplyType = acApplyFilter Then
    If DCount("*", "tblProducts", Me.Filter) = 0 Then
      MsgBox "There are no records satisfying the filter conditions.", vbExclamation
      Me.Filter = ""
    End If
  End If
End Sub
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Get Filter By Form Data

Post by agibsonsw »

Thank you.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.