Solved

Modify If statement

Posted on 2011-03-03
8
238 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 500 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

831 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