p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Access VBA (http://p2p.wrox.com/forumdisplay.php?f=80)
-   -   Nested Do Until Loop (http://p2p.wrox.com/showthread.php?t=19198)

goels September 30th, 2004 08:21 AM

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


pgtips September 30th, 2004 08:37 AM

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.

goels September 30th, 2004 08:44 AM

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

Cheers
Goels



All times are GMT -4. The time now is 07:35 AM.

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