Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you do a continue or NEXT in a For Next Loop 1

Status
Not open for further replies.

Pack10

Programmer
Feb 3, 2010
495
US
I have a For Next construct. I sometimes have a condition where I need to skip to the next iteration. How do you do it in Access VBA, that is do a Next.
 
How about helping us out, can you show what you have to this point?

FYI- there are several Access forums, this one is used mostly for all except Access, but sense you asked here we will try and answer here.
 
normally it is just

for
if
end if
next

so going to the next item is equivalent to falling out of the if check. But if you had to you can do something like this.

Public Sub printOdds()
Dim i As Integer
For i = 1 To 10
If i / 2 = i \ 2 Then GoTo nextI
Debug.Print i
nextI:
Next i
End Sub

That is just an example. In reality I would avoid any go to statements and simply use the first construct

Public Sub printOdds()
Dim i As Integer
For i = 1 To 10
If not (i / 2 = i \ 2) Then
Debug.Print i
end if
next i
End Sub
 
There are numerous approaches. The best one depends on what you're dealing with. For example:
Code:
Sub Demo()
Dim i As Integer
For i = 1 To 10
  If i = 3 Then
    i = 4
  Else
    'Do something else
  End If
  MsgBox i
Next i
For i = 1 To 10
'Some test here, eg:
  If MsgBox("Skip the next item?", vbYesNo) = vbYes Then i = i + 1
  MsgBox i
Next i
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
Thanks. Thats what I did and it worked out well.
 
Be aware that this workaround is irrelevent for a FOR EACH ... NEXT loop.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top