Solved

Loop months of the year in this code

Posted on 2012-12-28
7
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…

734 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