By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

How to add two If Me statements together

P: 59
I have two statements which, on their own, work perfectly. I am trying to get them to work together in one statement but get an error message. They are
Expand|Select|Wrap|Line Numbers
  1. Private Sub NextDue_BeforeUpdate(Cancel As Integer)
  2.     If Me.NextDue > Me.EndDate Then
  3.     MsgBox "Check Rent Review Date"
  4.     Cancel = True
  5.     End If
  6. End Sub
Expand|Select|Wrap|Line Numbers
  1.   If Me.NextDue < Date Then
  2.     MsgBox "Date in Past"
  3.     Cancel = True
  4.     End If
  5. End Sub
I have tried to add them together as
Expand|Select|Wrap|Line Numbers
  1. Private Sub NextDue_BeforeUpdate(Cancel As Integer)
  2.     If Me.NextDue > Me.EndDate Then
  3.     MsgBox "Check Rent Review Date"
  4.     Cancel = True
  5.     End If
  6. Else
  7.     If Me.NextDue < Date Then
  8.     MsgBox "Date in Past"
  9.     Cancel = True
  10.     End If
  11. End Sub
but get the Compile Error: "Else without If". I've tried to add ElseIf but get another Compile Error: "Expected: expression". Can they be done together?
Apr 3 '17 #1

✓ answered by MikeTheBike

Hi

Not sure what you are trying to do but perhaps this is what you want.
Expand|Select|Wrap|Line Numbers
  1. Private Sub NextDue_BeforeUpdate(Cancel As Integer)
  2.  If Me.NextDue > Me.EndDate Then
  3.      MsgBox "Check Rent Review Date"
  4.      Cancel = True
  5.  ElseIf Me.NextDue < Date Then
  6.      MsgBox "Date in Past"
  7.      Cancel = True
  8.  End If
  9.  
  10. End Sub
???


MTB

Share this Question
Share on Google+
3 Replies


Expert 100+
P: 634
Hi

Not sure what you are trying to do but perhaps this is what you want.
Expand|Select|Wrap|Line Numbers
  1. Private Sub NextDue_BeforeUpdate(Cancel As Integer)
  2.  If Me.NextDue > Me.EndDate Then
  3.      MsgBox "Check Rent Review Date"
  4.      Cancel = True
  5.  ElseIf Me.NextDue < Date Then
  6.      MsgBox "Date in Past"
  7.      Cancel = True
  8.  End If
  9.  
  10. End Sub
???


MTB
Apr 3 '17 #2

P: 59
MTB, yes worked perfectly. Could have sworn I tried ElseIf that way also!
Apr 3 '17 #3

NeoPa
Expert Mod 15k+
P: 31,494
Hi Glat.

It's very possible you confused yourself with your code indenting. It can be an enormous help when done properly, but worse than not using it at all when done inconsistently as you have in your example. Notice it's perfect in Mike's suggestion.
Apr 4 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.