Re: Trouble With "For Each" and "Next"

Giganews Newsgroups
Subject: Re: Trouble With "For Each" and "Next"
Posted by:  r…
Date: 28 Aug 2006

Cancelling the event doesn't stop the code. You have to do that

Put an Exit For or Exit Sub after the Cancel Event


Dale wrote:
> Access 2000
> I am trying to check the form to be sure that required fields are
> entered.  For each required field (Control) I have set the tag property
> to "1".  I am trying to loop through all controls with a tag property
> of "1", If they are null, I want to stop the loop and setfocus on the
> control.
> The below code works Except it will not stop looping when a control is
> null. It will display the msgBox and then continue to the next control.
> Dim ctl As Control
> For Each ctl in Me,Controls
>  If ctl.Tag = "1" And IsNull(ctl) Then
>      MsgBox("Required Field Is Blank")
>      ctl.SetFocus
>      DoCmd.CancelEvent
>  End If
> Next ctl
> MsgBox("All Required Fields Complete")
> Seems simple, but I can't get the loop to stop at the CancelEvent.
> Any and ALL help truely appreciated!
> Dale



In response to

Trouble With "For Each" and "Next" posted by Dale on 28 Aug 2006