Solved

condense 3 loops into 1

Posted on 2014-09-26
3
81 Views
Last Modified: 2014-10-06
This code has the same iterations 3 times.  How can I keep only one loop and simply modify only the 3 variables in bold.  I don't see a pattern for the ages that would work as I was thinking of putting a loop within a loop.

'Get List of 8 to 12 and under--------
       esql = "DECLARE @fromdt AS datetime " & _
                "DECLARE @todt AS datetime " & _
                "SET @fromdt = '" & DTPicker1 & "' " & _
                "SET @todt = '" & DTPicker2 & "' " & _
                "SELECT  p.Agency, count(distinct r.RegID) " & _
                "FROM tblOrgProfile as p " & _
                "LEFT JOIN tblOrgRegistrations as r " & _
                    "ON p.AgencyID = r.AgencyID " & _
                    "AND r.AgeRegistration >= [b]8 [/b]" & _
                    "AND r.AgeRegistration <= [b]12 [/b]" & _
                "LEFT JOIN tblOrgHours as h " & _
                    "ON h.RegID = r.regid " & _
                    "AND h.ActivityDate >= @fromdt " & _
                    "AND h.ActivityDate <= @todt " & _
                "GROUP BY p.Agency " & _
                "ORDER BY p.Agency"
                
       rec1.Open (esql), conn, adOpenDynamic, adLockOptimistic
       
        i = 6
        
        Do Until rec1.EOF = True
                        
                ApExcel.Workbooks("CYSReport.xls").Sheets("Direct Service").Cells(i, [b]12[/b]).Formula = rec1.Fields(1)
                
                i = i + 2
                rec1.MoveNext

        
        Loop
                
        rec1.Close
            
       
'Get List of 13 to 17 and under--------
       esql = "DECLARE @fromdt AS datetime " & _
                "DECLARE @todt AS datetime " & _
                "SET @fromdt = '" & DTPicker1 & "' " & _
                "SET @todt = '" & DTPicker2 & "' " & _
                "SELECT  p.Agency, count(distinct r.RegID) " & _
                "FROM tblOrgProfile as p " & _
                "LEFT JOIN tblOrgRegistrations as r " & _
                    "ON p.AgencyID = r.AgencyID " & _
                    "AND r.AgeRegistration >= [b]13 [/b]" & _
                    "AND r.AgeRegistration <= [b]17 [/b]" & _
                "LEFT JOIN tblOrgHours as h " & _
                    "ON h.RegID = r.regid " & _
                    "AND h.ActivityDate >= @fromdt " & _
                    "AND h.ActivityDate <= @todt " & _
                "GROUP BY p.Agency " & _
                "ORDER BY p.Agency"
                
       rec1.Open (esql), conn, adOpenDynamic, adLockOptimistic
       
        i = 6
        
        Do Until rec1.EOF = True
                        
                ApExcel.Workbooks("CYSReport.xls").Sheets("Direct Service").Cells(i, [b]13[/b]).Formula = rec1.Fields(1)
                
                i = i + 2
                rec1.MoveNext

        
        Loop
                
        rec1.Close
        

'Get List of 18 to 20 and under--------
       esql = "DECLARE @fromdt AS datetime " & _
                "DECLARE @todt AS datetime " & _
                "SET @fromdt = '" & DTPicker1 & "' " & _
                "SET @todt = '" & DTPicker2 & "' " & _
                "SELECT  p.Agency, count(distinct r.RegID) " & _
                "FROM tblOrgProfile as p " & _
                "LEFT JOIN tblOrgRegistrations as r " & _
                    "ON p.AgencyID = r.AgencyID " & _
                    "AND r.AgeRegistration >= [b]18 [/b]" & _
                    "AND r.AgeRegistration <= [b]20 [/b]" & _
                "LEFT JOIN tblOrgHours as h " & _
                    "ON h.RegID = r.regid " & _
                    "AND h.ActivityDate >= @fromdt " & _
                    "AND h.ActivityDate <= @todt " & _
                "GROUP BY p.Agency " & _
                "ORDER BY p.Agency"
                
       rec1.Open (esql), conn, adOpenDynamic, adLockOptimistic
       
        i = 6
        
        Do Until rec1.EOF = True
                        
                ApExcel.Workbooks("CYSReport.xls").Sheets("Direct Service").Cells(i, [b]14[/b]).Formula = rec1.Fields(1)
                
                i = i + 2
                rec1.MoveNext

        
        Loop
                
        rec1.Close
        

Open in new window

0
Comment
Question by:al4629740
3 Comments
 

Author Comment

by:al4629740
ID: 40346916
as you can see, the bold did not work for me but I think you can see the variables that need attention
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40346935
Something like this could work:

'Get List of 8 to 12 and under--------
for x as int16 = 8 to 22 step 5
  if x = 18 then 
    y = 20
  else
    y = x + 4
  end if
  esql = "DECLARE @fromdt AS datetime " & _
                "DECLARE @todt AS datetime " & _
                "SET @fromdt = '" & DTPicker1 & "' " & _
                "SET @todt = '" & DTPicker2 & "' " & _
                "SELECT  p.Agency, count(distinct r.RegID) " & _
                "FROM tblOrgProfile as p " & _
                "LEFT JOIN tblOrgRegistrations as r " & _
                    "ON p.AgencyID = r.AgencyID " & _
                    "AND r.AgeRegistration >= " & x & " & _
                    "AND r.AgeRegistration <= " & y & " & _
                "LEFT JOIN tblOrgHours as h " & _
                    "ON h.RegID = r.regid " & _
                    "AND h.ActivityDate >= @fromdt " & _
                    "AND h.ActivityDate <= @todt " & _
                "GROUP BY p.Agency " & _
                "ORDER BY p.Agency"
                
       rec1.Open (esql), conn, adOpenDynamic, adLockOptimistic
       
        i = 6
        
        Do Until rec1.EOF = True
                        
                ApExcel.Workbooks("CYSReport.xls").Sheets("Direct Service").Cells(i, [b]12[/b]).Formula = rec1.Fields(1)
                
                i = i + 2
                rec1.MoveNext

        
        Loop
                
        rec1.Close
next            
                    

Open in new window

0
 

Accepted Solution

by:
royeh earned 500 total points
ID: 40351899
Open 3 recordsets, and loop through them all:

E.G.

'Get List of 8 to 12 and under--------
       esql1 = "DECLARE @fromdt AS datetime " & _
                "DECLARE @todt AS datetime " & _
                "SET @fromdt = '" & DTPicker1 & "' " & _
                "SET @todt = '" & DTPicker2 & "' " & _
                "SELECT  p.Agency, count(distinct r.RegID) " & _
                "FROM tblOrgProfile as p " & _
                "LEFT JOIN tblOrgRegistrations as r " & _
                    "ON p.AgencyID = r.AgencyID " & _
                    "AND r.AgeRegistration >= [b]8 [/b]" & _
                    "AND r.AgeRegistration <= [b]12 [/b]" & _
                "LEFT JOIN tblOrgHours as h " & _
                    "ON h.RegID = r.regid " & _
                    "AND h.ActivityDate >= @fromdt " & _
                    "AND h.ActivityDate <= @todt " & _
                "GROUP BY p.Agency " & _
                "ORDER BY p.Agency"
                
'Get List of 13 to 17 and under--------
       esql2 = "DECLARE @fromdt AS datetime " & _
                "DECLARE @todt AS datetime " & _
                "SET @fromdt = '" & DTPicker1 & "' " & _
                "SET @todt = '" & DTPicker2 & "' " & _
                "SELECT  p.Agency, count(distinct r.RegID) " & _
                "FROM tblOrgProfile as p " & _
                "LEFT JOIN tblOrgRegistrations as r " & _
                    "ON p.AgencyID = r.AgencyID " & _
                    "AND r.AgeRegistration >= [b]13 [/b]" & _
                    "AND r.AgeRegistration <= [b]17 [/b]" & _
                "LEFT JOIN tblOrgHours as h " & _
                    "ON h.RegID = r.regid " & _
                    "AND h.ActivityDate >= @fromdt " & _
                    "AND h.ActivityDate <= @todt " & _
                "GROUP BY p.Agency " & _
                "ORDER BY p.Agency"
                
'Get List of 18 to 20 and under--------
       esql3 = "DECLARE @fromdt AS datetime " & _
                "DECLARE @todt AS datetime " & _
                "SET @fromdt = '" & DTPicker1 & "' " & _
                "SET @todt = '" & DTPicker2 & "' " & _
                "SELECT  p.Agency, count(distinct r.RegID) " & _
                "FROM tblOrgProfile as p " & _
                "LEFT JOIN tblOrgRegistrations as r " & _
                    "ON p.AgencyID = r.AgencyID " & _
                    "AND r.AgeRegistration >= [b]18 [/b]" & _
                    "AND r.AgeRegistration <= [b]20 [/b]" & _
                "LEFT JOIN tblOrgHours as h " & _
                    "ON h.RegID = r.regid " & _
                    "AND h.ActivityDate >= @fromdt " & _
                    "AND h.ActivityDate <= @todt " & _
                "GROUP BY p.Agency " & _
                "ORDER BY p.Agency"
                
       rec1.Open (esql1), conn, adOpenDynamic, adLockOptimistic
       rec2.Open (esql2), conn, adOpenDynamic, adLockOptimistic
       rec3.Open (esql3), conn, adOpenDynamic, adLockOptimistic

        i = 6
        
        Do Until rec1.EOF = True And rec2.EOF = True And rec3.EOF = True
	
                If rec1.EOF = False Then ApExcel.Workbooks("CYSReport.xls").Sheets("Direct Service").Cells(i, [b]12[/b]).Formula = rec1.Fields(1)
                If rec2.EOF = False Then ApExcel.Workbooks("CYSReport.xls").Sheets("Direct Service").Cells(i, [b]13[/b]).Formula = rec2.Fields(1)
                If rec3.EOF = False Then ApExcel.Workbooks("CYSReport.xls").Sheets("Direct Service").Cells(i, [b]14[/b]).Formula = rec3.Fields(1)
                
                i = i + 2

                If rec1.EOF = False Then rec1.MoveNext
                If rec2.EOF = False Then rec2.MoveNext
                If rec3.EOF = False Then rec3.MoveNext
        
        Loop
                
        rec1.Close
        rec2.Close
        rec3.Close

Open in new window


You'll need to double-check the EOF flags, because you have multiple recordsets.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb6 connector to mongodb 2 120
Modifying Conditional Format from VBA code 3 60
Added a column screws up code 5 63
VBA: Insert New column with specific format type 12 38
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question