Adding a counter to a loop

G

Guest

Hello Group,

I have code (see below), that I need to add a counter to within the loop. I
need to know that " if this is the first time through the loop.. counter = <1
then "this code", if the counter is >1 then "this code"

If Not .BOF And Not .EOF Then
.MoveFirst
Do Until .EOF

stDocName = "Rpt_WorkIssued_toSub_Admin"
[Forms]![frmUpdateSelectedZones_Admin]![txtZone] = ![ZONE]
Result = SetPrintMDE(Me.hWnd, stDocName, True, 1, False, , , -1)

.MoveNext
Loop
End If
.Close
End With

Thanks
Grant
 
K

Ken Snell \(MVP\)

If you just need to know "first time" or "not first time", then just use a
boolean variable:

Dim blnFirstTime As Boolean
blnFirstTime = True
If Not .BOF And Not .EOF Then
.MoveFirst
Do Until .EOF
If blnFirstTime = True Then
' put code here for the first time through
Else
' put code here for all other times through
End If
stDocName = "Rpt_WorkIssued_toSub_Admin"
[Forms]![frmUpdateSelectedZones_Admin]![txtZone] = ![ZONE]
Result = SetPrintMDE(Me.hWnd, stDocName, True, 1, False, , , -1)
blnFirstTime = False
.MoveNext
Loop
End If
.Close
End With
 
W

Wayne Morgan

If all you need to know is the first time or subsequent times through the
loop, you can use a counter but don't need one. If you want to know which
time through the loop you're on, then you'll need a counter.

Examples:
(Without Counter)
Dim bolFirstTime As Boolean
If Not .BOF And Not .EOF Then

FirstTime = True
.MoveFirst
Do Until .EOF
If FirstTime Then
stDocName = "Rpt_WorkIssued_toSub_Admin"
[Forms]![frmUpdateSelectedZones_Admin]![txtZone] = ![ZONE]
Result = SetPrintMDE(Me.hWnd, stDocName, True, 1, False, ,
, -1)
Else
'Do Something Else Here
End If
.MoveNext
FirstTime = False
Loop
End If
.Close
End With

(With Counter)
Dim lngCounter As Long
If Not .BOF And Not .EOF Then
'initialize the counter at 1 for first time through
lngCounter = 0
.MoveFirst
Do Until .EOF
'Increment the counter
lngCounter = lngCounter + 1
'If lngCounter = 1 this is the first time through
If lngCounter =1 Then
stDocName = "Rpt_WorkIssued_toSub_Admin"
[Forms]![frmUpdateSelectedZones_Admin]![txtZone] =
![ZONE]
Result = SetPrintMDE(Me.hWnd, stDocName, True, 1, False,
, , -1)
Else
'Do something else here
End If
.MoveNext
Loop
End If
.Close
End With
 
G

Guest

Thanks guys.. just what I needed..
Grant


Wayne Morgan said:
If all you need to know is the first time or subsequent times through the
loop, you can use a counter but don't need one. If you want to know which
time through the loop you're on, then you'll need a counter.

Examples:
(Without Counter)
Dim bolFirstTime As Boolean
If Not .BOF And Not .EOF Then

FirstTime = True
.MoveFirst
Do Until .EOF
If FirstTime Then
stDocName = "Rpt_WorkIssued_toSub_Admin"
[Forms]![frmUpdateSelectedZones_Admin]![txtZone] = ![ZONE]
Result = SetPrintMDE(Me.hWnd, stDocName, True, 1, False, ,
, -1)
Else
'Do Something Else Here
End If
.MoveNext
FirstTime = False
Loop
End If
.Close
End With

(With Counter)
Dim lngCounter As Long
If Not .BOF And Not .EOF Then
'initialize the counter at 1 for first time through
lngCounter = 0
.MoveFirst
Do Until .EOF
'Increment the counter
lngCounter = lngCounter + 1
'If lngCounter = 1 this is the first time through
If lngCounter =1 Then
stDocName = "Rpt_WorkIssued_toSub_Admin"
[Forms]![frmUpdateSelectedZones_Admin]![txtZone] =
![ZONE]
Result = SetPrintMDE(Me.hWnd, stDocName, True, 1, False,
, , -1)
Else
'Do something else here
End If
.MoveNext
Loop
End If
.Close
End With


--
Wayne Morgan
MS Access MVP


Grant said:
Hello Group,

I have code (see below), that I need to add a counter to within the loop.
I
need to know that " if this is the first time through the loop.. counter =
<1
then "this code", if the counter is >1 then "this code"

If Not .BOF And Not .EOF Then
.MoveFirst
Do Until .EOF

stDocName = "Rpt_WorkIssued_toSub_Admin"
[Forms]![frmUpdateSelectedZones_Admin]![txtZone] = ![ZONE]
Result = SetPrintMDE(Me.hWnd, stDocName, True, 1, False, ,
, -1)

.MoveNext
Loop
End If
.Close
End With

Thanks
Grant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Nested IF 7
Loop inside a loop 6
Loop through a Control 6
Do/Loop Dilemma 6
Change Password 2
Please help with a Do/Loop 3
Looping Through Records 3
stuck in a loop 5

Top