repeatloop entire subroutine whi
Microsoft Office TrainingThe UK's Number 1 for Microsoft Office Training Sitemap add this page to your favourites/bookmarksBookmark page
 
view a printable version of this pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

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

replyReplyWed 26 Aug 2009, 12:09Delegate Li-kim said...

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



For upcoming training course dates see: Pricing & availability

replyReplyFri 28 Aug 2009, 12:27Delegate Li-kim said...

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.

 

 

Please browse our web site to find out more about
microsoft access training class and other Microsoft training courses.

Access tip:

Hiding rows and columns

To hide a column or row in Access, you need to minimise the column or row. This can be done by placing your mouse on the right edge of a column, wait for the mouse to change to a double aroow and then drag to minimise the column

View all Access hints and tips

forum postHi,

Could you let me know what I should use the brackets for after the initial sub command?

ie. Sub ThisMacro()

I remember it is used to define something but also remember that it wasn't used in every case.

Thanks,
Matt

» Forum post: Brackets after the sub name


Rate this page:
2.2/5 (213 votes cast)
Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses
Microsoft Certified Partner
Security Seal verified by visa, mastercard securecard

Mini sitemap. These are the main areas of our web site. Full sitemap.

Management training

Professional Skills courses
Project Management Course London
Project Management Courses London
Project Management Training London
Project Management Training
Project Seminar
Project Seminars
Time Management Course London
Time Management London
Time Management Courses London
Time Management Training London
Introduction to Finance course
Assertiveness Skills course
Effective Communications Skills training
Presentation Skills London

Training Formats

Public scheduled courses
On-site training
Closed company courses

Consultancy
Application Development

Blogs

Excel Training
MS Project Training
Microsoft Training Blog

Version differences

Office 2010 vs 2007
MS Project version differences

Training Information

London Computer Training
Computer Training London
Docklands Training Courses
Docklands Training London

Training venues London
Client list
FAQ
Pricing and availability
Course details / Syllabus

Training Articles
Training Information

Microsoft training

Microsoft Office training
& IT Applications

Microsoft Project training
Microsoft Outlook training
Microsoft Powerpoint training
Microsoft Word training
MS Project courses
MS Project training
Outlook courses
PowerPoint courses
PowerPoint training
VBA courses
Word courses
Microsoft.training
(more...)

Excel Training

Excel courses
Excel Training Courses Medway
Autonumber in Excel
Microsoft Excel training
Basic Excel Courses
Basic Excel Course
Basic Excel Training

Interested in MS Access training?

Access courses
Microsoft Access training
Microsoft access courses
Microsoft training access course
Microsoft+access+training
Access courses in london

Training provider

Training providers
IT training companies
IT training providers
Management Training providers
Management Training provider

Event history, feedback results
Events in 2012 · 2011 · 2010 · More

See also

Crystal Reports training