repeatloop entire subroutine whi

Forum home » Delegate support and help forum » Microsoft Access VBA Training and help » Repeat/Loop entire subroutine (which contains loop)

Repeat/Loop entire subroutine (which contains loop)

resolvedResolved · High Priority · Version 2003

Li-kim has attended:
Excel Intermediate course
Excel Advanced course
Excel VBA Intro Intermediate course

Repeat/Loop entire subroutine (which contains loop)

Hello, I have a nested loop problem.

I have a sub that contains a loop. It connects to an ODBC database and returns values corresponding to values in cells in Row 1, Column z, where z = z + 1. E.g. D1, contains Jan 2009, E1 contains Feb 2009, etc.

The connection is based on several driver parameters, one of which is contained in Row x, Column B. Right now, the above sub connects using the parameter specified in cell B2 and loops through cells (1, z) to return as many values as there are in Row 1.

I would like the entire loop above to then connect using the parameter in cell B3 and loop through cells (1, z) to return as many values as there are in Row 1, then do the same for the parameter in cell B4.

Sub TotalReferringSiteVisits()

' MACRO __ USE FOR TOTAL VISITS FROM REFERRING SITES
' Macro connects to ODBC database with profile_ID specified in Cells(x, 2),
' each time retrieving total ReferringSite visits i.e. site equals to NULL for months specified in Cells(1, z),
' then puts them in separate columns on 1 row for each month e.g. Cells(2, z) contains Cells(1, z) result, Cells(2, z + 1) contains Cells(1, z) result, etc.
' (results are returned from SQL as 2 rows by 1 column)
' then does all of the above but for connect ODBC database connection with profile_ID specified in Cells(x+1,2), with results in Row 4


Dim x As Integer
x = 2

Dim y As Integer
y = 2

Dim z As Integer
z = 4


' Set Do loop to stop when an empty cell is reached

Do Until IsEmpty(Cells(1, z))

' Connect to Database, Return query using SQL window

With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DSN=xxx Driver xxx;DATABASE=xxxxxxxx;SERVER=odbc.xxxxxxxx.com;" _
), Array("PORT=123;AccountId=xxxxxxxx;UID=xxxxxxxx;" _
), Array("PASSWORD=xxxxxxxx;" _
), Array("ProfileGuid=" & Cells(x, 2) & ";SSL=1;")), Destination:=Cells(y, z))

.CommandText = Array( _
"SELECT Sum(ReferringSite_0.Visits)" & Chr(13) & "" & Chr(10) & _
"FROM ReferringSite ReferringSite_0" & Chr(13) & "" & Chr(10) & _
"WHERE (ReferringSite_0.TimePeriod='" & Cells(1, z) & "') AND (ReferringSite_0.Site Is Null)")

.Name = "Query_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False

End With


' Checking for content in next cell down in column B

x = x + 1
z = z + 1

Loop
End Sub



RE: Repeat/Loop entire subroutine (which contains loop)

I have got it to loop but still need help. What I have now is...

Sub
Dim x As Integer
x = 2
Dim y As Integer
y = 2
For x = 2 To 4
Dim z As Integer
z = 5
Do
With...
.CommandText = Array( _...
End With
z = z + 1
Loop Until IsEmpty(Cells(1, z))
y = y + 2
Next x
End Sub

The problem with the above though, is that I have to specify x = 2 to 4, where there are 3 values (cells B2-B4). I would have liked the external loop to occur until all cells in column B are empty, the same way the internal loop occurs.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Access tip:

Action queries

Unlike select queries you cannot use action queries as a data source for the use with forms or reports,as they do not return a dataset that can be read.

View all Access hints and tips


Server loaded in 0.06 secs.