• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

13. Control Statements > 13.3. The Exit For Statement

The Exit For Statement

VBA provides the Exit For statement to exit a For loop prematurely. For instance, the code in Example 13-1 finds the first field whose type is Integer.

Example 13-1. Finding the First Integer field
Sub FindFirstIntegerField(  )

Dim i As Integer
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Objects")

For i = 0 To rs.Fields.Count - 1
   If rs.Fields(i).Type = dbInteger Then Exit For
Next

If i < rs.Fields.Count Then
   ' First Integer field found
Else
   ' No such field exists
End If

rs.Close 

End Sub

We can also control the step size and direction for the counter in a For loop using the Step keyword. For instance, in the following code, the counter i is incremented by 2 each time the block of code is executed:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint