Sabtu, 22 Agustus 2009

2.1 Creating Your First Application

2.1 Creating Your First Application

In this section, we will not go into the technical aspects of Visual Basic programming yet, what you need to do is just try out the examples below to see how does in VB program look like:

Example 2.1.1 is a simple program. First of all, you have to launch Microsoft Visual Basic 6. Normally, a default form with the name Form1 will be available for you to start your new project. Now, double click on Form1, the source code window for Form1 as shown in figure 2.1 will appear. The top of the source code window consists of a list of objects and their associated events or procedures. In figure 2.1, the object displayed is Form and the associated procedure is Load.

Figure 2.1 Source Code Window

When you click on the object box, the drop-down list will display a list of objects you have inserted into your form as shown in figure 2.2. Here, you can see a form with the name Form1, a command button with the name Command1, a Label with the name Label1 and a Picture Box with the name Picture1. Similarly, when you click on the procedure box, a list of procedures associated with the object will be displayed as shown in figure 2.3. Some of the procedures associated with the object Form1 are Activate, Click, DblClick (which means Double-Click) , DragDrop, keyPress and more. Each object has its own set of procedures. You can always select an object and write codes for any of its procedure in order to perform certain tasks.

Figure 2.2: List of Objects

Figure 2.3: List of Procedures

You do not have to worry about the beginning and the end statements (i.e. Private Sub Form_Load.......End Sub.); Just key in the lines in between the above two statements exactly as are shown here. When you press F5 to run the program, you will be surprise that nothing shown up .In order to display the output of the program, you have to add the Form1.show statement like in Example 2.1.1 or you can just use Form_Activate ( ) event procedure as shown in example 2.1.2. The command Print does not mean printing using a printer but it means displaying the output on the computer screen. Now, press F5 or click on the run button to run the program and you will get the output as shown in figure 2.4.

You can also perform arithmetic calculations as shown in example 2.1.2. VB uses * to denote the multiplication operator and / to denote the division operator. The output is shown in figure 2.3, where the results are arranged vertically.

Example 2.1.1

Private Sub Form_Load ( )

Form1.show

Print “Welcome to Visual Basic tutorial”

End Sub

Figure 2.4 : The output of example 2.1.1

Example 2.1.2

Private Sub Form_Activate ( )

Print 20 + 10
Print 20 - 10
Print 20 * 10
Print 20 / 10

End Sub

Figure 2.5: The output of example 2.1.2

You can also use the + or the & operator to join two or more texts (string) together like in example 2.1.4 (a) and (b)

Example 2.1.4(a)


Private Sub

A = Tom
B = “likes"
C = “to"
D = “eat"
E = “burger"
Print A + B + C + D + E

End Sub

Example 2.1.4(b)

Private Sub

A = Tom
B = “likes"
C = “to"
D = “eat"
E = “burger"
Print A & B & C & D & E

End Sub


The Output of Example 2.1.4(a) &(b) is as shown in Figure 2.7.

2.2 Steps in Building a Visual Basic Application

Step 1 : Design the interface

You need to design an interface that is user-friendly and easy to navigate. Give clear instructions by typing suitable captions for the command buttons and labels. If you design the program for fun and games, it has to be reasonably attractive. You can look at the interface of my Star War game, as shown below:

Step 2 : Set properties of the controls (Objects)

Each Visual Basic control has its own properties. For example, a textbox has font color property, font size property, text alignment property and more. Setting properties will determine how a control looks and behaves. Therefore, we need to set suitable properties for a control so that the program will run as intended to be. You will more about properties in the next chapter

Step 3 : Write the event procedure

The final and the most difficult step is to write the event procedure. An event procedure consists of VB code that control how a VB event executes. We have to write code for any possible event trigger by the user, like clicking the button or pressing a key on the keyboard. Although writing code is not easy, fortunately Visual Basic programming language is the easiest to master amongst all the programming languages. It is English-like so if you know English you should have no problem in mastering the language. We will guide you step by step on writing the VB code in the coming chapters.

1.1 The concept of computer programming

Before we begin Visual Basic 6 programming, let us understand some basic concepts of programming. According to Webopedia, a computer program is an organized list of instructions that, when executed, causes the computer to behave in a predetermined manner. Without programs, computers are useless. Therefore, programming means designing or creating a set of instructions to ask the computer to carry out certain jobs which normally are very much faster than human beings can do.


Most people think that computer CPU is a very intelligent thing, which in actual fact it is a dumb and inanimate object that can do nothing without human assistant. The microchips of a CPU can only understand two distinct electrical states, namely, the on and off states, or 0 and 1 codes in the binary system. So, the CPU only understands a combinations of 0 and 1 codes, a language which we called machine language. Machine language is extremely difficult to learn and it is not for us laymen to master it easily. Fortunately , we have many smart programmers who wrote interpreters and compilers that can translate human language-like programs such as BASIC into machine language so that the computer can carry out the instructions entered by the users. Machine language is known as the primitive language while Interpreters and compilers like Visual Basic are called high-level language. Some of the high level programming languages beside Visual Basic are Fortran, Cobol, Java, C, C++, Turbo Pascal, and more. Among the aforementioned programming languages, Visual Basic is the most popular. Not only it is easily to learn because of its English-like syntaxes, it can also be Incorporated into all the Microsoft office applications such as Microsoft words, Microsoft Excel, Microsoft PowerPoint and more. Visual Basic for applications is known as VBA. If you wish to know more about VBA, please visit our tutorial at: Excel VBA Tutorial
1.2 What programs can you create with Visual Basic 6?

With VB 6, you can create any program depending on your objective. For example, you can create educational programs to teach science , mathematics, language, history , geography and so on. You can also create financial and accounting programs to make you a more efficient accountant or financial controller. For those of you who like games, you can create those programs as well. Indeed, there is no limit to what program you can create ! There are many such programs in this tutorial, so you must spend more time on the tutorial in order to learn how to create those programs. If you wish to view some of the sample programs, you can take a look at the link below:

VB Sample Programs
1.3 The Visual Basic 6 Integrated Development Environment

Before you can program in VB 6, you need to install Visual Basic 6 in your computer. If you do not own a copy of Visual Basic 6 software yet , you can purchase it from Amazon.com by clicking the link below:

Microsoft Visual Basic 6.0 Professional

Basically any present computer system should be able to run the program, be it a Intel Pentium II, Intel Pentium III, Intel Pentium IV or even AMD machines, Visual Basic 6 can run without any problem. It might not be true for VB2008, older machines might not be able to run VB2008 as it takes up much more resources, therefore I still prefer VB 6 as it is light and easy to program. It is still very useful and powerful, and I am happy to know that Microsoft Windows Vista can support VB 6. However, if you prefer to learn VB 2008, you can refer to our VB 2008 Tutorial.

On start up, Visual Basic 6.0 will display the following dialog box as shown in Figure 1.1. You can choose to either start a new project, open an existing project or select a list of recently opened programs. A project is a collection of files that make up your application. There are various types of applications that we could create, however, we shall concentrate on creating Standard EXE programs (EXE means executable program). Now, click on the Standard EXE icon to go into the actual Visual Basic 6 programming environment.



Figure 1.1