Presentation is loading. Please wait.

Presentation is loading. Please wait.

If statements (Inven1, Inven2, Inven2a, Inven3, Inven3a)

Similar presentations


Presentation on theme: "If statements (Inven1, Inven2, Inven2a, Inven3, Inven3a)"— Presentation transcript:

1 If statements (Inven1, Inven2, Inven2a, Inven3, Inven3a)
Please use speaker notes for additional information! This is a series of programs that use the IF statement. The programs are Inven1, Inven2, Inven2a, Inven3 and Inven3a.

2 Inven1 This form was designed with a series of labels, text boxes and command buttons. There is a command button to calculate, a command button to clear the data that has been entered and a command button to end processing. Note that the buttons are called cmdClear, cmdCalc and cmdEnd with the first three letters that indicate that this is a command button in lower case.

3 Inven1 I entered in the data for Item Number, Item Name, On Hand and On Order. I then clicked Calculate. The message Need to Order appeared in the box that I had set up to receive the message. The code for this is shown on the next slide. As you will see the on hand field is checked and if it is less than 20, the Need to Order message is displayed. On later slides I will show how the color and font size were set for this display.

4 Inven1 There are three events shown here. The cmdCalc click event, the cmdClear click event and the cmdEnd click event. The cmdCalc click even contains an IF statement with no ELSE. The cmdClear simply moves a single space to all of the text boxes on the form. The cmdEnd contains the single command End which ends the processing.

5 If txtOnHand.Text < 20 Then txtMsg.Text = "Need to Order” End If
Inven1 On hand < 20 N Y Need to Order to txtMsg VISUAL BASIC CODE: If txtOnHand.Text < 20 Then txtMsg.Text = "Need to Order” End If Logic flowchart for If statement. Note that the circle at the bottom is showing the End IF.

6 Private Sub cmdCalc_Click() If txtOnHand.Text < 20 Then
Inven1 If the entry in txtOnHand is less than 20, the message Need to Order will be displayed in the text box called txtMsg. If it is not less than 20 nothing will happen. Private Sub cmdCalc_Click() If txtOnHand.Text < 20 Then txtMsg.Text = "Need to Order" End If End Sub Private Sub cmdClear_Click() txtItemNum.Text = " " txtItemName.Text = " " txtOnHand.Text = " " txtOnOrder.Text = " " txtMsg.Text = " " Private Sub cmdEnd_Click() End All of the text box fields are being set to a single space to clear out the entries. End will stop the processing and you will see the design form back on the screen. The default property on the text box fields is .Text. I have used it here, but frequently I will not since it is the default property.

7 Inven1 Note that the txtMsg text box has had the font size set to 12 and the font style set to Bold. In addition the ForeColor is red. That is why you see the message in Red and in larger, bold letters.

8 If txtOnHand.Text < 20 Then If txtOnOrder.Text < 20 Then
Inven2 On hand < 20 N Y On order < 20 N Y Need to Order to txtMsg Logic for Inven2 where two things must be true to process. If either of the conditions is false, no processing is done. VISUAL BASIC CODE: If txtOnHand.Text < 20 Then If txtOnOrder.Text < 20 Then txtMsg.Text = "Need to Order" End If

9 Inven2 In this example, both on hand and on order need to be less than 20 for the message to display. This code is written with an IF within an IF. If the first condition is true, then the second condition is checked. If the second condition is true the message is displayed. If the first condition is not true the second condition is not checked. Note that both ifs are closed with their own End If. IF condition 1 THEN IF condition 2 THEN action to be taken if both conditions are true END IF

10 If txtOnHand.Text < 20 Then If txtOnOrder.Text < 20 Then
Inven2a On hand < 20 N Y On order < 20 Okay to txtMsg N Y Low on hand to txtMsg Need to Order to txtMsg VISUAL BASIC CODE: If txtOnHand.Text < 20 Then If txtOnOrder.Text < 20 Then txtMsg.Text = "Need to Order" Else txtMsg.Text = "Low on hand" End If txtMsg.Text = "Okay" The three possible outputs are shown on the next three slides. If the first condition of on hand less than 20 is true then the second condition of on order less than 20 is checked. If the second condition is also true, Need to Order is displayed. If the second condition is not true, Low on hand is displayed. Back to the first condition. Note that if the first condition is not true, the second condition is not checked. The message Okay is displayed.

11 Inven2a Since on hand is less than 20 and on hand is less than 20, the Need to Order message is displayed.

12 Inven2a The on hand is less than 20. When on order is tested to see if it is less than 20 it is discovered that it is not. Therefore the else is executed. Note that because on hand is less that 20, the second question is asked. If it were not less than 20, the second question would not be asked. The second question is whether on order is less than 20. It is not so the else is executed. Low on hand is displayed and the if is ended.

13 Inven2a On hand is not less than 20 so the inner question or condition is never asked. The else that goes with the outer If is executed and Okay is moved to the message.

14 If txtOnHand.Text < 20 Then txtMsg.Text = "Need to Order" Else
Inven3 On hand < 20 N Y Need to Order to txtMsg On order < 20 N Y Need to Order to txtMsg VISUAL BASIC CODE: If txtOnHand.Text < 20 Then txtMsg.Text = "Need to Order" Else If txtOnOrder.Text < 20 Then End If OR code: Either condition 1 or condition 2 must be true to process. In other words either on hand must be less than 20 or on order must be less than 20.

15 Inven3 Notice that if the first condition which tests if on hand is less than 20 is true the Need to Order message is displayed. If the first condition is not true, then the else asks the second condition which is if on order is less than 20. If the second condition is true then Need to Order is displayed. If the second condition is not true, there are no more chances and nothing is displayed. This is a classic OR example. If either condition 1 or condition 2 is true the message is displayed. If neither condition is true then no processing is done.

16 Inven3 In this example, the on hand is not less than 20, but the on order is so the message Need to Order is displayed.

17 Inven3 In this slide neither on hand no on order was less than 20 so no processing is done. This slide also shows the importance of the clear. If the numbers were entered before the message was clear, the message would remain after the button to calculate was clicked.

18 VISUAL BASIC CODE: Inven3a On hand < 20 N Y Need to Order to txtMsg
On order < 20 N Y Okay to txtMsg Need to Order to txtMsg VISUAL BASIC CODE: If txtOnHand.Text < 20 Then txtMsg.Text = "Need to Order" Else If txtOnOrder.Text < 20 Then txtMsg.Text = "Okay" End If If you get a no to both conditions in the OR, then Okay is displayed.

19 Inven3a This is similar to Inven3. The difference is that now processing will be done if the answer to both conditions is false.


Download ppt "If statements (Inven1, Inven2, Inven2a, Inven3, Inven3a)"

Similar presentations


Ads by Google