Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access 2007 VBA - Open report with filtering

Posted on 2011-02-11
7
Medium Priority
?
1,211 Views
Last Modified: 2012-05-11
This one should be quick and easy...

I've got a tabular report bound to a query.  I need to open that report from a form where I select a projectID and a date range.  In a button click event on the form I am able to open it using this...

DoCmd.OpenReport "rptExpenditureReport", acViewReport

But how do I pass parameters to the report and how do I utilize those parameters as filters?

Thanks
0
Comment
Question by:JosephEricDavis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 10

Accepted Solution

by:
VTKegan earned 2000 total points
ID: 34873075
DoCmd.OpenReport "rptExpenditureReport", acViewReport,,"[ProjectID]=" & Me.ProjectID & " AND [DateRange]=#" & Me.DateRange & "#"
0
 
LVL 10

Assisted Solution

by:VTKegan
VTKegan earned 2000 total points
ID: 34873093
Here is a link to msdn on exactly how to do this:

http://msdn.microsoft.com/en-us/library/bb243764(v=office.12).aspx
0
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 34873133
I like to save the values to variables and construct a filtered recordset.  This allows for some error trapping.  Then save the filtered recordset to a query, which is the source of the filtered report.  Here is sample code:
[sample code fragment using the procedure]

   Dim dbs As DAO.Database
   Dim lngCount As Long
   Dim lngID As Long
   Dim rpt As Access.Report
   Dim rst As DAO.Recordset
   Dim strPrompt As String
   Dim strQuery As String
   Dim strRecordSource As String
   Dim strReport As String
   Dim strSQL As String
   Dim strTitle As String
   
   strRecordSource = "tblInventoryItemsComponents"
   strQuery = "qryTemp"
   Set dbs = CurrentDb

   'Numeric filter
   lngID = Nz(Me![ID])
   If lngID <> 0 Then
      strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
         & "[ID] = " & lngID & ";"
   End If

   'String filter
   strInventoryCode = Nz(Me![InventoryCode])
   If strInventoryCode <> "" Then
      strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
         & "[InventoryCode] = " & Chr$(39) & strInventoryCode & Chr$(39) & ";"
   End If

   'Date range filter from custom database properties
   dteFromDate = CDate(GetProperty("FromDate", ""))
   dteToDate = CDate(GetProperty("ToDate", ""))
   strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
      & "[dteDateReceived] Between " & Chr(35) & dteFromDate _
      & Chr(35) & " And " & Chr(35) & dteToDate & Chr(35) & ";"

   'Date range filter from controls
   If IsDate(Me![txtFromDate].Value) = True Then
      dteFromDate = CDate(Me![txtFromDate].Value)
   End If

   If IsDate(Me![txtToDate].Value) = True Then
      dteToDate = CDate(Me![txtToDate].Value)
   End If

   strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
      & "[dteDateReceived] Between " & Chr(35) & dteFromDate _
      & Chr(35) & " And " & Chr(35) & dteToDate & Chr(35) & ";"

   Debug.Print "SQL for " & strQuery & ": " & strSQL
   lngCount = CreateAndTestQuery(strQuery, strSQL)
   Debug.Print "No. of items found: " & lngCount
   If lngCount = 0 Then
      strPrompt = "No records found; canceling"
      strTitle = "Canceling"
      MsgBox strPrompt, vbOKOnly + vbCritical, strTitle
      GoTo ErrorHandlerExit
   Else
      'Use this line if you need a recordset
      Set rst = dbs.OpenRecordset(strQuery)
   End If

   'Use SQL string as the record source of a form
   strFormName = "fpriLoadSoldPackingSlip"
   DoCmd.OpenForm FormName:=strFormName, _
      view:=acDesign
   Set frm = Forms(strFormName)
   frm.RecordSource = strSQL
   DoCmd.OpenForm FormName:=strFormName, _
      view:=acNormal
   
   'Use SQL string as the record source of a report
   strReport = "rptLoadSold"
   DoCmd.OpenReport ReportName:=strReport, _
      view:=acViewDesign, _
      windowmode:=acHidden
   Set rpt = Reports(strReport)
   rpt.RecordSource = strSQL
   DoCmd.OpenReport ReportName:=strReport, _
      view:=acViewNormal, _
      windowmode:=acWindowNormal

=========================

Public Function CreateAndTestQuery(strTestQuery As String, _
   strTestSQL As String) As Long
'Created by Helen Feddema 28-Jul-2002
'Last modified 6-Dec-2009

On Error Resume Next
   
   Dim qdf As DAO.QueryDef
   Dim rst As DAO.Recordset
   
   'Delete old query
   Set dbs = CurrentDb
   dbs.QueryDefs.Delete strTestQuery

On Error GoTo ErrorHandler
   
   'Create new query
   Set qdf = dbs.CreateQueryDef(strTestQuery, strTestSQL)
   
   'Test whether there are any records
   Set rst = dbs.OpenRecordset(strTestQuery)
   With rst
      .MoveFirst
      .MoveLast
      CreateAndTestQuery = .RecordCount
   End With
   
ErrorHandlerExit:
   Exit Function

ErrorHandler:
   If Err.Number = 3021 Then
      CreateAndTestQuery = 0
      Resume ErrorHandlerExit
   Else
   MsgBox "Error No: " & Err.Number _
      & " in CreateAndTestQuery procedure; " _
      & "Description: " & Err.Description
   End If
   
End Function

Open in new window

0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 31

Expert Comment

by:Helen Feddema
ID: 34873142
Feeding values from controls directly to a report can be problematic, because of data conversion and/or Nulls or empty strings in the controls.
0
 
LVL 7

Author Comment

by:JosephEricDavis
ID: 34873192
I'm not quite sure exactly how the date range condition is being put together.  Can you give me an example of how the final conditional string is supposed to turn out if the date field I'm filtering on is called ExpenditureDate?

Thanks
0
 
LVL 10

Expert Comment

by:VTKegan
ID: 34873251
If the criteria is numeric then you need nothing around the criteria like:

"[ProjectID]=" & Me.ProjectID

If the criteria is a string then you need ' 's around the criteria like:

"[ProjectID]='" & Me.ProjectID & "'"

If the criteria is a date then you need #'s around the criteria like:

"[ExpenditureDate]=#" & Me.ExpenditureDate & "#"

So the final result would be (assuming ProjectID is numeric)

"[ProjectID]=" & Me.ProjectID & " AND [ExpenditureDate]=#" & Me.ExpenditureDate & "#"




Your question seems to ask about a date range which I initially missed.  So you have two dates selected on your form, and you want to return values where [ExpenditureDate] is between those two values?
0
 
LVL 10

Assisted Solution

by:VTKegan
VTKegan earned 2000 total points
ID: 34873273
For date range you would want to use

"[ExpenditureDate] BETWEEN #" & Me.StartDate & "# AND #" & Me.EndDate & "#"

So final result would be (assuming ProjectID is numeric)

DoCmd.OpenReport "rptExpenditureReport", acViewReport, ,"[ProjectID]=" & Me.ProjectID & " AND [ExpenditureDate] BETWEEN #" & Me.StartDate & "# AND #" & Me.EndDate & "#"
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question