I will get the simple stuff done with first, then the more difficult code. No worry though! I will try to explain the code line-by-line as before. The code involved is incredibly simple, but only because I know how. Lets begin.
You may have guessed that the End command will go into the Exit button on the form. Duh!
Open the code window by double-clicking on the Exit button and type End.
Now lets code the Clear command button.
Click on the object box in the code window and select cmdClear.
Type the following lines
txtName.Text = ""
txtAddress.Text = ""
txtCity.Text = ""
txtState.Text = ""
txtZip.Text = ""
txtSheetrock.Text = ""
txtBlueboard.Text = ""
txtSheetrockPrice.Text = ""
txtBlueboardPrice.Text = ""
lblTotalQuantity.Text = ""
lblTotalPrice.Text = ""
Just as the code says, the text and caption properties for the specified objects will be empty when cmdClear is clicked.
Select the txtState object in the object box of the code window.
Select the LostFocus event in the event box
Press tab, then type txtState.Text = UCase(txtState.Text).
This line converts the text in the text box txtState to uppercase characters using the UCase function when the cursor leaves the text box.
Now it is time to finally calculate this order form.
Double-click the command button cmdCalc to open the code window
Note: We will use a function called Val, which treats a string as a numeric value. Otherwise, the program will concatenate the string, or put them together. For instance: 2 + 12 will equal 212 if we enter it into code like it is. A string is simply text, nothing more. Anyway,
Type the following line
lblTotalQuantity.Caption = Val(txtSheetrock.Text) + Val(txtBlueboard.Text)
The code above simply states that the caption in lblTotal will equal the value of txtSheetrock.Text plus the value of txtBlueboard.Text. Test the application if you want. Make sure you only enter numbers. The Val statement only looks at numbers in a string. If you enter letters in the text boxes, it will return, or display 0.
Examples:
Val("105 Riders Lane") returns 105
Val("Riders Lane") returns 0
Now lets display some total value.
Double-click the cmdCalc button and type the following line below the line already in the code:
lblTotalPrice.Caption = Val(txtSheetrockPrice.Text) * Val(txtSheetrock.Text) + Val(txtBlueboardPrice.Text) * Val(txtBlueboard.Text)
The code above simply states that the total price label caption will equal the price entered into txtSheetrockPrice.Text, times the quantity entered into txtSheetrock.Text, plus the price entered into txtBlueboardPrice.Text, times the quantity entered into txtBlueboard.Text.
Sounds wordy huh? You're right! There is no other way to explain it though.
I hope it worked for you, because now we will change something else! Notice how the total price is not calculated in a dollar and cents format. To change this, we will use a function called Format.
Create a line below the last line of code in the cmdCalc sub-procedure and enter the following code:
lblTotalPrice.Caption = Format(lblTotalPrice.Caption, "$0,0.00")
This code tells the computer to format the label lblTotalPrice in dollars and cents format in the thousands. You can also replace the "$0,0.00" with "Currency" and it will format the same way.
Tip: Remember to make sure that you place your parenthesis in the proper places for more complex computations
Example:
Val(txtText.Text) * Val(txtText2.Text) * 5 will return something
different than (Val(txtText.Text) * Val(txtText2.Text)) *5.
(Val(txtText.Text) * Val(txtText2.Text)) * 5 multiplies the product of
both text values by 5.
Val(txtText.Text) * Val(txtText2.Text) * 5 solely multiplies
txtText2.text by 5 and multiplies that product by txtText.text.
We will deal with printing in later point in this tutorial.
Keep trying these examples and experiment with other text controls IF you are lost. Otherwise, move on to Page 7.
How do you like this tutorial? Are you learning the language easily? Please let me know via e-mail at webtechjava@yahoo.com.