Sunday, July 26, 2009

New lesson for Excel VBA Tutorial

I have just added a new chapter to our Excel VBA Tutorial. This new chapter discusses what is a sub procedure and how to write the code. Please check the link above to view the chapter contents.

Sunday, July 19, 2009

The Trigonometric Functions

I have just Updated lesson 11 with some trigonometric functions.

The common trigonometric functions are Sin, Cos, Tan and Atn.

a) Sin is the function that computes the value of sine of an angle in radian.

b) Cos is the function that computes the value of cosine of an angle in radian.

c) Tan is the function that computes the value of tangent of an angle in radian.

d) Atn is the function that computes the value of arc tangent of an angle in radian.

An angle in degree has to be converted to radian before it can be calculated by the above trigonometric functions. From high school mathematics, we know that π radian is equivalent to 180°; which means 1 radian is equivalent to π divided by 180. Therefore, in order to convert an angle x from degree to radian, we have to multiply x by (π/180). However, there is a small problem because it is rather difficult to obtain the precise value of π, fortunately, there is a way to do it in VB. First of all, we know that an arc tangent of 1 will return the value of 45° which is π/4 radian. So, to obtain the value of π, just multiply the arc tangent of 1 with 4. Let’s examine how all the above calculations can be done in the following example:

HTML clipboard

In this example, the program will display the values of sine, cosine and tangent for various angles in degree between 0° and 360° in a table form . The value of π is obtained using the equation pi=4*Atn(1). The angle in degree is converted to radian by multiplying the angle by (π/180). Different angles are obtained through the use of For...Next Loop. The program is shown below

Private Sub Form_Activate ()

pi = 4 * Atn(1)

Print "angle", "Sin x", "Cos x", "Tan x"

For degree = 0 To 360 Step 30

angle = degree * (pi / 180)

Print degree, Round(Sin(angle), 4), Round(Cos(angle), 4), Round(Tan(angle), 4)

Next degree

End Sub


The Output



Saturday, July 11, 2009

Scientific Calculator

In previous post, I have discussed about a new project , i.e. creating scientific calculator. Now, we shall add the number buttons, a cancel button and a dot button. The number buttons are created as an array of buttons, and each button will be identified with its index, corresponding to its caption.

I will also replace the display panel with a label so that the users won't be able to erase the number. They can only clear the number by clicking the cancel button. The alignment property of the label is set to right justified, so that the digit entered will start from the right position, similar to the conventional calculator.

The value of pi is now obtained by using the Atn function (Arc Tangent). We know that Tan(pi/4)=1, so Atn(1)=pi/4, therefore pi=Atn(1)*4. By using this function, we will obtain a more accurate value of pi.

In this project, we shall also limit the numbers to be less than 30 digit. If any user enters a number with more than 30 digits, an error message "data overflow" will be shown.

The Interface:


The code :

Dim Num_of_digit As Integer
Private Sub buttonNum_Click(Index As Integer)
If Num_of_digit > 0 And Num_of_digit <> "0" Then
panel.Caption = panel.Caption + Right$(Str$(Index), 1)
ElseIf panel.Caption = "0" Then
panel.Caption = Str$(Index)
End If

ElseIf Num_of_digit >= 30 Then
panel.Caption = "Data Overflow"

Else

panel.Caption = Right$(Str$(Index), 1)

End If

Num_of_digit = Num_of_digit + 1



End Sub

Private Sub Clear_Click()

panel.Caption = "0"
Num_of_digit = 0

End Sub

Private Sub Command1_Click(Index As Integer)
Dim x As Double
Dim pi As Double
pi = Atn(1) * 4
Select Case Index

Case 0
x = Val(panel.Caption)
panel.Caption = Round((Sin(x * pi / 180)), 4)
Num_of_digit = 0

Case 1
x = Val(panel.Caption)
panel.Caption = Round((Cos(x * pi / 180)), 4)
Num_of_digit = 0
Case 2
x = Val(panel.Caption)
panel.Caption = Round((Tan(x * pi / 180)), 4)
Num_of_digit = 0

End Select

End Sub

Friday, July 3, 2009

New Project-Creating a Scientic Calculator

Creating a scientific calculator is not an easy task in Visual Basic 6. However, you don't have to worry. I will start with a simple interface and gradually we will progress to a more complex interface, and then a fully functional scientific calculator.So, please follow my blog as often as possible for updates on this project. Meanwhile, if you have any idea about the calculator, please share with us.

Right now, take a look at the simple interface.

The code :

Private Sub Command1_Click(index As Integer)
Select Case index
Dim x As Double
Case 0
x = Val(Text1.Text)
Text1.Text = Round((Sin(x * 3.14159265 / 180)), 4)
Case 1
x = Val(Text1.Text)
Text1.Text = Round((Cos(x * 3.14159265 / 180)), 4)
Case 2
x = Val(Text1.Text)
Text1.Text = Round((Tan(x * 3.14159265 / 180)), 4)

End Select

End Sub