Results 1 to 3 of 3
  1. #1
    libraccess's Avatar
    libraccess is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Location
    Napier New Zealand
    Posts
    129

    Steer me towards Modules

    Not sure if this is the right forum to go in
    But the following code is from Allen Browne and is one I find really useful not the least because it solves the problem of us people on the dark side of the Earth use Dates in funny formats.
    Anyway the code lets you via a form choose start end and text and open a report to suit
    Private Sub cmdPreview_Click()
    'On Error GoTo Err_Handler 'Remove the single quote from start of this line once you have it working.
    'Purpose: Filter a report to a date range.
    'Documentation: http://allenbrowne.com/casu-08.html
    'Note: Filter uses "less than the next day" in case the field has a time component.
    Dim strReport As String
    Dim strDateField As String
    Dim strWhere As String
    Dim lngView As Long
    Const strcJetDate = "\#mm\/dd\/yyyy\#" 'Do NOT change it to match your local settings.

    'DO set the values in the next 3 lines.
    strReport = "rptSales" 'Put your report name in these quotes.
    strDateField = "[SaleDate]" 'Put your field name in the square brackets in these quotes.
    lngView = acViewPreview 'Use acViewNormal to print instead of preview.

    'Build the filter string.
    If IsDate(Me.txtStartDate) Then
    strWhere = "(" & strDateField & " >= " & Format(Me.txtStartDate, strcJetDate) & ")"
    End If
    If IsDate(Me.txtEndDate) Then
    If strWhere <> vbNullString Then
    strWhere = strWhere & " AND "
    End If
    strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")"
    End If

    'Close the report if already open: otherwise it won't filter properly.


    If CurrentProject.AllReports(strReport).IsLoaded Then
    DoCmd.Close acReport, strReport
    End If

    'Open the report.
    'Debug.Print strWhere 'Remove the single quote from the start of this line for debugging purposes.
    DoCmd.OpenReport strReport, lngView, , strWhere

    Exit_Handler:
    Exit Sub

    Err_Handler:
    If Err.Number <> 2501 Then
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Cannot open report"
    End If
    Resume Exit_Handler
    End Sub

    So I have a Report Menu Form that has the start & end dates and txt so far I can call up 7 reports - and quite a few more to come
    So each has a OnClick Button and runs the code above with the appropriate "rptName" inserted and
    the rest of the code is identical
    Question : Can this code be put into a module and the On Click event set up the report Name and (date(s) & txt and then call the module
    or does it matter if I have the same(ish) code repeated 20 times (looks clunky) I'm all for doing things a better way
    Thanks

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You could certainly put that in a function in a standard module fairly easily and pass the report name to it. The tricky part would be if the reports took different criteria or had different field names in the criteria (like SaleDate). You could use optional input parameters and test them, or include the field names as parameters.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    libraccess's Avatar
    libraccess is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Location
    Napier New Zealand
    Posts
    129
    Can you point me to some good reading or example that would be similar to this as I've been trawling away to no end
    Thanks

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

Similar Threads

  1. Modules using Crosstabs
    By OTSeraph in forum Access
    Replies: 1
    Last Post: 02-10-2012, 10:50 AM
  2. What is the point of different modules?
    By cowboy in forum Programming
    Replies: 3
    Last Post: 03-29-2010, 10:43 PM
  3. Update Access Modules
    By Othello911 in forum Access
    Replies: 0
    Last Post: 01-06-2009, 08:39 AM
  4. update access modules
    By wallen in forum Programming
    Replies: 0
    Last Post: 01-06-2009, 08:22 AM
  5. How to use Modules
    By wasim_sono in forum Access
    Replies: 0
    Last Post: 01-16-2007, 06:29 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