Compile error: else without if

W

Will

I get a compile error: else without if error message from this code. Tried
and tried removing ifs and end ifs and all Thens are at the end of the line.
Can anyone tell me what's wrong with it?

Private Sub btnSave_Click()
On Error GoTo Err_btnSave_Click

If Me!Status > 4 And Me!Quantity <> Me!OldQuantity Then 'After Confirmed
can't edit quantity
MsgBox "Cannot edit quantity of PO when already ordered, but up
to 20% + or - can be booked in", , "Purchase Order"
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
End If
ElseIf Me!Status < 5 And Me!Quantity < (Me!OldQuantity * 0.8) Then
'Reducing below 20% permitted
MsgBox "Quantity reduced by more than 20 %", , "Purchase Order"
DoCmd.DoMenuItem A_FORMBAR, A_FILE, A_SAVERECORD, , A_MENU_VER20
DoCmd.DoMenuItem A_FORMBAR, A_RECORDSMENU, A_REFRESH, ,
A_MENU_VER20
End If
ElseIf Me!Quantity > (Me!OldQuantity * 1.2) Then
MsgBox "Quantity increased by more than 20 %, Status is now
amended and awaits reauthorisation", , "Purchase Order"
Me.Status = 1 'If increased by more than 20% status becomes new
and needs to be authorised
DoCmd.DoMenuItem A_FORMBAR, A_FILE, A_SAVERECORD, , A_MENU_VER20
DoCmd.DoMenuItem A_FORMBAR, A_RECORDSMENU, A_REFRESH, ,
A_MENU_VER20
End If
Else
DoCmd.DoMenuItem A_FORMBAR, A_FILE, A_SAVERECORD, , A_MENU_VER20
DoCmd.DoMenuItem A_FORMBAR, A_RECORDSMENU, A_REFRESH, , A_MENU_VER20
MsgBox "Edit of PO is now saved", , "Purchase Order"
DoCmd.Close
End If
Exit_btnSave_Click:
Exit Sub

Err_btnSave_Click:
MsgBox Error$
Resume Exit_btnSave_Click


Thanks in advance
 
G

George Nicholson

The proper and expected structure is
If...Then
...
ElseIf...Then
...
ElseIf ...Then
...
Else
...
End If

*NOT*

If..Then
...
End If
ElseIf
...
End If
ElseIf
...
End If
Else
...
End If

One, and only one If, Else and *End If* per structure (athough you can nest
structures)
Multiple ElseIfs are allowed. If True, all statements between it and the
next ElseIf or Else will be executed. You only time you would need an End If
following an ElseIf would be if there is no subsequent ElseIf or Else, which
isn't the case in your code.
 
F

Frank Stone

hi,
try removing the first end if or changing the elseif just
after it to if. (at lines 4 & 5 i think)
 
W

Will

thanks to both your replies!

I now want to add a vbokcancel option on the message box. I have assigned
If Statements for the ok and cancel options but they are not functioning
correctly. I have tried the DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo,
, acMenuVer70 and me.undo. At the moment decreasing and increasing the
quantity work directly opposite. When I click OK and when I click cancel to
decrease it undoes the change. When I click OK and when I try clicking
cancel it keeps the change. Any ideas where I am going wrong?
Your help is greatly appreciated. Here's the code:


Private Sub btnSave_Click()
On Error GoTo Err_btnSave_Click

If Me!Status > 4 And Me!Quantity <> Me!OldQuantity Then 'After Confirmed
can 't edit quantity
MsgBox "Cannot edit quantity of PO already ordered", , "Purchase
Order"
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70

ElseIf Me!Status < 5 And Me!Quantity < (Me!OldQuantity * 0.8) Then
'Reducing below 20% permitted
MsgBox "Quantity reduced by more than 20 %", vbOKCancel,
"Purchase Order Quantity Decreased"
If vbOK = 1 Then
DoCmd.DoMenuItem A_FORMBAR, A_FILE, A_SAVERECORD, , A_MENU_VER20
DoCmd.DoMenuItem A_FORMBAR, A_RECORDSMENU, A_REFRESH, ,
A_MENU_VER20
If vbCancel = 2 Then
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
End If
End If

ElseIf Me!Quantity > (Me!OldQuantity * 1.2) Then
MsgBox "Quantity increased by more than 20 %, Status of PO now
amended and awaits reauthorisation", vbOKCancel, "Purchase Order Quantity
Increased"
If vbOK = 1 Then
Me.Status = 1 'If increased by more than 20% status becomes new and
needs to be authorised
DoCmd.DoMenuItem A_FORMBAR, A_FILE, A_SAVERECORD, , A_MENU_VER20
DoCmd.DoMenuItem A_FORMBAR, A_RECORDSMENU, A_REFRESH, ,
A_MENU_VER20
If vbCancel = 2 Then
Me.Undo
End If
End If

Else
DoCmd.DoMenuItem A_FORMBAR, A_FILE, A_SAVERECORD, , A_MENU_VER20
DoCmd.DoMenuItem A_FORMBAR, A_RECORDSMENU, A_REFRESH, , A_MENU_VER20
MsgBox "Edit of PO is now saved", , "Purchase Order"
DoCmd.Close
End If
Exit_btnSave_Click:
Exit Sub

Err_btnSave_Click:
MsgBox Error$
Resume Exit_btnSave_Click


End Sub
 
W

Will

Problem solved

I used If Resp = VBOk and Else and this works fine on both now

Many thanks
 

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

Similar Threads


Top