Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

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
0
pdvsa
Asked:
pdvsa
1 Solution
 
Rey Obrero (Capricorn1)Commented:
Private Sub Form_BeforeUpdate(Cancel As Integer)

if  [MSAmount]  > 0 then

    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 if  
End Sub
0
 
CluskittCommented:
Just to correct capricorn1's code slightly (don't need points):

if  [MSAmount]  >= 0 then
0
 
roger_karamCommented:
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)
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
CluskittCommented:
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.
0
 
pdvsaProject financeAuthor Commented:
thanks!
0
 
CluskittCommented:
Glad to help :)
0
 
roger_karamCommented:
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. :-)
0
 
CluskittCommented:
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. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now