Next without For

K

KateB

I created a DB a while ago to record people who did not attend training. As
there are new courses, staff groups, bases, training rooms etc that can be
added at any time, I added a requery sub command so that as soon as they are
added to the respective table they can be seen in the Form. My code is:

Private Sub Form_AfterUpdate()
Me!CourseList.Requery
Next
Me.StaffGroup.Requery
Next
Me.Base.Requery
Next
Me.TrainerID.Requery
Next
Me.LocationID.Requery
End Sub

However, I have just been told that it stops the DB working and gives the
error "Next without For". I don't know how to solve this! Can anyone help?
It was OK when I tested it but is now causing problems so needs solving!

Kate
 
B

bcap

That code has never worked as it is, if it used to work it has since been
changed.

The Next statements are completely redundant, remove them all.
 
K

KateB

I thought it did! It doesn't fail every time which is why its confused me.

So do I need to have an after update statement for each field, or will it
work if I just delete the Next statements?

I'm afraid I'm a self-taught novice (the worst kind I suspect!) so just
bumble along trying different things. Thanks for the help.

Kate
 
F

fredg

I thought it did! It doesn't fail every time which is why its confused me.

So do I need to have an after update statement for each field, or will it
work if I just delete the Next statements?

I'm afraid I'm a self-taught novice (the worst kind I suspect!) so just
bumble along trying different things. Thanks for the help.

Kate

The error message is correct.
For each Next in code you need to have had a For statement.
Here are some examples:

Dim X as Integer
For X = 1 to 5
'Do something
Next X

or ....

Dim Ctl as Control
For each ctl in Me.Controls
ctl.BackColor = vbYellow
Next

You don't need to use any Next statement in your code.

Private Sub Form_AfterUpdate()
Me!CourseList.Requery
Me.StaffGroup.Requery
Me.Base.Requery
Me.TrainerID.Requery
Me.LocationID.Requery
End Sub
 
B

bcap

If you remove the "Next" statements it will work in the sense that the error
will go away, but whether it does what you want I cannot say because I don't
know what your form is doing or how it is structured.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top