Error with For..Each..Next loop

hi there

Registered User.
Local time
Yesterday, 21:45
Joined
Sep 5, 2002
Messages
171
hi all,

having trouble with a simple audit trail routine i'm writing. i'm getting a compile error indicating:

"Next without For"

here's the code.

***********************
Dim ctl As Control
Dim frm As Form
Dim sUser As String

sUser = Environ("UserName")

Set frm = fForm

For Each ctl In frm.Controls 'loop through controls
Select Case ctl.ControlType 'only loop through certain controls
Case acTextBox, acComboBox, acCheckBox
Select Case ctl.Name 'don't review audit trail controls
Case "Created", "CreatedBy", "Modified", "ModifiedBy"
Next ctl
Case Else 'look for first control whose value has changed
If (ctl.Value <> ctl.OldValue) Then
mMod.Value = Now
mModBy.Value = sUser
Exit For
Else
Next ctl
End If
End Select
End Select
*******************************************************

can anyone see where i'm going wrong?

many thanks for the help
 
Yes; the Select/Case must be completely within the loop (as must the If/Then). In other words, you can't have a "Next" within either of those. Also, you can only have 1 "Next" (unless I'm blind and missed the other "For").
 
hi all,

having trouble with a simple audit trail routine i'm writing. i'm getting a compile error indicating:

"Next without For"

here's the code.

***********************
Dim ctl As Control
Dim frm As Form
Dim sUser As String

sUser = Environ("UserName")

Set frm = fForm

For Each ctl In frm.Controls 'loop through controls
Select Case ctl.ControlType 'only loop through certain controls
Case acTextBox, acComboBox, acCheckBox
Select Case ctl.Name 'don't review audit trail controls
Case "Created", "CreatedBy", "Modified", "ModifiedBy"
Next ctl
Case Else 'look for first control whose value has changed
If (ctl.Value <> ctl.OldValue) Then
mMod.Value = Now
mModBy.Value = sUser
Exit For
Else
Next ctl
End If
End Select
End Select
*******************************************************

can anyone see where i'm going wrong?

many thanks for the help


The code does not reflect the proper structures for the commands being used. In your case, something more like the following should be used with modification to account for the need to end the loop when the target is found.
Code:
For
    Select
        Case FirstCase
            Do Something
        Case SecondCase
            Do Something Else
        Case Last Case
            Do Something Else
        Case Else
            Do Something Else
    End Select
Next
 
Last edited:
hi guys,

thanks for the info. i figured it was some syntax thing.

i used "GoTo" to pull my "Next ctl" out of the condition clause and everything worked with the existing logic.

thanks for the help
 

Users who are viewing this thread

Back
Top Bottom