Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727

    nest loops


    All; using access 2010. I have a snippet of my code below:

    Code:
    Set rstCust = CurrentDb().OpenRecordset("qryCust_email")
                        With rstCust
                            ' Get the total e-mail count.
                            .MoveLast
                            intNumEmailsToCreate = Nz(.RecordCount, 0)
                            .MoveFirst
     
                            ' Loop through the Cust- creating an e-mail (with attached reports) for each cust.
                            Do While Not .EOF
    I need to insert another loop using the same recordset to loop through not only cust but state also. I haven't used nested loops in a while and don't want to waste time not afforded to me. Can anyone steer me in the right direction pls. thanks in advance.

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Not sure what you are asking. Yes, nested loops...... but not enough info what you want to do.

    Best I can say is you can't use the same recordset because the current record would change. Open another recordset.
    Code:
    Set rstCustState = CurrentDb().OpenRecordset("qryCust_email")   ' RS for state
    
    Set rstCust = CurrentDb().OpenRecordset("qryCust_email")     'RS for customer
    
    
                If rstCust .BOF and rstCust .EOF Then      'can't movelast if no records
                     Msgbox "No Records"
                        Exit Sub
                Else
                        With rstCust
                            ' Get the total e-mail count.
                            .MoveLast
                            intNumEmailsToCreate = Nz(.RecordCount, 0)
                            .MoveFirst
     
                            ' Loop through the Cust- creating an e-mail (with attached reports) for each cust.
                            Do While Not .EOF
    
                                     Do While Not rstCustState.EOF

  3. #3
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Thanks for the reply. I will try this and post back.

  4. #4
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    The data is not showing up in the reports. Here's the snippet code with the modifications you suggested:

    Code:
    Private Sub cmdOK_Click()
    On Error GoTo Err_cmdOK_Click
    Dim rstBrokers As DAO.Recordset
    Dim rstState As DAO.Recordset
    Set rstCust = CurrentDb().OpenRecordset("qryCust_email")
    Set rstCustState = CurrentDb().OpenRecordset("qryCust_email")
                        If rstCust.BOF and rstCust.EOF then
                                    Msgbox “No Records”
                                    Exit Sub
                    Else                   
                           With rstCust
                            ' Get the total e-mail count.
                                    .MoveLast
                            intNumEmailsToCreate = Nz(.RecordCount, 0)
                            .MoveFirst
                            ' Loop through the  Cust- creating an e-mail (with attached reports) for each cust.
                            Do While Not .EOF                      
                            Do While Not rstCustState.EOF
                             ' Create the e-mail.
                                If (Not g_blnTestMode) Or (intNumEmailsCreated <= 2) Then
                                    ' Make Cust name filename-ready.
                                    strScrubbedCustName = Replace(!Cust_Name, ".", "")
                                    strState = Replace(!state_abbr, ".", "")
                                    strCustID = Replace(!CustID, ".", "")
                                   
                                    ' Set current Cust (used by the reports' underlying queries).
                                    g_varCurrentCustID = !CustID & "" & !state_abbr
    '                                & "" & !state_abbr
    '                                & " " & g_varCurrentCustID
                                                                    
    '                                Debug.Assert False
                                     strFldValue = !letter_code
                                     If strFldValue = "NEW" Then
                                        ' Create the report #1.
                                    strPathAndFilename_Report1 = strPathToStatementFiles & " " & strCustID & " " & strScrubbedCustName & " " & strState & " " & varCheckDate & ".pdf"
                                    DoCmd.OutputTo acOutputReport, "rptLetterNEW_email", acFormatPDF, strPathAndFilename_Report1, False
                                    DoEvents     ' Allow this operation to be fully completed before proceeding.
                                    Else
                                  ' Create the report #2.
                                    strPathAndFilename_Report2 = strPathToStatementFiles & " " & strClient & " " & strScrubbedCustName & " " & strState & " " & varCheckIssueDate & ".pdf"
                                    DoCmd.OutputTo acOutputReport, "rptLetterTRUEUP_email", acFormatPDF, strPathAndFilename_Report2, False
                                    DoEvents     ' Allow this operation to be fully completed before proceeding.
                                    End If
                                 rstCusts.MoveNext
                            Loop
                                rstState.MoveNext
                            Loop
                            ' Close the recordset.
                            rstCusts.Close
                            rstState.Close
                        End With
                        Set rstCusts = Nothing
                        Set rstState = Nothing
            End If
        End If
    Exit_cmdOK_Click:
        Exit Sub
    Err_cmdOK_Click:
        Application.Echo True
        g_TrappedErrorMsg "cmdOK_Click", Err.Description, Err.Number
        Resume Exit_cmdOK_Click
    End Sub
    what am I missing??
    Thanks

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The following two lines should be at the top of every code page
    Code:
    Option Compare Database   ' <<= Should be at the top of every module
    Option Explicit           ' <<= Should be at the top of every module
    "Option Explicit" requires that every variable be explicitly declared (the Dim statement).

    In the IDE, click on TOOLS/Options. Click on the "Editor" tab. The 2nd check box, "Require Variable Declaration" should be checked. Now any NEW modules will have this added automatically. Any existing modules must have "Option Explicit" added manually.

    -----------------------------------

    You have declared "rstBrokers" but never used the record set.
    You have declared "rstCust" (singular), but in the code you have "rstCusts" (plural).

    You have declared "rstCustState", but in code you switched to "rstCusts", which is not declared.

    ----------------------------------

    You are missing an "End If" statement for the line "If (Not g_blnTestMode) Or (intNumEmailsCreated <= 2) Then"

    ----------------------------------

    You have two loops: the outer loop is "Do While Not .EOF" (rstCust) and the inner loop "Do While Not rstCustState.EOF". But you have the "MoveNext" statements crossed: (disreguard the misnamed recordsets)
    Code:
            With rstCust
    
                ' Loop through the  Cust- creating an e-mail (with attached reports) for each cust.
                Do While Not .EOF   ' (rstCust)
                    Do While Not rstCustState.EOF
    
                            rstCusts.MoveNext
                        Loop
                        rstState.MoveNext
                    Loop
    
                    ' Close the recordset.
                    rstCusts.Close
                    rstState.Close
                End With
    Should be: (disreguard the misnamed recordsets)
    Code:
            With rstCust
    
                ' Loop through the  Cust- creating an e-mail (with attached reports) for each cust.
              -  Do While Not .EOF   ' (rstCust)
              |
              |      - Do While Not rstCustState.EOF
              |     |
              |      -    rstState.MoveNext
              |          Loop
              -           rstCusts.MoveNext
                    Loop
    
                    ' Close the recordset.
                    rstCusts.Close
                    rstState.Close
                End With
    ----------------------------------


    In this part of the code:
    Code:
        If rstCust.BOF And rstCust.EOF Then
            MsgBox "No Records"
            Exit Sub  
        Else
    The Msgbox text has the wrong type of opening and closing quotes.
    Delete or comment out the "Exit Sub" line because it is not needed.
    The If() (above) will fall through to the label "Exit_cmdOK_Click:".
    The recordset close statements and "Set .... Nothing" are in the wrong place. They should be just above the "Exit Sub" statement.
    Code:
    Exit_cmdOK_Click:
        ' Close the recordset.
        rstCusts.Close
        rstState.Close
        Set rstCusts = Nothing
        Set rstState = Nothing
        Exit Sub
    ----------------------------------

    My personal preference... I never use spaces when creating file names. Causes too many headaches.
    Instead of this:
    Code:
    strPathAndFilename_Report1 = strPathToStatementFiles & " " & strCustID & " " & strScrubbedCustName & " " & strState & " " & varCheckDate & ".pdf"
    I use the underscore to seperate parts of the file name:
    Code:
    strPathAndFilename_Report1 = strPathToStatementFiles & "_" & strCustID & "_" & strScrubbedCustName & "_" & strState & "_" & varCheckDate & ".pdf"
    ----------------------------------

    Finally, it looks like there are approx 12 variables that have not been declared.

    -----------------------------------


    My apologies if this seems harsh. But you did ask
    what am I missing??

  6. #6
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Not a problem Steve. I asked for help. This is only a snippet of the code. I have declared the variables I will look and make corrections and post back. thanks for your help!

  7. #7
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    I made some adjustments and attached the following snippet:

    Code:
    Private Sub cmdOK_Click()
    On Error GoTo Err_cmdOK_Click
    Dim rstCust As DAO.Recordset
    Dim rstState As DAO.Recordset
    Set rstCust = CurrentDb().OpenRecordset("qryCust_email")
    Set rstState = CurrentDb().OpenRecordset("Select  State_abbr  from qryState_Email where  CustID =” & rstCust!CustID")
                       
                        If rstCust.BOF And rstCust.EOF Then
                            MsgBox "No Records"
                            Else
                        With rstCust
                            ' Get the total e-mail count.
                            .MoveLast
                            intNumEmailsToCreate = Nz(.RecordCount, 0)
                            .MoveFirst
                      ' Loop through the Cust - creating an e-mail (with attached reports) for each Cust.
                            Do While Not rstCust.EOF
                            Do While Not rstState.EOF
                                ' Create the e-mail.
                                If (Not g_blnTestMode) Or (intNumEmailsCreated <= 2) Then
                                    ' Make Cust name filename-ready.
                                    strScrubbedCustName = Replace(!Bus_Name, ".", "")
                                    strState = Replace(!state_abbr, ".", "")
                                    strCust = Replace(!CustID, ".", "")
                                   
                                    ' Set current CustID (used by the reports' underlying queries).
                                    g_varCurrentCustID = !CustID
                                    strFldValue = !letter_code
                                    If strFldValue = "NEW" Then
       
                                    ' Create the report #1.
                                    strPathAndFilename_Report1 = strPathToStatementFiles & " " & strCust & " " & strScrubbedCustName & " " & strState & " " & varCheckIssueDate & ".pdf"
                                    DoCmd.OutputTo acOutputReport, "rptLetterNEW_email", acFormatPDF, strPathAndFilename_Report1, False
                                    DoEvents     ' Allow this operation to be fully completed before proceeding.
                                   Else
                                           ' Create the report #2.
                                    strPathAndFilename_Report2 = strPathToStatementFiles & " " & strCust & " " & strScrubbedCustName & " " & strState & " " & varCheckIssueDate & ".pdf"
                                    DoCmd.OutputTo acOutputReport, "rptLetter_email", acFormatPDF, strPathAndFilename_Report2, False
                                    DoEvents     ' Allow this operation to be fully completed before proceeding.
                                    End If
                                    ' Build the list of attachments.
    '                                varSemicolonSeparatedListOfAttachments = strPathAndFilename_Report1 & "; " & strPathAndFilename_Report2
                                   
                                    varSemicolonSeparatedListOfAttachmentsNEW = strPathAndFilename_Report1
                                  
                                    varSemicolonSeparatedListOfAttachmentsT = strPathAndFilename_Report2
                                rstState.MoveNext
                            Loop
                                rstCust.MoveNext
                            Loop
                            ' Close the recordset.
                            rstCust.Close
                            rstState.Close
                        End With
                       
                        Set rstCust = Nothing
                        Set rstState = Nothing
        End If
        End If
      
    Exit_cmdOK_Click:
        Exit Sub
     
    Err_cmdOK_Click:
        Application.Echo True
        g_TrappedErrorMsg "cmdOK_Click", Err.Description, Err.Number
        Resume Exit_cmdOK_Click
    End Sub
    
    
    This is still not working. I've made sure I declared all variables and correct the names. I have tried to use a different recordset for the state but still not working. I am still getting multiple reports for the same custid in the same pdfs not separate.
    Help please
    this is driving me crazy that I can't get this correct.

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    What data type is "CustID"? Text or Long Integer?

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Open filtered report and output that. https://www.accessforums.net/modules...ode-49802.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.

  10. #10
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Both CustID and State_abbr are text datatypes.

  11. #11
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    So; are you saying I need to use a filter instead of a second loop?? I need to get a report for custid state_abbr combination.

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    I am saying you need to loop through a recordset of customer email addresses and within that loop, open report appropriately filtered for desired records, and transmit that.
    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.

  13. #13
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Ok Forgive me; But I don't know how to make this code work. I've been at it too long and I am at a total loss on how to fix it. I did study the code in the link but don't know how to apply it to my code and fix it. I really appreciate the help.

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    What process do you want to code? Something like:

    1. open recordset of unique customer ID and email addresses

    2. begin loop of recordset

    3. open report 1 filtered for customer records

    4. output report 1 to PDF

    5. close report 1

    6. open report 2 filtered for customer records

    7. output report 2 to PDF

    8. close report 2

    9. construct email elements and attach the two PDF files

    10. send email

    11. move to next customer record

    12. repeat loop

    13. close recordset and end procedure
    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.

  15. #15
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Somehow you had double apostrophes instead of double quotes at the end of the "Set rstState..." line.
    Also in that line, "CustID" was improperly delimited.
    Still was missing an "End If". There must be the same number of "End If"s as "If" statements.
    The open recordset for State_abbr must be inside the CustID loop

    Code:
    Private Sub cmdOK_Click()
        On Error GoTo Err_cmdOK_Click
    
        Dim rstCust As DAO.Recordset
        Dim rstState As DAO.Recordset
    
        Set rstCust = CurrentDb().OpenRecordset("qryCust_email")
    
        If rstCust.BOF And rstCust.EOF Then
            MsgBox "No Records"
        Else
            With rstCust
                ' Get the total e-mail count.
                .MoveLast
                intNumEmailsToCreate = Nz(.RecordCount, 0)
                .MoveFirst
                ' Loop through the Cust RS - creating an e-mail (with attached reports) for each Cust.
                Do While Not rstCust.EOF
    
                   ' moved inside CustID loop
                    ' get the state for the current CustID
                    Set rstState = CurrentDb().OpenRecordset("SELECT  State_abbr FROM qryState_Email WHERE  CustID ='" & rstCust!CustID & "'")
    
                    ' Loop through the State_abbr RS
                    Do While Not rstState.EOF
                        ' Create the e-mail.
                        If (Not g_blnTestMode) Or (intNumEmailsCreated <= 2) Then
                            ' Make Cust name filename-ready.
                            strScrubbedCustName = Replace(!Bus_Name, ".", "")
                            strState = Replace(!state_abbr, ".", "")
                            strCust = Replace(!CustID, ".", "")
    
                            ' Set current CustID (used by the reports' underlying queries).
                            g_varCurrentCustID = !CustID
                            strFldValue = !letter_code
                            If strFldValue = "NEW" Then
    
                                ' Create the report #1.
                                strPathAndFilename_Report1 = strPathToStatementFiles & " " & strCust & " " & strScrubbedCustName & " " & strState & " " & varCheckIssueDate & ".pdf"
                                DoCmd.OutputTo acOutputReport, "rptLetterNEW_email", acFormatPDF, strPathAndFilename_Report1, False
                                DoEvents     ' Allow this operation to be fully completed before proceeding.
                            Else
                                ' Create the report #2.
                                strPathAndFilename_Report2 = strPathToStatementFiles & " " & strCust & " " & strScrubbedCustName & " " & strState & " " & varCheckIssueDate & ".pdf"
                                DoCmd.OutputTo acOutputReport, "rptLetter_email", acFormatPDF, strPathAndFilename_Report2, False
                                DoEvents     ' Allow this operation to be fully completed before proceeding.
                            End If
                            ' Build the list of attachments.
                            '                                varSemicolonSeparatedListOfAttachments = strPathAndFilename_Report1 & "; " & strPathAndFilename_Report2
    
                            varSemicolonSeparatedListOfAttachmentsNEW = strPathAndFilename_Report1
    
                            varSemicolonSeparatedListOfAttachmentsT = strPathAndFilename_Report2
    
    
                            rstState.MoveNext    ' move to the next State_abbr record
                        End If
                    Loop    ' State_abbr
    
                    rstState.Close    ' Close the rstStaterecordset.
    
                    rstCust.MoveNext    ' move to the next CustID record
                Loop  ' CustID
            End With    ' CustID
        End If
    
    Exit_cmdOK_Click:
        On Error Resume Next
    
        'clean up
        rstCust.Close
        rstState.Close
        Set rstCust = Nothing
        Set rstState = Nothing
    
        Exit Sub
    
    Err_cmdOK_Click:
        Application.Echo True
        g_TrappedErrorMsg "cmdOK_Click", Err.Description, Err.Number
        Resume Exit_cmdOK_Click
    
    End Sub

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Is there a way to nest these?
    By GinaFlan in forum Forms
    Replies: 3
    Last Post: 10-07-2014, 12:11 PM
  2. Nest query
    By seanwd in forum Queries
    Replies: 0
    Last Post: 06-18-2014, 02:14 PM
  3. Replies: 2
    Last Post: 07-12-2013, 06:55 AM
  4. Loops
    By Monica2 in forum Programming
    Replies: 8
    Last Post: 09-14-2011, 09:16 AM
  5. Help with Loops
    By rotorque in forum Access
    Replies: 0
    Last Post: 01-07-2009, 05:06 PM

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