Solved

Modify If statement

Posted on 2011-03-03
8
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

724 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