Results 1 to 7 of 7
  1. #1
    craig1988 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2014
    Posts
    82

    generate multiple reports based on queries with changing criteria

    Hi all



    This is the finishing piece to a project I am currently working on.

    I have a table with 5 fields (tblMain); AccNo, AccDesc, SpecialPrint, ChequeDate, ChequeVolume

    The Cheque dates can vary from 1984 to current date and beyond (as time goes by).

    I need to generate a separate report for each year that is contained in the table. (it is generally only 5 or 6 but I will have to allow for every year from 1984 onwards). The report, I presume will be based on a SELECT query. I also presume I need some sort of a loop to iterate through the years?

    Can someone get me started?

    Regards
    Craig

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You can either print out a separate report for each year or have one report print with page break for each year. The first requires looping a recordset object in VBA, the second is accomplished with report design.

    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT Year([ChequeDate]) AS Yr FROM tablename;")
    rs.MovFirst
    While Not rs.EOF
    DoCmd.OpenReport "report name", , , "Year([ChequeDate])=" & rs!Yr
    DoCmd.Close acReport, "report name" , acSaveNo
    rs.MoveNext
    Wend
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    craig1988 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2014
    Posts
    82
    Quote Originally Posted by June7 View Post
    You can either print out a separate report for each year or have one report print with page break for each year. The first requires looping a recordset object in VBA, the second is accomplished with report design.

    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT Year([ChequeDate]) AS Yr FROM tablename;")
    rs.MovFirst
    While Not rs.EOF
    DoCmd.OpenReport "report name", , , "Year([ChequeDate])=" & rs!Yr
    DoCmd.Close acReport, "report name" , acSaveNo
    rs.MoveNext
    Wend
    I like this code. I am making progress however I am getting an error with the below piece of code


    "Year([ChequeDate])=" & rs!Yr

    Can you see an error in this??

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    No. What is the exact error message?

    You need to use your real table name in place of tablename.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    craig1988 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2014
    Posts
    82
    Hi June7

    Thanks for the continued support.

    This is the error I get!!

    Run-time error ‘3075’

    Syntax error (missing operator) in query expression ‘Year([ChequeDate])=’


    Here is my full code:


    Code:
    Private Sub Command52_Click()
        Dim rs As DAO.Recordset
        
        Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT Year([ChequeDate]) AS Yr FROM tblIBISSheets;")
        
        rs.MoveFirst
        While Not rs.EOF
        
        DoCmd.OpenReport "rptTest", acViewReport, , "Year([ChequeDate])=" & rs!Yr
        DoCmd.Close acReport, "report name", acSaveNo
        rs.MoveNext
        
        Wend
    End Sub

    I have it triggered from a test button for now, hence the "Command52_Click()". I have all tables, reports etc spelled correct.


    Thanks
    Craig

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I don't know why that fails. The syntax works for me.

    Step debug. Follow code as it executes. What value is rs!Yr holding? Refer to link at bottom of my post for debugging guidelines.

    About working with recordsets http://allenbrowne.com/ser-29.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    craig1988 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2014
    Posts
    82
    Hi June7

    It was a formatting issue. I should have spotted it. The code runs fine now. Thank you!!

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

Similar Threads

  1. Replies: 7
    Last Post: 09-27-2014, 09:11 PM
  2. Replies: 8
    Last Post: 08-14-2014, 06:00 AM
  3. Replies: 6
    Last Post: 02-25-2014, 07:44 PM
  4. Replies: 5
    Last Post: 08-24-2013, 02:07 AM
  5. Replies: 1
    Last Post: 07-02-2010, 03:55 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