Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
PrArith Private Sub cmdCalculate_Click() Dim wknum1 As Single, wknum2 As Single, wkcalc As String Rem Process two numbers with +, -, *, or / and show the answer. picShow.Cls Call GiveDirections picShow.Print wknum1 = InputBox("Enter first number", "Entry 1",, 2000, 2000) wknum2 = InputBox("Enter second number - do not enter 0 if divide", "Entry 2",, 2000, 2000) wkcalc = InputBox("Enter +, -, *, /", "Entry process",, 2000, 2000) If wkcalc = "+" Then picShow.Print "The sum of"; wknum1; _ "and"; wknum2; "is"; wknum1 + wknum2 Else If wkcalc = "-" Then picShow.Print "The difference between"; wknum1; _ "and"; wknum2; "is"; wknum1 - wknum2 Else If wkcalc = "*" Then picShow.Print "Multiplying"; wknum1; _ "and"; wknum2; "results in"; wknum1 * wknum2 Else picShow.Print "Dividing"; wknum1; _ "by"; wknum2; "results in"; wknum1 / wknum2 End If End Sub Dim defines work variables to receive numbers as single and the variable to receive the sign as string. Rem is for remarks and picShow.Cls clears the picture box. Call will execute the routine GiveDirections and then return to continue processing.,, is where a default could be entered and 2000 are the x and y positions for the box. The line continuation character is the underscore preceded by a space. The If statement tests the input processing character entered in the third InputBox and stored as wkcalc.
PrArith Explains the InputBox. To Add a Procedure to your code, select Tools and then Add Procedure. A box will appear to allow you to name the procedure and specify certain features. Private Sub GiveDirections() End Sub This code is generated when you click OK.
PrArith Private Sub cmdCalculate_Click() Dim wknum1 As Single, wknum2 As Single, wkcalc As String Rem Process two numbers with +, -, *, or / and show the answer. picShow.Cls Call GiveDirections picShow.Print … End Sub Private Sub GiveDirections() Rem Explain the user input that is needed. picShow.Print "This program does a calculation." picShow.Print "Enter one number in the first input box." picShow.Print "Enter a second number in the second input box." picShow.Print "Do not enter 0 as the second number if dividing." picShow.Print "Pick +, -, *, / to pick the type of calculation." End Sub Call GiveDirections goes to the give direction subroutine and executes. When the code has been executed, it returns and starts processing with picShow.Print.
Private Sub cmdExit_Click() Call QuitMessage End End Sub Private Sub QuitMessage() Dim wkans As String picShow.Print "You have chosen to end processing." wkans = MsgBox("Processing will end", vbOKOnly, "Exit Box") End Sub PrArith When the Exit button is clicked, cmdExit_Click() is executed. The first thing is to Call QuitMessage. QuitMessage adds a line to the picture box and puts up a message box. When the user clicks OK on the message box, control returns to cmdExit_Click and the next command is executed. Since that is the End command, processing stops.
PrArith The main form appears in the center of the screen because of this setting.
PrArith When you run, the first screen appears. Clicking the Calculate button brings up the instructions and the request to enter the first number. I entered 17.
PrArith
The result is displayed. This ends the Calculate. The Exit button was clicked. A message is written in the picture box and the message box is displayed on the screen.
Math
projCP1 Three numbers are keyed in and added. The answer is stored in wrkAnswer. If wrkAnswer is over 100 than the subroutine Over100 is called and two variables are passed to it. One wrkAnswer and the other is an empty variable called wrkResult which will receive an answer from the called subroutine and pass it back when the processing in the subroutine is complete. It wrkAnswer is not over 100 than the subroutine NotOver100 is called and the same information is passed to it. After the call, the data is put on the form. In the subroutines subResult is calculated and when the routine is complete it will be passed back. wrkResult was passed back and is now going to the form.
projCP1 Private Sub cmdCalculate_Click() Dim wrkAnswer As Single, wrkResult As Single wrkAnswer = Val(txtNum1) + Val(txtNum2) + Val(txtNum3) If wrkAnswer > 100 Then Call Over100(wrkAnswer, wrkResult) Else Call NotOver100(wrkAnswer, wrkResult) End If txtAnswer = wrkAnswer txtResult = wrkResult End Sub Private Sub Over100(subAnswer As Single, subResult As Single) subResult = subAnswer * 1.05 End Sub Private Sub NotOver100(subAnswer As Single, subResult As Single) subResult = subAnswer * 1.1 End Sub wrkAnswer = = is not greater than 100 so NotOver100 is called and 57 is passed to it as wrkAnswer. wrkResult contains nothing. subAnswer * 1.1 = 57 *1.1 = 62.7 subResult is passed back and will be received as wrkResult are shown on the form
projCP1 Private Sub cmdCalculate_Click() Dim wrkAnswer As Single, wrkResult As Single wrkAnswer = Val(txtNum1) + Val(txtNum2) + Val(txtNum3) If wrkAnswer > 100 Then Call Over100(wrkAnswer, wrkResult) Else Call NotOver100(wrkAnswer, wrkResult) End If txtAnswer = wrkAnswer txtResult = wrkResult End Sub Private Sub Over100(subAnswer As Single, subResult As Single) subResult = subAnswer * 1.05 End Sub Private Sub NotOver100(subAnswer As Single, subResult As Single) subResult = subAnswer * 1.1 End Sub wrkAnswer = = is greater than 100 so Over100 is called and 200 is passed to it as wrkAnswer. wrkResult contains nothing. subAnswer * 1.05 = 200 *1.05 = 210 subResult is passed back and will be received as wrkResult are shown on the form
PrAdrProc cmdBreak calls SplitName and passes it the wkadr that was keyed in on the InputBox. In SplitName it is called subwkadr and is again defined as a string. I then use functions to break the name down into parts and display the parts on the form. Note that this information is put into the boxes in the subroutine. I could have passed back three additional items and put them in the form from cmdBreak.
PrAdrProc When Break Up Street Address is clicked, the InputBox appears. I keyed in an address following the instructions. Debugging boxes. The first shows. The first shows the location of the first space. The second shows the length of the street name. The breakup of the name.
PrAdrProcFunc Call SplitName(wkadr, wkstnum, wkstnam, wksttype) calls a procedure named SplitName and passes it the keyed in address as wkadr and empty variables that will receive data in the procedure. CombineName is a function that is passed wkstnum, wkstnam, wksttype. The results of the function are placed in txtWhole.
PrAdrProcFunc Subwkadr receives data from wkadr. The other fields defined after SplitName will hold the results of breaking up the address and will return the components to cmdBreak when the called procedure has finished executing.
PrAdrProcFunc txtWhole.Text = CombineName(wkstnum, wkstnam, wksttype) The statement above sends wkstnum, wkstname and wksttype to the function CombineName. They are then defined as fnwkstnum, fnwkstnam and fnwksttype with string type. CombineName is also a string type. After combining the name in funcName, funcName is assigned to CombineName. The function is now done and CombineName contains the combined address. Back in cmdBreak, CombineName is assigned to txtWhole on the form.
Private Function NewFunction() End Function