Else without If

J

Jody

I have a form that I have been requested to make certian fields required on.
However, they only need to be required if there is a value in a corresponding
field. There are 20 potential "required" fields on this form. I have the
following code which works for one required field, but I am having trouble
with joining multiple statements together to get the desired effect on all 20
fields:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If (Me.PITM1) > 0 And IsNull(Me.PQTY1) Then
MsgBox "QTY is required"
Cancel = True
Me.PQTY1.SetFocus
End If

Else

If (Me.CITM1) > 0 And IsNull(Me.CQTY1) Then
MsgBox "QTY is required"
Cancel = True
Me.CQTY1.SetFocus
End If

End Sub

I am getting a compile error: "Else without If" when this code trys to run.
Can someone point me in the right direction?
 
D

Dirk Goldgar

Jody said:
I have a form that I have been requested to make certian fields required
on.
However, they only need to be required if there is a value in a
corresponding
field. There are 20 potential "required" fields on this form. I have the
following code which works for one required field, but I am having trouble
with joining multiple statements together to get the desired effect on all
20
fields:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If (Me.PITM1) > 0 And IsNull(Me.PQTY1) Then
MsgBox "QTY is required"
Cancel = True
Me.PQTY1.SetFocus
End If

Else

If (Me.CITM1) > 0 And IsNull(Me.CQTY1) Then
MsgBox "QTY is required"
Cancel = True
Me.CQTY1.SetFocus
End If

End Sub

I am getting a compile error: "Else without If" when this code trys to
run.
Can someone point me in the right direction?


You have an Else statement after your first End If. With block If
statements, an Else always goes inside an If ... End If block.

For what you're doing, I think you want to use the ElseIf statement, anyway.
Try this:

If (Me.PITM1) > 0 And IsNull(Me.PQTY1) Then
MsgBox "QTY is required"
Cancel = True
Me.PQTY1.SetFocus
ElseIf (Me.CITM1) > 0 And IsNull(Me.CQTY1) Then
MsgBox "QTY is required"
Cancel = True
Me.CQTY1.SetFocus
End If
 
J

Jody

AH! Thank you both. I had the wrong syntax when I attempted the ElseIf (I
was writing it as Else If). I will try both ways and see which works best.
Again, thanks for your help.
 

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