Link to home
Start Free TrialLog in
Avatar of pdvsa
pdvsaFlag for United States of America

asked on

Modify If statement

Experts, how can I modify the below for if [MSAmount] is negative then dont fire the code and Else do fire it.  

Private Sub Form_BeforeUpdate(Cancel As Integer)
   
    If IsNull([InvoiceDate]) Or IsNull([ExpectedDateRecCash]) Then
        MsgBox "Enter the Invoice Date or Expected Date to Receive Cash before moving to another record"
    Cancel = True
    End If
   
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just to correct capricorn1's code slightly (don't need points):

if  [MSAmount]  >= 0 then
Just to throw in an opinion as well, I like using and exit statement to avoid multiple IF - End IF blocks:

If [MSAmount] < 0 then exit sub

(my 2 cents - points to the first guy that answered)
Actually, that would create more IFs. If < 0 then exit sub ElseIf etc...
Using just If >= 0 will do all you want in just one go. Besides, you might want to continue running more code after the IFs. You just don't want the code inside it to run for negatives.

As a rule, I only use exit sub when I know for a fact I want the code interrupted due to something.
Avatar of pdvsa

ASKER

thanks!
Glad to help :)
Hey Cluskitt, I agree, but if you follow his statement: "is negative then dont fire the code", then using the exit sub line will get you out. Also, you don't need the else if afterwards. Just a single if. :-)
ElseIf or
EndIf
If
Is pretty much the same, though, when the first if has only 2 possibilities :)

And yes, I'm not disagreeing with you, exit sub is handy in many cases, including this one. I was just pointing out that I personally prefer to use it only on cases where I have to leave in the middle of the code. If there's only one if, there's really no point in using exit sub, as failure to fulfill the condition will bring the code to the end of the Sub, namely the End Sub line. :)