Solved

Loop months of the year in this code

Posted on 2012-12-28
7
194 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 45

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 250 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
Technology Partners: 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 250 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

762 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