This is a blog that discusses anything related to Visual Basic and provides updates for VISUAL BASIC TUTORIAL website. Your comments and suggestions are welcome here. Thank you.
Monday, October 26, 2009
Visual Basic for Business
Monday, September 21, 2009
New Lesson- Using the Printer
In previous lessons, we have only written programs that send output to the screen and not the printer. In this new lesson, we will learn how to send an output to the printer and get it printed. Sending output to the printer is a simple task in Visual Basic, it involves the use of the Printer object and the print method. The standard code of sending an output to the printer and get it printed out is as follows:
Private Sub Form_Load()
Printer.Print "Welcome to Visual Basic"
End Sub
For more details, refer to
http://www.vbtutor.net/lesson39.html
Wednesday, September 2, 2009
New Lesson!
Thursday, August 27, 2009
Keyboard Handling
Keyboard handling is relatively easy in VB6. There are basically only three events associated with the keyboard, i.e. KeyPress, KeyUp and KeyDown. You can write code for each of the aforementioned events. Before you can do that, you need to know the ASCII code as well as the chr function. ASCII stands for American Standard Code for Information Interchange. Each character or symbol on a keyboard is represented by a numerical code (ASCII code) which can be understood by the computer. It may be even an action such as Enter or Backspace. For a complete set of ASCII code, please refer to this link below:
http://www.asciitable.com/.
On the other hand, the Chr function is to convert an ASCII code into a meaningful character which can be undertood by a human being.
A standard VB code for handing keyboard is shown in the example below:
Private Sub Form_KeyPress(KeyAscii As Integer)
Print Chr(KeyAscii)
End Sub
The program will display the correponding character on the form as the user press a key on the keyboard that sends the ASCII code to the computer and the VB program convert that code into a character and display it on the form, it is that simple!
More to come.
Sunday, July 26, 2009
New lesson for Excel VBA Tutorial
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:
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
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
