Results 1 to 14 of 14
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195

    Print Each PostCode rs Loop

    Hi Guy's, i am unsure if I have got this correct, my first attempt returned msgbox after masgbox continually with 80 intQty for the 1st record when i know the intQty should have been x 2 records so had to force close by task manager



    I am looking for a msgbox to pop up to print each post code for the given shipment date, delto and postcode so once you click yes, the report opens (eventually change to viewnormal once working) then the msgbox displays the next record to print

    I think I may have the looping incorrect or the msgbox in the wrong stage of looping ?

    Once you click yes, I need the report to open, the report opens for Me.txtPC (PostCode) and Me.cboDeliveryPlanning (Del To) and Me.cboShipmentDateIndex1 (shipment date)

    Code:
     myDate = Me.cboShipmentDateIndex1    Set rs = CurrentDb.OpenRecordset("Select * from tbEdit WHERE ShipmentDate = #" & myDate & "# Order By DelTo;")
        If Not (rs.BOF And rs.EOF) Then
        rs.MoveFirst
        Do Until rs.EOF = True
        strPC = rs.Fields("PostCode")
        Me.txtPC = strPC
        strDelTo = rs.Fields("DelTo")
        Me.cboDeliveryPlanning = strDelTo
        intQty = DCount("DelTo", "tblEdit", "[PostCode] = '" & strPC & "'")
            If MsgBox("Print: " & strDelTo & " - " & strPC & " " & intQty & " Items", vbQuestion + vbYesNo, "PRINT NOW") = vbNo Then
            Exit Sub
            Else
            DoCmd.OpenReport "rptShipmentInNameOne", acViewPreview 'Changing acViewNormal Once Working
            End If
        rs.MoveNext
        Loop
        End If

  2. #2
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    I think I have spotted one mistake i made, please correct me, did not select date in the intQty line therefore looking at full history for that delto and postcode instead of specific ShipmentDate ?

    Would still rather you guy's confirm if the code will loop ok ?

    Code:
    intQty = DCount("DelTo", "tblEdit", "[PostCode] = '" & strPC & "' And [ShipmentDate] = #" & myDate & "#")

  3. #3
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    I would use strPC as there can be 6 DelTo Names with various branches so postcode is most unique

  4. #4
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Hi Guy's changing the intQty criteria to ShipmentDate Works fine, it's not moving to the next Delto (Postcode) ??

  5. #5
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    OK, the report that opens is opening twice, once each click

    click yes
    aa opens
    click yes
    aa opens again
    click yes
    ab opens
    click yes
    ab opens

    So it's opening the report again before moving to next record, where am i wrong on this one please

    thank you very much

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,007
    Use Debug.Print instead of MSGBOX, much easier to debug with.
    Indenting correctly also helps with debugging.
    Code:
    myDate = Me.cboShipmentDateIndex1
    Set rs = CurrentDb.OpenRecordset("Select * from tbEdit WHERE ShipmentDate = #" & myDate & "# Order By DelTo;")
    If Not (rs.BOF And rs.EOF) Then
        rs.MoveFirst
        Do Until rs.EOF = True
            strPC = rs.Fields("PostCode")
            Me.txtPC = strPC
            strDelTo = rs.Fields("DelTo")
            Me.cboDeliveryPlanning = strDelTo
            intQty = DCount("DelTo", "tblEdit", "[PostCode] = '" & strPC & "'")
            If MsgBox("Print: " & strDelTo & " - " & strPC & " " & intQty & " Items", vbQuestion + vbYesNo, "PRINT NOW") = vbNo Then
                Exit Sub
            Else
                DoCmd.OpenReport "rptShipmentInNameOne", acViewPreview    'Changing acViewNormal Once Working
            End If
            rs.MoveNext
        Loop
    End If
    You have tbEdit and tblEdit?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    msgbox after masgbox continually with 80 intQty for the 1st record when i know the intQty should have been x 2 records
    Your loop is governed by the number of records in the recordset, not some field value, so I don't get that statement.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Hi guy's yes i realized i had a typo then corrected the tbEdit, to tblEdit

    This opens up the report and displays the same record on 2nd click then it moves to next then same again, it will open that same name (postcode) twice

    Full code pasted

    Will try debug.print as per WGM suggestion

    do you need me to send record source properties of the report ?
    Code:
    Dim rs As DAO.RecordsetDim strDelTo As String, strPC as String
    Dim myDate As Date
    Dim intQty As Integer
    
    
    If Me.cboPrint = "Print One" Then
        myDate = Me.cboShipmentDateIndex1
        Set rs = CurrentDb.OpenRecordset("Select * from tblEdit WHERE ShipmentDate = #" & myDate & "# Order By DelTo;")
        If Not (rs.BOF And rs.EOF) Then
        rs.MoveFirst
        Do Until rs.EOF = True
        strPC = rs.Fields("PostCode")
        Me.txtPC = strPC
        strDelTo = rs.Fields("DelTo")
        Me.cboDeliveryPlanning = strDelTo
        intQty = DCount("DelTo", "tblEdit", "[PostCode] = '" & strPC & "' And [ShipmentDate] = #" & myDate & "#")
            If MsgBox("Print: " & strDelTo & " - " & strPC & " " & intQty & " Items", vbQuestion + vbYesNo, "PRINT NOW") = vbNo Then
            Exit Sub
            Else
            DoCmd.OpenReport "rptShipmentInNameOne", acViewPreview 'Changing acViewNormal Once Working
            End If
        rs.MoveNext
        Loop
        End If
    End If

  10. #10
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Tried debug.print, just opens the report, doesn't loop through next name (postcode) in line


    Code:
    If Me.cboPrint = "Print One" Then    myDate = Me.cboShipmentDateIndex1
        Set rs = CurrentDb.OpenRecordset("Select * from tblEdit WHERE ShipmentDate = #" & myDate & "# Order By DelTo;")
        If Not (rs.BOF And rs.EOF) Then
        rs.MoveFirst
        Do Until rs.EOF = True
        strPC = rs.Fields("PostCode")
        Me.txtPC = strPC
        strDelTo = rs.Fields("DelTo")
        Me.cboDeliveryPlanning = strDelTo
        intQty = DCount("DelTo", "tblEdit", "[PostCode] = '" & strPC & "' And [ShipmentDate] = #" & myDate & "#")
           Debug.Print
           ' If MsgBox("Print: " & strDelTo & " - " & strPC & " " & intQty & " Items", vbQuestion + vbYesNo, "PRINT NOW") = vbNo Then
          '  Exit Sub
           ' Else
            DoCmd.OpenReport "rptShipmentInNameOne", acViewPreview 'Changing acViewNormal Once Working
            'End If
        rs.MoveNext
        Loop
        End If
    End If

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,007
    Just what is the report supposed to show, data for each postcode?
    If so, you are not filtering the report with anything?

    Comment out the report line and add your fields/variables to the debug.print
    I expect you will see what you want then?
    Last edited by Welshgasman; 02-27-2022 at 01:34 PM.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,959
    DoCmd.OpenReport "rptShipmentInNameOne", acViewPreview, , "PostCode='" & rs!PostCode & "'"

    Or design report using Sorting & Grouping and force each postcode to a new page. Then just print one report filtered to shipment date and/or other parameters. No recordset looping.
    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.

  14. #14
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Thank you all, i have taken some of your suggestions and adapted to work, I have managed to generate space on a different tab control which i didn't have so now populating a list and using a combo to select Auto Print;View Print so the user just keeps clicking down the list whilst Auto print is selected, we have managed to save some time by this, similarly my initial thought was just keep clicking yes on the MsgBox but finding the space to add a list has saved about 75% of time.

    Thanks again

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

Similar Threads

  1. Print PDF Loop due to Error 3014
    By forbudt4u in forum Macros
    Replies: 16
    Last Post: 02-10-2021, 06:33 PM
  2. Report print loop
    By xjps in forum Access
    Replies: 20
    Last Post: 02-05-2020, 08:31 PM
  3. Print in a loop
    By LonghronJ in forum Modules
    Replies: 4
    Last Post: 02-03-2016, 07:44 AM
  4. Replies: 1
    Last Post: 08-04-2015, 03:54 AM
  5. Replies: 2
    Last Post: 10-14-2013, 05:09 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