Results 1 to 4 of 4
  1. #1
    redwards465 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    1

    Post Basic question, while loops and defining strings


    Hi guys, been searching all over the net and simply cannot work out how to do this. I have a while loop that I am running in order to open/print reports based on a filter.

    My loop looks through a query I've set up using a Recordset and is supposed to take the values and use them as a report filter so that each unique value opens/prints its own report.

    Here is the snippet where I am running into problems:

    Code:
                Dim ProjectCode As String
      
                ProjectCode = rs.Fields("PROJECTPHASE")
                'takes relevant query field data to generate report for each desired PROJECTPHASE. ProjectCode is set up to change the PROJECTPHASE for each loop (so reports are opened for each unique PROJECTPHASE)
    
    
                DoCmd.OpenReport "MyReport", acViewReport, "", "[Staff_w_query2]![PROJECTPHASE]='ProjectCode'", acNormal
                'filter the report by the correct PROJECTPHASE as developed above
    
                
                .MoveNext
                'move onto next ProjectCode
    The above syntax doesn't actually assign the new ProjectCode for each loop.

    Would be extremely grateful if anyone could point me in the right direction - seems like a simple fix but I'm new to all of this.

    Thanks!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    This will work with forms and list box.
    on a form put a listbox of the items to report on. (the listbox query shows all phases)
    then the report looks at this listbox and only reports on the one selected.
    Code:
    sub btnPrintRpts_click()
    dim vPhase
    dim i as integer
    for i = 0 to lstBox.listcount -1
      vPhase = lstBox.itemdata(i)    'get next item in listbox
      lstBox = vPhase        'set the listbox to this item
    
          'get vitals off the listbox
      docmd.OpenReport "myReport",acViewPreview,,"[phase]='" & lstBox & "'"
    next
    end sub

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    That would work if you were printing. The second time through the loop, the report is already open; you can't open it again.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    In the record source for the report, are there two fields with the name "PROJECTPHASE"? Why do you have "Staff_w_query2" in the WhereCondition clause?

    You need to concatenate the value of the string variable "ProjectCode" to the field for the WhereCondition.
    And since "ProjectCode" is declared as a string, you need delimiters.

    Something like
    Code:
    DoCmd.OpenReport "MyReport", acViewReport, , "PROJECTPHASE = '" & ProjectCode & "'", acNormal

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

Similar Threads

  1. Basic Question
    By alimbagirl in forum Access
    Replies: 5
    Last Post: 05-01-2017, 12:35 PM
  2. Easy question about strings
    By gpeirce in forum Programming
    Replies: 6
    Last Post: 11-19-2016, 07:30 PM
  3. Basic Question
    By poppet in forum Access
    Replies: 9
    Last Post: 07-24-2014, 05:41 AM
  4. Basic Question
    By za20001 in forum Queries
    Replies: 2
    Last Post: 04-11-2011, 03:59 PM
  5. Really basic question
    By jimlaw in forum Forms
    Replies: 1
    Last Post: 07-27-2009, 07:20 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