Results 1 to 2 of 2
  1. #1
    kfschaefer1 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2014
    Posts
    1

    Question do Loops - Not looping thru all records

    I have a function that is called from a query. I need to loop thru the same Contractnumber, but for 4 different Quarters to return different values for the function (4 records total). I seems to Loop thru the first 2 quarters but the return value does not change and the final results display the last result for all 4 quarters. See attachment.

    How do I get it to loop thru the correct number of quarters and to return the different values needed?



    Attachment 15783


    Code:
    Public Function BkOvrCalc(ByVal gContractID As String) As Long
    Dim curDB As DAO.Database
    Dim strSQL As String, strSQL1 As String
    Dim rs As DAO.Recordset, rs1 As DAO.Recordset
    Dim x As Integer
    Dim nOvrAmt As Currency
    Dim intUseTier As Integer
    Dim dblMultiplier As Double
    
    
       On Error GoTo BkOvrCalc_Error
    
    
    Set curDB = CurrentDb
    
    
        'List of all Contracts and Quarters Totals for calculation of Override Dollars by Tier%
        strSQL = "SELECT qrySummaryExpectation_Detail.ContractNumber, qrySummaryExpectation_Detail.Quarter," & _
                    " qrySummaryExpectation_Detail.ORType, TotalNetUSExp, PctYrlyIncrease," & _
                    " Tier1, Tier2, Tier3, Tier4, Tier5, Tier6" & _
                " FROM qrySummaryExpectation_Detail INNER JOIN qryBkOverRide_Normalized ON" & _
                    " (qrySummaryExpectation_Detail.Quarter = qryBkOverRide_Normalized.Quarter) AND" & _
                    " (qrySummaryExpectation_Detail.ContractNumber = qryBkOverRide_Normalized.ContractNumber)" & _
                " WHERE (((qrySummaryExpectation_Detail.ContractNumber)='00010674'))"
         Set rs = curDB.OpenRecordset(strSQL)
        
        'List of all Contracts and AccountPercentage, Account Dollars & Payout Percentage
        strSQL1 = "SELECT ContractNumber, ORType, T1E, T2E, T3E, T4E, T5E, T6E," & _
                    " AT1Per, AT2Per, AT3Per, AT4Per, AT5Per, AT6Per," & _
                    " AT1Dol, AT2Dol, AT3Dol, AT4Dol, AT5Dol, AT6Dol" & _
                " FROM tblContracts" & _
                " WHERE ContractNumber = " & Chr(34) & gContractID & Chr(34) & ""
        Set rs1 = curDB.OpenRecordset(strSQL1)
        
        rs.MoveFirst
        Do Until rs.EOF
            ' Override Code Type
            x = rs.Fields("ORType")
            Debug.Print rs!Quarter
            Debug.Print gContractID
            Select Case x ' OverRide Type
                Case 1 'Quarters
                    intUseTier = fnTier(rs!PctYrlyIncrease, rs!Tier1, rs!Tier2, rs!Tier3, rs!Tier4, rs!Tier5, rs!Tier6)
                    Debug.Print intUseTier
                    dblMultiplier = IIf(intUseTier = 0, 0, rs1.Fields("T" & intUseTier & "E"))
                    Debug.Print dblMultiplier
                    nOvrAmt = rs.Fields("TotalNetUSExp") * dblMultiplier
                Case 2 'Annual Flat%
                Case 3 'Annual Flat$
            End Select
            Debug.Print nOvrAmt
    
    
            rs.MoveNext
        Loop
            BkOvrCalc = nOvrAmt
    
    
    onExit:    rs.Close
        rs1.Close
        Set rs = Nothing
        Set rs1 = Nothing
    
    
       On Error GoTo 0
       Exit Function
    
    
    BkOvrCalc_Error:
    
    
     '  MsgBox "Error " & Err.Number & " (" & Err.Description & _
            ") in procedure BkOvrCalc of Module basUtilities"
    
    
    End Function
    
    Public Function fnTier(TestVal As Double, ParamArray TierValues() As Variant) As Integer
    
    
        Dim intLoop As Integer
        
        For intLoop = LBound(TierValues) To UBound(TierValues)
            If TestVal <= TierValues(intLoop) Then
                fnTier = intLoop
                Exit Function
            End If
        Next
        
        fnTier = intLoop
        
    End Function
    Attached Thumbnails Attached Thumbnails Capture1.PNG  

  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,921
    Your attachment link doesn't work. Do you want to provide db for analysis? Follow instructions at bottom of my post.
    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.

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

Similar Threads

  1. Replies: 9
    Last Post: 01-31-2014, 12:09 PM
  2. Looping through records in a continous form
    By accessnihon in forum Forms
    Replies: 3
    Last Post: 01-04-2012, 01:04 AM
  3. Loops
    By Monica2 in forum Programming
    Replies: 8
    Last Post: 09-14-2011, 09:16 AM
  4. Looping through Records in SQL
    By make me rain in forum Queries
    Replies: 13
    Last Post: 07-17-2011, 08:58 AM
  5. Loops
    By cksm4 in forum Programming
    Replies: 14
    Last Post: 10-10-2010, 02:10 PM

Tags for this Thread

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