Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| Search | Today's Posts | Mark Forums Read
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 30th, 2004, 08:21 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Nested Do Until Loop

Hi Everyone,

When I test the below given code in the immediate window of VBA Editor the innermost loop functions while the outer loop is not entered into after the innermost loop has finished.

Option Compare Database
Option Explicit

Sub EasyScript()

Dim db As Database
Dim rec As Recordset
Dim rec1 As Recordset
Dim accountname As String
Dim Date1 As String


Set db = CurrentDb()
Set rec = db.OpenRecordset("tbl_accountname")
Set rec1 = db.OpenRecordset("tbl_date")

Do Until rec.EOF
accountname = rec(2)
Do Until rec1.EOF
Date1 = rec1(2)
Debug.Print accountname
Debug.Print Date1
rec1.MoveNext
Loop
rec.MoveNext
Loop


End Sub

The data that comes in rec(2) one-by-one is : 00, 01, 02 & 03
The data comes in rec1(2) one-by-one is : 00 & 01

A run of the code gives:

00
00
00
01

Whearas it should be giving:

00
00
00
01
01
00
01
01
02
00
02
02
03
00
03
01

I cannot make out why this is happening. I am new to VBA and i could be possibly making some very foolish mistake. Please bear with me.

Chow
Goels

Reply With Quote
  #2 (permalink)  
Old September 30th, 2004, 08:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

After the first pass through the inner loop you need to set rec1 back to the beginning (rec1.MoveFirst), otherwise on the second pass through the outer loop the "Do Until rec1.EOF" bit does nothing because rec1 is still at EOF from the previous pass.
Reply With Quote
  #3 (permalink)  
Old September 30th, 2004, 08:44 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot for your help. It was such an oversight on my part not to notice it. Thanks a lot.

Cheers
Goels

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested For-Each Loop In XSLT dghosh XSLT 10 July 2nd, 2008 04:34 AM
QueryTables nested loop Sherman McCoy Excel VBA 0 June 4th, 2008 02:37 PM
nested loop adnan2 Java Basics 1 July 13th, 2007 04:21 PM
nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.