Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Loop months of the year in this code

Posted on 2012-12-28
7
Medium Priority
?
199 Views
Last Modified: 2013-01-02
I am trying to loop 3 months of the year into the code below.  How do I write this type of loop statement.  I am drawing a blank for some reason...


'How turn this into a loop where Var1 is a loop of January, February, March so that I get the total for all 3 months

select count(*) from table where Month = Var1
rec2.CursorLocation = adUseClient
rec2.Open (esql), conn, adOpenStatic, adLockOptimistic

Total = Total + rec.Fields(0)

Open in new window

0
Comment
Question by:al4629740
7 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 38728665
Does your table have a column named "month"?  If this is a new table, I should advise you to have column names that are different than database function names.

What type of column is "month" (numeric or text)?

Normally, I would create a Select statement that summed/counted the data for a date range, rather than executing three different Select statements.
Example:
select count(*) from table where Month Between 1 and 3

Open in new window

0
 

Author Comment

by:al4629740
ID: 38728671
Month column is text.

What I need was a loop to go through specified months as mentioned in the question.  

Thanks
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 750 total points
ID: 38728860
This would answer one interpretation of your question:
Sub LoopSub(conn As Connection)
    Dim Var1 As String
    Dim m As Integer
    Dim eSQL As String
    Dim rec2 As New ADODB.Recordset
    Dim Total As Double
    
    For m = 1 To 3
        Var1 = Format(DateSerial(2000, m, 1), "MMMM")
        eSQL = "select count(*) from table where Month = " ' & Var1 & '"
        rec2.CursorLocation = adUseClient
        rec2.Open eSQL, conn, adOpenStatic, adLockOptimistic
        Total = Total + rec2.Fields(0)
        rec2.Close
    Next m
End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
Faustulus earned 750 total points
ID: 38730260
This code would convert what you already have into a loop that runs 3 times:
    Const AllMonths As String = "January February March"
    Dim i As Integer
    
    For i = 0 To 2
        select count(*) from table where Month = Split(AllMonths)(i)
        rec2.CursorLocation = adUseClient
        rec2.Open (esql), conn, adOpenStatic, adLockOptimistic

        Total = Total + rec.Fields(0)
    Next i

Open in new window

0
 
LVL 9

Expert Comment

by:shorvath
ID: 38730375
select count(*)
from table
where Month = 'January'
or  Month = 'February'
or Month = ' March'
0
 
LVL 9

Expert Comment

by:shorvath
ID: 38730376
No loop required, just the query
0
 

Author Closing Comment

by:al4629740
ID: 38738517
Thanks for the effort
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

879 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