Monday, October 26, 2009

Visual Basic for Business

Visual basic is a perfect programming language for building customized business applications. For example, you can design a payroll system, an inventory system, an order entry application, accounting software , financial calculator and more. All these applications involve the use of databases and Visual Basic is good at linking databases.

We are in the process of designing some business applications in VB . As soon as they are ready we will add them in our tutorial.

Happy programming.

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!

I have added another lesson to your favourite Visual Basic 6 tutorial.
The new lesson(lesson 38) discusses about keyboard handling involving the use of ASCII, which I have mentioned in the previous post of this blog.

In this lesson, you will learn how to trace keys that are being pressed by the user. Detail explannations and easy-to-understand examples are presented. To view the lesson, click the following link:

Thursday, August 27, 2009

Keyboard Handling

So far most of the programs we have done here were using the command buttons which required mouse actions such as click or double click. However, we can also write the code that can be triggered using the keyboard.

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

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