Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Modify If statement

Posted on 2011-03-03
8
Medium Priority
?
271 Views
Last Modified: 2012-05-11
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
Comment
Question by:pdvsa
8 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 35026804
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
 
LVL 18

Expert Comment

by:Cluskitt
ID: 35026877
Just to correct capricorn1's code slightly (don't need points):

if  [MSAmount]  >= 0 then
0
 
LVL 5

Expert Comment

by:roger_karam
ID: 35027054
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:Cluskitt
ID: 35027101
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
 

Author Closing Comment

by:pdvsa
ID: 35027672
thanks!
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 35027735
Glad to help :)
0
 
LVL 5

Expert Comment

by:roger_karam
ID: 35027763
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
 
LVL 18

Expert Comment

by:Cluskitt
ID: 35027818
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question