Else without If

  • Thread starter Thread starter Jody
  • Start date Start date
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?
 
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
 
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.
 
Back
Top