Presentation is loading. Please wait.

Presentation is loading. Please wait.

Delivery and other DO Examples Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "Delivery and other DO Examples Please use speaker notes for additional information!"— Presentation transcript:

1 Delivery and other DO Examples Please use speaker notes for additional information!

2 Do Loop Private Sub cmdCalc_Click() Rem up to 1 lb costs $1.50 Rem over 1 lb costs $1.50 plus $.50 for every Rem additional 4 0unces Const cstFirstLb As Single = 1.5 Const cstEvery4oz As Single = 0.5 Dim wrkWeight As Integer Dim wrkCost As Single wrkWeight = Val(txtPounds.Text) * 16 + Val(txtOunces.Text) If wrkWeight < 17 Then wrkCost = cstFirstLb Else wrkWeight = wrkWeight - 16 wrkCost = cstFirstLb Do While wrkWeight > 0 wrkCost = wrkCost + cstEvery4oz wrkWeight = wrkWeight - 4 Loop End If txtAmountDue.Text = Format(wrkCost, "Currency") End Sub 5 pounds were entered - I am converting that to ounces and then adding the number of ounces that were entered. 5 * 16 + 6 = 86 for this example If weight is a pound or less the cost is the cost of a pound. 86 not < 17 In this example, wrkWeight = 86 - 16 = 70 wrkCost = 1.5 The pricing is 1.5 for the first pound and then.5 for every 4 ounces (or portion of 4 ounces) over. Does the loop while wrkWeight > 0. Each pass through it adds.5 to the cost and subtracts 4 ounces from the weight. wrkCost is put in the text box - in this example it is 10.5.

3 Private Sub cmdCalc_Click() Rem up to 1 lb costs $1.50 Rem over 1 lb costs $1.50 plus $.50 for every Rem additional 4 0unces Const cstFirstLb As Single = 1.5 Const cstEvery4oz As Single = 0.5 Dim wrkWeight As Integer Dim wrkCost As Single wrkWeight = Val(txtPounds.Text) * 16 + Val(txtOunces.Text) If wrkWeight < 17 Then wrkCost = cstFirstLb Else wrkWeight = wrkWeight - 16 wrkCost = cstFirstLb Do While wrkWeight > 0 wrkCost = wrkCost + cstEvery4oz wrkWeight = wrkWeight - 4 Loop End If txtAmountDue.Text = Format(wrkCost, "Currency") End Sub 5 pounds were entered - I am converting that to ounces and then adding the number of ounces that were entered. 5 * 16 + 6 = 86 for this example If weight is a pound or less the cost is the cost of a pound. 86 not < 17 In this example, wrkWeight = 86 - 16 = 70 wrkCost = 1.5 wrkCost 0f 10.5 is s put in the text box Enter Loop: wrkWeight = 70, wrkCost = 1.5 End 1st pass: wrkCost = 2 wrkWeight = 66 End 2nd pass: wrkCost = 2.5 wrkWeight = 62 End 3rd pass: wrkCost = 3 wrkWeight = 58 End 4th pass: wrkCost = 3.5 wrkWeight = 54 End 5th pass: wrkCost = 4 wrkWeight = 50 End 6th pass: wrkCost = 4.5 wrkWeight = 46 End 7th pass: wrkCost = 5 wrkWeight = 42 End 8th pass: wrkCost = 5.5 wrkWeight = 38 End 9th pass: wrkCost = 6 wrkWeight = 34 End 10th pass: wrkCost = 6.5 wrkWeight = 30 End 11th pass: wrkCost = 7 wrkWeight = 26 End 12th pass: wrkCost = 7.5 wrkWeight = 22 End 13th pass: wrkCost = 8 wrkWeight = 18 End 14th pass: wrkCost = 8.5 wrkWeight = 14 End 15th pass: wrkCost = 9 wrkWeight = 10 End 16th pass: wrkCost = 9.5 wrkWeight = 6 End 17th pass: wrkCost = 10 wrkWeight = 2 End 17th pass: wrkCost = 10.5 wrkWeight = -2 wrkWeight is no longer greater than 0 so the loop ends

4 Private Sub cmdCalc_Click() Rem up to 1 lb costs $1.50 Rem over 1 lb costs $1.50 plus $.50 for every Rem additional 4 0unces Const cstFirstLb As Single = 1.5 Const cstEvery4oz As Single = 0.5 Dim wrkWeight As Integer Dim wrkCost As Single wrkWeight = Val(txtPounds.Text) * 16 + Val(txtOunces.Text) If wrkWeight < 17 Then wrkCost = cstFirstLb Else wrkWeight = wrkWeight - 16 wrkCost = cstFirstLb Do While wrkWeight > 0 wrkCost = wrkCost + cstEvery4oz wrkWeight = wrkWeight - 4 Loop End If txtAmountDue.Text = Format(wrkCost, "Currency") End Sub Private Sub cmdClear_Click() txtName.Text = "" txtStAdr.Text = "" txtCSZ.Text = "" txtPounds.Text = "" txtOunces.Text = "" txtAmountDue.Text = "" End Sub Private Sub cmdExit_Click() End End Sub Delivery

5 ProjNP1 "123","Ann Alden",25,"A" "234","John Adams",35,"B" "345","Susan Smith",40,"A" "456","Linda Costa",36,"B" "567","Donald Page",28,"B" "678","Ryan Brown",40,"C" "789","Robert Souza",35,"A" "890","Mary Fields",25,"C" Option Explicit Private Sub cmdExit_Click() Close #1 End End Sub Private Sub cmdRead_Click() Dim Idno As String, Name As String Dim Hrs As Integer, PayCode As String If Not EOF(1) Then Input #1, Idno, Name, Hrs, PayCode txtIdNo = Idno txtName = Name txtHrs = Hrs txtPayCode = PayCode End If End Sub Private Sub Form_Load() Open "A:\Nametest.txt" For Input As #1 End Sub

6 Option Explicit Private Sub cmdClear_Click() txtEnterName.Text = "" txtPay.Text = "" Open "A:\Nametest.txt" For Input As #1 txtEnterName.SetFocus End Sub Private Sub cmdEnd_Click() Close #1 End End Sub Private Sub cmdFind_Click() Dim IdNo As String, Name As String Dim Hrs As Integer, PayCode As String Dim wrkPay As Single Do While Name <> txtEnterName.Text And Not EOF(1) Input #1, IdNo, Name, Hrs, PayCode Loop If Name = txtEnterName.Text Then If PayCode = "A" Then wrkPay = Hrs * 10 ElseIf PayCode = "B" Then wrkPay = Hrs * 15 ElseIf PayCode = "C" Then wrkPay = Hrs * 20 Else wrkPay = Hrs * 0 End If txtPay.Text = Format(wrkPay, "Currency") End If Close #1 End Sub Private Sub Form_Load() Open "A:\Nametest.txt" For Input As #1 End Sub "123","Ann Alden",25,"A" "234","John Adams",35,"B" "345","Susan Smith",40,"A" "456","Linda Costa",36,"B" "567","Donald Page",28,"B" "678","Ryan Brown",40,"C" "789","Robert Souza",35,"A" "890","Mary Fields",25,"C" ProjPay1

7 Private Sub cmdFind_Click() Dim IdNo As String, Name As String Dim Hrs As Integer, PayCode As String Dim wrkPay As Single Do While Name <> txtEnterName.Text And Not EOF(1) Input #1, IdNo, Name, Hrs, PayCode Loop If Name = txtEnterName.Text Then If PayCode = "A" Then wrkPay = Hrs * 10 ElseIf PayCode = "B" Then wrkPay = Hrs * 15 ElseIf PayCode = "C" Then wrkPay = Hrs * 20 Else wrkPay = Hrs * 0 End If txtPay.Text = Format(wrkPay, "Currency") End If Close #1 End Sub "123","Ann Alden",25,"A" "234","John Adams",35,"B" "345","Susan Smith",40,"A" "456","Linda Costa",36,"B" "567","Donald Page",28,"B" "678","Ryan Brown",40,"C" "789","Robert Souza",35,"A" "890","Mary Fields",25,"C" ProjPay1 Ryan Brown was entered as txtEnterName. The loop will sequentially move through the records in the file until it finds a match or until EOF is encountered. The loop continues as long as there is not a match and it is not EOF. Each pass through the loop brings in a record. When there is a match the loop ends and the last input is the name we are looking for.

8 Do While Condition ? Execute the statements in the loop True Execute the statements following the loop False Syntax: Do While condition VB statements Loop

9 Private Sub cmdFind_Click() Dim IdNo As String, Name As String Dim Hrs As Integer, PayCode As String Dim wrkPay As Single Do While Name <> txtEnterName.Text And Not EOF(1) Input #1, IdNo, Name, Hrs, PayCode Loop If Name = txtEnterName.Text Then If PayCode = "A" Then wrkPay = Hrs * 10 ElseIf PayCode = "B" Then wrkPay = Hrs * 15 ElseIf PayCode = "C" Then wrkPay = Hrs * 20 Else wrkPay = Hrs * 0 End If txtPay.Text = Format(wrkPay, "Currency") End If Close #1 End Sub ProjPay1 "123","Ann Alden",25,"A" "234","John Adams",35,"B" "345","Susan Smith",40,"A" "456","Linda Costa",36,"B" "567","Donald Page",28,"B" "678","Ryan Brown",40,"C" "789","Robert Souza",35,"A" "890","Mary Fields",25,"C" If a match was found the loop ended. The loop can also end because EOF was reached. The IF statement checks to make sure that the match was found and then processes. No processing is done if the match was not found. The inner IF statement checks the PayCode and calculates the wrkPay depending on the code. At the end of the inner IF which checks the PayCode, the wrkPay is formatted as currency and moved to a text box.

10 Option Explicit Private Sub cmdClear_Click() txtEnterName.Text = "" txtPay.Text = "" Open "A:\Nametest.txt" For Input As #1 txtEnterName.SetFocus End Sub Private Sub cmdEnd_Click() Close #1 End End Sub Private Sub cmdFind_Click() Dim IdNo As String, Name As String Dim Hrs As Integer, PayCode As String Dim wrkPay As Single Do While Name <> txtEnterName.Text And Not EOF(1) Input #1, IdNo, Name, Hrs, PayCode Loop If Name = txtEnterName.Text Then If PayCode = "A" Then wrkPay = Hrs * 10 ElseIf PayCode = "B" Then wrkPay = Hrs * 15 ElseIf PayCode = "C" Then wrkPay = Hrs * 20 Else wrkPay = Hrs * 0 End If txtPay.Text = Format(wrkPay, "Currency") End If Close #1 End Sub Private Sub Form_Load() Open "A:\Nametest.txt" For Input As #1 End Sub ProjPay1 Note that in this program, I/O is handled in the following ways: Open is done with the form load. Close is done after the search that is done in cmdFind. Open is done when the screen is cleared. Close is done when cmdEnd is executed.

11 Option Explicit Private Sub cmdClear_Click() txtEnterName.Text = "" txtPay.Text = "" Open "A:\Nametest.txt" For Input As #1 txtEnterName.SetFocus End Sub Private Sub cmdEnd_Click() Close #1 End End Sub Private Sub cmdFind_Click() Dim IdNo As String, Name As String Dim Hrs As Integer, PayCode As String Dim wrkPay As Single Do Until Name = txtEnterName.Text Or EOF(1) Input #1, IdNo, Name, Hrs, PayCode Loop Rem The Until can either be with the Do or with Rem the Loop clause Rem Do Rem Input #1, IdNo, Name, Hrs, PayCode Rem Loop until Name = txtEnterName.Text Or EOF(1) If Name = txtEnterName.Text Then If PayCode = "A" Then wrkPay = Hrs * 10 ElseIf PayCode = "B" Then wrkPay = Hrs * 15 ElseIf PayCode = "C" Then wrkPay = Hrs * 20 Else wrkPay = Hrs * 0 End If txtPay.Text = Format(wrkPay, "Currency") End If Close #1 End Sub Private Sub Form_Load() Open "A:\Nametest.txt" For Input As #1 End Sub ProjPay2 The Do Until executes the loop until the until clause becomes true true as opposed to the Do While which executes the loop while the condition is true. Note that the until can be coded with the Do or with the Loop.

12 Condition ? Execute the statements in the loop True Execute the statements following the loop False Syntax: Do VB statements Loop Until condition is true Do Until

13 Option Explicit Private Sub cmdClear_Click() txtEnterName.Text = "" txtPay.Text = "" Open "A:\Nametest.txt" For Input As #1 txtEnterName.SetFocus End Sub Private Sub cmdEnd_Click() Close #1 End End Sub Private Sub cmdFind_Click() Dim IdNo As String, Name As String Dim Hrs As Integer, PayCode As String Dim wrkPay As Single Do Until Name = txtEnterName.Text Or EOF(1) Input #1, IdNo, Name, Hrs, PayCode Loop Rem The Until can either be with the Do or with Rem the Loop clause Rem Do Rem Input #1, IdNo, Name, Hrs, PayCode Rem Loop until Name = txtEnterName.Text Or EOF(1) If Name = txtEnterName.Text Then Select Case PayCode Case "A" wrkPay = Hrs * 10 Case "B" wrkPay = Hrs * 15 Case "C" wrkPay = Hrs * 20 Case Else wrkPay = Hrs * 0 End Select txtPay.Text = Format(wrkPay, "Currency") End If Close #1 End Sub Private Sub Form_Load() Open "A:\Nametest.txt" For Input As #1 End Sub ProjPay3 Select Case PayCode means that the PayCode field from the record on the file will be checked. The valid cases are where the PayCode is an A, or a B, or a C. For any other Code, the pay will be calculated using a 0 times the hours worked.


Download ppt "Delivery and other DO Examples Please use speaker notes for additional information!"

Similar presentations


Ads by Google