Introduction to Data Types and Data Type Conversion (Article)

Category:
Miscellaneous
Type:
Snippets
Difficulty:
Beginning

Author: David Nishimoto

Version Compatibility: Visual Basic 5

More information:
For beginners.

Instructions: Copy the declarations and code below and paste directly into your VB project.

Few things to know:

Boolean = True or False Byte = A single ASCII character (code 0 to 128) Currency = Decimal fractions, such as dollars and cents. Date = An eight-character date. (mm/dd/yyyy) Double = Double-precision floating point numbers with 14 digits of accuracy. Integer = Whole numbers in the range -32,768 to 32,767 Long = Larger whole numbers Single = Single-precision floating point numbers with six digits of accuracy. String = Alphanumeric data; letters, digits, and other characters. Variant = Converts from one type to another, as needed

VB built-in Functions:

CDbl = Convert to Double Cint = Convert to Integer Cstr = Convert to String Clng = Convert to Long CDate = Convert to Date Chr = Convert value to ascii character Asc = Convert from ascii to value Open a New project(ctrl+N) Create 3 text boxes in the center of your form one over the other. Edit the top text box properties(F4) to: Name: txtValue1 Maxlength: 3 Text: Edit the middle text box properties(F4) to: Name: txtValue2 Maxlength: 3 Text: Edit the bottom text box properties(F4) to: Name: txtResult Maxlength: 10 text: Create 3 lables. One to the left of each text box. Select the top lable and edit it's properties(F4) to: Name: lblValue1 Alignment: Right Caption: Value1 Select the center lable and edit it's properties(F4) to: Name: lblValue2 Alignment: Right Caption: Value2 Select the last lable and edit it's properties(F4) to: Name: lblResult Alignment: Right Caption: Result Create 4 command buttons below the lables and text boxes. Select the first command button and edit it's properties(F4) to: Name: cmdAdd Caption: Add Select the second command button and edit it's properties(F4) to: Name: cmdSubtract Caption: Subtract Select the third command button and edit it's properties(F4) to: Name: cmdMultipy Caption: Multipy Select the Forth command button and edit it's properties(F4) to: Name: cmdDivide Caption: Divide Double click on your add command button. 1) In the General Declaration area type "Option Explicit" 2) Now go to the cmdAdd sub routine and enter the following code to program the Add command button to add Value1 to Value2 and place the output in the Result text box. Your code will look something like this:
Private Sub cmdadd_Click() Dim dvalue1 As Double Dim dvalue2 As Double Dim dresult As Double If DataValidation() = False Then GoTo Exit_cmdadd_Click End If dvalue1 = CDbl(txtvalue1) dvalue2 = CDbl(Txtvalue2) dresult = dvalue1 + dvalue2 txtresult = CStr(dresult) Exit_cmdadd_Click: End Sub
1) To make sure that the user enters numeric imput your program you will need to check the input. 2) If the input is not numeric the following sub routine will detect an input error. Private Function DataValidation() As Boolean Dim bReturnValue As Boolean bReturnValue = True If IsNumeric(txtvalue1) = False Then txtvalue1.BackColor = vbRed MsgBox "Please enter a number for value1" txtvalue1.BackColor = vbWhite bReturnValue = False DataValidation = bReturnValue Exit Function End If If IsNumeric(Txtvalue2) = False Then Txtvalue2.BackColor = vbRed MsgBox "Please enter a number for value2" Txtvalue2.BackColor = vbWhite bReturnValue = False DataValidation = bReturnValue Exit Function End If DataValidation = True End Function
Now go to the object combobox at the top and select cmdSubtract and enter the following in the sub routine: Private Sub cmdsubtract_Click() Dim dvalue1 As Double Dim dvalue2 As Double Dim dresult As Double If DataValidation() = False Then GoTo Exit_cmdadd_Click End If dvalue1 = CDbl(txtvalue1) dvalue2 = CDbl(Txtvalue2) dresult = dvalue1 - dvalue2 txtresult = CStr(dresult) Exit_cmdSubtract_Click: End Sub
Agian go to the object combobox at the top to the screen and select cmdmultiply and enter the following in your sub routine: Private Sub cmdmultipy_Click() Dim dvalue1 As Double Dim dvalue2 As Double Dim dresult As Double If DataValidation() = False Then GoTo Exit_cmdadd_Click End If dvalue1 = CDbl(txtvalue1) dvalue2 = CDbl(Txtvalue2) dresult = dvalue1 * dvalue2 txtresult = CStr(dresult) Exit_cmdMultiply_Click: End Sub
Once more to to the object combobox at the top of the screen ad select cmdDivide and enter the following in your sub routine: Private Sub cmddivide_Click() Dim dvalue1 As Double Dim dvalue2 As Double Dim dresult As Double If dvalue2 = 0 Then dresult = 0 Else dresult = dvalue1 / dvalue2 End If If DataValidation() = False Then GoTo Exit_cmdadd_Click End If dvalue1 = CDbl(txtValue1) dvalue2 = CDbl(txtValue2) dresult = dvalue1 / dvalue2 txtResult = CStr(dresult) Exit_cmdDivide_Click: End Sub
Run your program(F5) enter letters in value1 and value2 and then press a command button. You should recive an error that prompts you to enter a numeric value in the textboxes for value1 and value2. After you recive the error message replace the letters with numbers and try each command button which should execute the function that it is labeled.