Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on If statements (Calculate, Calculate1, Calculate2)

Similar presentations


Presentation on theme: "More on If statements (Calculate, Calculate1, Calculate2)"— Presentation transcript:

1 More on If statements (Calculate, Calculate1, Calculate2)
Please use speaker notes for additional information! This Visual Basic project deals with IF statements and message boxes.

2 Calculate This uses the If..Else structure to test for the value that was entered in txtDiscCode on the form.

3 Calculate Discount Code = A N Y Discount Code = B N Y Result =
Amt * .8 Discount Code = C Result = Amt * .85 N Y Discount Code = D Result = Amt * .9 N Y Messge Enter code A, B, C, or D Result = Amt * .95 This is the logic flowchart for the IF code in the Visual Basic. This is an OR structure with 4 different possibilities. If the code is neither A, B, C, or D then the message is displayed. Notice that the last IF is ended first with a circle that would be coded as an End If. Then the next to the last IF is ended with a circle and the this pattern continues until the top IF is ended with its own End IF.

4 Calculate The user entered a discount code of A so the processing multiplied the value entered by the user by 0.8 and stored the answer in txtRslt which is the result field on the form. Note that I did not use work areas in this code. The results of the calculation are but directly in the text box on the form.

5 Calculate In this case, the code is B, so the amount that is entered by the user is multiplied by 0.85.

6 Calculate If the code is not A, it is checked to see if it is B. If the code is not B it is checked to see if it is C. In this example, the code was entered as C so the amount that was entered as 100 is multiplied by .9 and the answer is put into the Result text box on the form.

7 Calculate In this case checking found that the code was not A, was not B and was not C. However, it was D so the amount of 100 that was entered was multiplied by .95 and the stored in the txtRslt text box on the form.

8 MsgBox "Enter code A, B, C, or D", vbOKOnly, "Error"
Calculate The MsgBox uses several parameters in this example. The parameters are separated by commas. The first parameter is the message itself. The second parameter is the type of message box. In this case it is showing only the OK button. The third parameter is the title for the message box. Please note that the message box was dragged to this location so that everything could be seen. When I clicked the Calculate button the message box came up over the form. Note also in this structure that since there were 4 IF statements there are 4 End If statements. See the circles on the flowchart for clarification. MsgBox "Enter code A, B, C, or D", vbOKOnly, "Error"

9 Calculate1 This is an alternate approach to the same logic using the ElseIf statement. This code uses the ElseIf instead of the IF statement with separate ELSE statements. The last ELSE is simply Else. The logic and the results are the same as the previous examples.

10 Calculate2 The Case structure can also be used to determine the contents of txtDiscCode. In this case, I first give the command Select Case and name the field that is to be tested. Next I code the Case for each possibility using the structure Case “A” etc, Finally, Case Else gives the code for the MsgBox which is to be displayed if none of the Case statements evaluated as TRUE. This alternative uses the Case structure to determine the processing that needs to be done.


Download ppt "More on If statements (Calculate, Calculate1, Calculate2)"

Similar presentations


Ads by Google