469,611 Members | 1,733 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,611 developers. It's quick & easy.

Trouble With "For Each" and "Next"

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
3 1648
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
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
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.

Similar topics

8 posts views Thread by joe | last post: by
2 posts views Thread by Dexter | last post: by
11 posts views Thread by .Net Sports | last post: by
11 posts views Thread by Maxwell2006 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.