Solved

Loop months of the year in this code

Posted on 2012-12-28
7
188 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now