By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Trouble With "For Each" and "Next"

P: n/a
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

Aug 29 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Dale <mt****@yadtel.netwrote:
: Access 2000

: 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
Exit For <================
: End If
: Next ctl
<snip>

You need to exit the loop: see above.
--thelma
: Dale

Aug 29 '06 #2

P: n/a
ray
Cancelling the event doesn't stop the code. You have to do that
explicitly.

Put an Exit For or Exit Sub after the Cancel Event

Ray
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
Aug 29 '06 #3

P: n/a
ray
Cancelling the event doesn't stop the code. You have to do that
explicitly.

Put an Exit For or Exit Sub after the Cancel Event

Ray
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
Aug 29 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.