Results 1 to 3 of 3
  1. #1
    tsamrein is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Sep 2017
    Posts
    6

    Question Command button to filter report and print

    I have a command button that prints a preview of a report. is there anyway to make this also prompt the user for a Work Order number and use that to filter the report? This is the command button functions.

    Private Sub cmdPrintPurchaseRequirements_Filtered_Click()
    DoCmd.OpenReport "rptPurchaseRequirements_Selected_3_M", acViewPreview
    ' DoCmd.OpenReport "rptPurchaseRequirements_Selected_3_M1", acViewPreview
    ' DoCmd.OpenReport "rptPurchaseRequirements_Selected_3_LMOB", acViewPreview
    End Sub

    I am thinking I will need to create a new report and run a similar command from button, but a colleague of mine has different thoughts.

    Thanks in advance.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    if cboWO is a combo box,

    Code:
    '----------------
    sub btnReport_click()
    '----------------
    
    If IsNUll(cboWO) Then
      docmd.OPenReport "rMyReport",acViewPreview
    Else
      docmd.OPenReport "rMyReport",acViewPreview,,[WorkOrder]='" & cboWO & "'"
    End If
    end sub

  3. #3
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Don't replicate forms/reports for similar end uses; it's not good design practice. If you want this prompt every time the button is clicked, the simplest solution is to make the work order field a parameter field in the query for the report by surrounding a prompt in square brackets. You place this in the criteria field; e.g. [Work Order Number]. Some people don't advocate the use parameters in queries for whatever reason; I think it's just personal preference. The lack of a value after being prompted may result in unsatisfactory results, but it's quite a simple operation to copy your existing query, parametize it and see how it performs either way. You can also filter the report using the DoCmd.OpenReport filter parameter if you have a form control that contains the work order number.

    Alternatively, you can provide a form textbox or combo for the value input and test if it's null or not. You'd have to execute one way if it's null and another if it's not. Depending on your preference, in vba you can
    - build a valid sql statement without the WHERE part, and concatenate the WHERE part based on the existence of a value
    - or run either of 2 queries (but then you're duplicating objects unnecessarily)
    - or create a query definition on the fly but not store it in the Nav pane (probably more complicated than what you need)
    - or create DAO parameters - definitely more complicated than what you need.
    The point of the above being that as with most situations, there's more than one approach.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 06-27-2014, 08:02 AM
  2. Print report command button
    By Stika in forum Reports
    Replies: 5
    Last Post: 12-21-2012, 05:56 PM
  3. Replies: 7
    Last Post: 09-04-2012, 02:51 PM
  4. Replies: 2
    Last Post: 04-02-2012, 04:34 AM
  5. Replies: 5
    Last Post: 08-22-2011, 11:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums