Visual Basic 6.0

Introduction Of Visual Basic

What is Visual Basic?

VISUAL BASIC is a high level programming language which evolved from the earlier DOS version called BASIC. BASIC means Beginners' All-purpose Symbolic Instruction Code. It is a relatively easy programming language to learn. The code looks a lot like English Language. Different software companies produced different versions of BASIC, such as Microsoft Q BASIC, QUICK BASIC, GWBASIC ,IBM BASICA and so on.
However, people prefer to use Microsoft Visual Basic today, as it is a well developed programming language and supporting resources are available everywhere. Now, there are many versions of VB exist in the market, the most popular one and still widely used by many VB programmers is none other than Visual Basic 6 . We also have VB.net, Visual Basic 2005, Visual Basic 2008 and the latest Visual Basic 2010 . Both Vb2008 and VB2010 are fully object oriented programming (OOP) languages.

Visual Basic, derived from the Basic language, is an object based and even-driven programming language from Microsoft. This language is relatively easy to learn. It enables you to create GUI (graphical user interface) applications easily using the Rapid Application Development (RAD) technique. The one most interesting feature of this language is , it comes with a designer called Integrated Development Environment (IDE). The easy-to-use tools of the IDE enable you to create buttons, textbox, and other controls for your applications with minimum efforts.


What programs can you create with Visual Basic 6?
With VB 6, you can create any program depending on your objective. For example, if you are a college or university lecturer, you can create educational programs to teach business, mathematics, science, economics, engineering, computer science, accountancy , financial management, information system and more to make teaching more effective and interesting. For example, you can create mathematical programs such as Geometric Progression, Quadratic Equation Solver, Simultaneous Equation Solver ,Prime Number, Factors Finder, Quadratic Function Graph Plotter and so on. For science teacher, you can create simulation programs such as Projectile, Simple Harmonic Motion, Star War etc. If you are in business, you can also create business programs such as inventory management system , Amortization Calculator , investments calculator, point-of-sale system, payroll system, accounting program and more to help manage your business and increase productivity. For those of you who like games , you can create those programs such as slot machine, reversi, tic tac toe and more. Besides, you can create multimedia programs such as Smart Audio Player, Multimedia Player and so on. Indeed, there is no limit to what program you can create !

History of Visual Basic:

Table : Versions of Visual Basic with release years.


Versions                 Year of release
Visual basic  1.0                                  1991
Visual basic  2.0                                  1992
Visual basic  3.0                                  1993
Visual basic 4.0                                   1995
Visual basic 5.0                                   1997
Visual basic 6.0                                   1998

Power of Visual Basic 6.0

Visual Basic 6.0 is a very powerful programming language. It enables GUI application development, access to databases using Data Access Objects, Remote Data Objects or ActiveX Data Objects, and enables the creation of ActiveX controls. Visual Basic supports API programming that lets you handle the windows operating system.

You can develop simple GUI program, large and complex commercial applications, applications for enterprise solutions and internet based applications.


Feature Of visual basic 6.0 

1. Learning Consists of all necessary tools required to build main stream Windows Applications.

2. Professional Includes advanced features such as tools to develop ActiveX and Internet controls.

Guo Interface: - VB is a Graphical User Interface language. This means that a VB program will always show something on the screen that the user can interact with to get a job done.

Modularization: - 

It is considered good programming practice to modularize your programs. Small modules where it is clearly indicated what comes into the module and what goes out makes a program easy to understand.

Object Oriented: - 

Object Oriented Programming is a concept where the programmer thinks of the program in "objects" that interact with each other. Visual Basic forces this good programming practice.


Debugging: - 

Visual Basic offers two different options for code debugging:- Debugging Managed Code Runtime Debugger The Debugging Managed Code individually debugs C and C++ applications and Visual Basic Windows applications. The Runtime Debugger helps to find and fix bugs in programs at runtime.


Data Access Feature: - 

 By using data access features, we can create databases, scalable server-side components for most databases, including Microsoft SQL Server and other enterprise-level database.


Macros IDE: - 

The Macros integrated development environment is similar in design and function to the Visual Studio IDE. The Macros IDE includes a code editor, tool windows, the properties windows and editors.



Starting Visual Basic:

Start a New Project:

If you are a programming learner, you're surely much advanced in knowledge about the basic computer operations than the general users. So I'm not telling you every ins and out of opening the software and so on. Run the Visual Basic software from list of programs or a desktop shortcut icon. A window (see picture below) will appear.

Click "Standard EXE". Start a standard .exe type of project. In the beginner level, you will only learn about this type of project.

Other project types on this window are for the advanced learners. You can do a lot of things implying VB6 is giving you enough power in your hand through these different project types.


The Integrated Development Environment:

After this, the main workspace appears where you will develop your application with the tools in IDE (Integrated Development Environment).

It is very important to know the names of all the elements of this development environment. The tools available here makes it very easy for you to develop an application. The VB6 IDE provides you many tools in one place. You can add a control on the form of your choice, set a property of an object from the Properties Window on the right hand side, set the form layout and many more things that you can use alongside your coding. You can even fill the ToolBox with lots of additional controls. 

View Code Windo:

Double-click on form or any control on the form or click "view code" icon in explorer window to view the code window.



Save your project:

After developing your application, save your project in order to modify or improve it later, or make an executable file with a few clicks. The project, form and module are saved in .vbp, .frm and .bas extensions respectively.

Click on "Make Project1.exe", it will make an executable file that will run like any other software.


The Integrated Development Environment:


The Integrated Development Environment (IDE), a great software development environment for the programmers, along with some short descriptions of the IDE elements.

The Menu Bar

The Menu Bar contains all the menus such as File, Edit, View, Tools and so on. You can learn about the menus by yourself.

The Tool Bar


The Tool Bar contains all the tools such as Open, Save, Copy, Cut, Start( to run a program) and so on.


The Tool Box

Toolbox contains the visual basic controls. You can also add other ActiveX controls to enhance your program.


How to place controls on the form ?

1. Select a control from Toolbox, click on form and drag until you have got the shape of the control you want.

2. Alternatively, you may double-click any control to add to form.

After adding a control to the form, u need to set its property and then write code for the control to work how you want.

There are two ways to set property

1. You can set property in Design Time from the Properties Window.

2. Or, you may wish to set property at run time by writing code.


Writing the Code for a Control:

Simply double-click the control (which is on the form) to view the code window and write code to specify how this control will work.

NOTE : 

1. Pointer is not a control. Click this icon to select controls already on the form.
2. All controls are object
3. Form is an object, but it is not a control. 

The Properties Window:

From properties window, you can set properties for controls.
(See in the Picture) 'Caption' is the property of the Form object.
'Form1' is the value of the property.
In the same way, 'Appearance' is the property. '1-3D' is the value.
In the Properties Window, notice the help information about the object. This helps in learning new properties.

(See Picture) Help information for the 'Caption' property is shown.

The Project Explorer Window

Press Ctrl+R if this window is not showing.

The Project Explorer Window gives you a view of the modules or forms which are contained in your VB application. You can switch from one form to another or from one module to another from the Project Explorer Window. You can view the code window of a particular form or module as well.


The Code Window

You need the Code Window to write code that will specify the behavior of the forms and the objects.
Remember that the Form is an object.

The Form Layout Window

The Form Layout Window shows where on the screen the form will be displayed when the program will be executed. Simply drag on it so that the form appears on the position where you want.

The Immediate Window

Press Ctrl+G to show the Immediate Window.

The Immediate Window helps in debugging your program by displaying the current values of variables or expressions in a certain line of your code.


The Object Browser

Press F2 to show the Object Browser Window. It is very useful because you can learn about all the methods, functions, properties and events of the objects. If you want to know about any property, method, event, function etc, simply search in the Object Browser.


An Overview of controls


Before getting into the actual programming work, you need to have an overview of the VB controls. In this lesson, a very short description of each of the controls has been given.

ToolBox

Label

The label control is used to display text. It is also used to label other controls. The end user cannot edit the label text.


TextBox

The TextBox control contains characters. End-users can edit the characters contained in the TextBox.

CommandButton

The CommandButton control is simply a button that we see in our daily-use software. When the end-user clicks the CommandButton, the program behaves according to the code assigned in the CommonButton.

Option Button

This control enables the end-user to select one among several options. Only one option button among others in a group can be on at the same time. You can name an option using the Caption property.

CheckBox

The CheckBox control is used to make a yes/no or true-false selection. You can check more than one CheckBox at the same time that let you make multiple choices. You can label this control using the Caption property.

VscrollBar & HscrollBar

VscrollBar and HscrollBar controls let you create Vertical scroll bar and Horizontal scroll bar respectively.

Frame

The Frame control is used as a container of other controls. This is also used to group different controls especially in Option Button controls when you wish to select more than one option. The Caption property associated with it is useful to label the frame.

PictureBox & Image

These controls are used to display images (e.g company logo). The supported picture formats are BMP, DIB (bitmap), ICO (icon), CUR (cursor), WMF (metafile), EMF (enhanced metafile), GIF and JPEG. But PNG format is not supported.

ListBox & ComboBox

The ListBox control contains a number of items. The user can select one or more items from the list.
The comboBox control has the feature of ListBox and TextBox. This control does not support multiple selections.
DriveListBox, DirListBox & FileListBox
These controls are often used together to perform file related tasks like opening or selecting files that are stored in the secondary memory.

Timer

The Timer control is not visible on the form when you run the program. It is used to execute lines of code repeatedly at specific intervals.

Shape & Line

These controls do not raise events. Shape and Line are used to draw line, rectangle, circle etc on the form.

The Data Control

The Data control is used for database programming.
OLE(Object Linking & Embedding)
You can connect other programs to your application that you have developed.

Apart from them, there are many other controls provided by the Visual Basic language which will be discussed in the appropriate chapters. You can add external ActiveX controls that will enhance the interface and functionality of your program.

First Visual Basic Program

This is my first Visual Basic program, an easy program to introduce VB programming.

Step 1: Start a .exe project. 

Step 2: Place a Command Button on the form from Toolbox.

Step 3: Click on Form and edit the caption property of the form. Write "My First VB Program".

Step 4: Edit the caption property of the Command Button from the Properties Window. Write "Print".

Step 5: Double-Click the CommandButton to open the Code Window. Write the following code.

Write code between the two lines 'Private Sub Command1_Click()' and 'End Sub'. These two lines will be created automatically after double-clicking the CommandButton. 

Explanation
'Sub' means Sub-Routine or Sub-Procedure or Function. 'Command1_Click()' is the sub-procedure name where 'Command1' is a control and 'Click' is an event. '( )' this sign indicates that 'Command1_Click()' is a function. 'Private' is the Scope. You will learn about scope in the next chapters. 'Print' is a Method.
You could also write Form1.print where Form1 is an object and print is its method. 'End Sub' indicates the end of the sub-procedure. You will later learn about sub-procedures.
Now you are done !
Press F5 to run the program or click the start icon button from the toolbar. The code will be executed and the string "Welcome to my first Program" will be displayed on the form as shown in the picture below.


Writing comments:

Comments are the lines of text that are not executed but used for the advantage of the programmers. Comments are written so that the other programmers can easily understand your program and you can better understand the code in case that it becomes complex or 16px. When you write comments, it becomes easy to maintain the code of your application. Commenting is a part of Documentation and it is a good practice to write comments.

How to write comments:

Comments are written using the apostrophe ( ' ). That means if you write anything after apostrophe that becomes your comment and text color of the comments becomes green. See the picture below.



Event Driven Programming:

Visual Basic is an event driven programming language. Before proceeding to the next chapter, it is very important to have a good concept of event driven programming.
The common events are Click, DblClick, Load, MouseMove, MouseDown, MouseUp, KeyPress, KeyUp, KeyDown, GotFocus, LostFocus etc.

When the user clicks , presses key or moves the mouse, the particular block of code of the corresponding event procedure is executed. Then the program behaves in a certain way. This is event driven programming. When you fire an event, the code in the event procedure is executed, and then visual basic does what the code in the event procedure instructs to do. For example, in the first sample program, when you click the Print button, click event is fired.

Then the code in the click event procedure gets executed.

The code instructs to print a text on the form. After that you see a text printed on the form. This is the concept of event driven programming. That means , the code is not executed from top to bottom but it works when the corresponding event procedure is invoked.

Example:
Write the following code in the DblClick event procedure of the form.

Private Sub Form_DblClick()
Print "You have double clicked"
End Sub

Output:

When you double-click on the form, the DblClick event procedure of the Form object is invoked and the code in the DblClick event procedure is executed. As a result, the code instructs to print a text on the form.

Variables and data types:

Variable

Variable is used to store value. The value of the variable may vary during the program execution.

Constant

Constant is a fixed value that does not change during the program execution. You can define your own constant to use it in your program.
Naming Rules of variables
1. A variable name must begin with an alphabet.
2. It cannot be more than 255 characters.
3. The variable name must not contain any special character like %,&,!,#,@ or $.
4. And finally, it has to be unique within the same scope.


Data Types

Visual Basic is rich in its data types. Data types are used to declare the variables. At the time of declaration, memory is allocated for the variables. Different data types are used to store different types of values.

Table: Memory storage for data types.

Data Type         Storage                   Data Type                              Storage

Byte                     1 byte                    String (variable-length)       Length + 10 bytes
Boolean               2 bytes                  String (Fixed-Length)          Length of string
Integer                2 bytes                  Currency                                8 bytes
Long                    4 bytes                  Decimal                                  12 bytes
Single                  4 bytes                  Object                                     4 bytes                                           Double                8 bytes                  Variant (numeric)                 16bytes                                  Date                    8 bytes                  Variant (text)                        length +22 bytes

 Table: Data types & their value range.

Data Type                             Value Range

Byte                                        0 to 255
Boolean                                   True/False
Integer                                    -32,768 to 32,767
Long                                       -2,147,483,648 to 2,147,483,647
Single                                      -3.402823*10^3 to -1.401298*10^45 for negative values
                                                 1.401298*10^-45  to 3.402823*10^38 for positive values
Double                                    -1.79*10^308 to -4.94*10^-324 for negative values 4.94*10^-324 to
                                                 1.79*10^308 for positive values
Date                                         January 1, 100 to December 31, 9999
String (Variable length)              0 to approximately 2 billion characters
String (Fixed length)                  1 to 65,400 characters
Currency                                 -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal                                   +,-79,228,162,514,264,337,593,543,950,335 if no decimal is used
                                               +,-7.9228162514264337593543950335 (28 decimal places)
Object                                      Any object
Variant (numeric)                       Any value as 16px as Double
Variant (text)                             Same as variable length string



Variable Declaration:

Depending on where the variables are declared and how they are declared, there are many ways to declare a variable in visual basic. When you declare a variable, memory space for the variable is reserved. This is called memory allocation. Different amount of memory space is reserved for different data types.

You can declare a variable with the 'dim' keyword.
Syntax:
Dim variable As [Type]

Example:
Private Sub cmdSum_Click()
Dim m As Integer
Dim n As Integer
Dim sum As Integer
m = 10 'm is a variable, 10 is a constant
n = 30
sum = m + n
Print "The sum is " & sum
End Sub

Output: The sum is 40

You can declare many variables in one line as follows and assign multiple variables in one line using ':' operator.

Private Sub cmdSum_Click()
Dim m As Integer, n as Integer, sum as Integer
m = 10 : n = 30
sum = m + n
Print "The sum is " & sum
End Sub
Output: The sum is 40
Implicit declaration: The Variant Data Type

If you use a variable without declaring its type, it is called a variant variable.
Example:
Dim num
Or,
Dim num As Variant

Or, if you choose not to declare a variable then also it is of the Variant data type. So you can use a variable in Visual Basic without declaring it. The variant data type can store numeric, date/time or string values. This is called implicit declaration. That means, you are declaring the variable implicitly. But it is not recommended to use implicit declaration and a good programmer will declare the variables explicitly. Because, it can lead to errors that may not be detected at run time.

Scope of a Variable:

Scope of a variable determines which part of the code can access the variable. A variable is declared in general declaration section of a module to make it available to all the procedures in the module.

Concepts of procedure and module are necessary before jumping on the scope part. So procedure and module are clarified first.

Example:

The following block of code is a procedure.

Private Sub Command1_Click()
Dim num As Integer
num = 45
Print num
End Sub

'Command1_Click()' is the procedure name. Here 'num' is a procedure-level variable. The value of the variable is destroyed when the procedure ends.

'End Sub' indicates the end of the event procedure.


What is a procedure?

A procedure is a block of Visual Basic statements enclosed by a declaration statement (Function, Sub, Operator, Get, Set) and a matching End declaration. All executable statements in Visual Basic must be within some procedure.

Calling A Procedure


You invoke a procedure from some other place in the code. This is known as a procedure call. When the procedure is finished running, it returns control to the code that invoked it, which is known as the calling code. The calling code is a statement, or an expression within a statement, that specifies the procedure by name and transfers control to it.

Types of procedure

There are three types of procedures.
1) Sub Procedure,
2) Function procedure,
3) property procedure.

Sub Procedures

Again Sub Procedures have two types :
a. Event Procedure,
b. General Procedure.

Every procedure has a unique name, a scope and a list of arguments. The function procedure, in addition, has a return value. Sub procedure and property procedure does not return value.
Scope of a procedure
A procedure can be Private, Public or Friend. You can invoke a Private procedure only from within the module. A Public procedure can be called from anywhere of the current project -- from within the module, from other modules of your application project, and in some cases from outside the program using COM. And a friend procedure can be called from anywhere of the current project but not from outside.
Understanding the Event Procedure
An event procedure does not return any value and you can call it from other procedures of the module. The procedure is called using the Call command. 

Example:
Private Sub Command1_Click()
Call Command2_Click
End Sub
____________________________________________________________ Private Sub Command2_Click()
Print "hello"
MsgBox "hello"
End Sub

So if you click the Command1 button, the Command2_Click event procedure is called, and the lines of code inside it are executed.
You may omit the Call command while calling a procedure.
Example :
Private Sub Command1_Click()
Command2_Click
End Sub
__________________________________________________________ Private Sub Command2_Click()
MsgBox "hello"
End Sub

Public event procedure

Example : Place a CommandButton on Form1. Now add a new form from the menu : Project > Add Form > Form. Place a CommandButton on Form2.
'In Form1
Private Sub Command1_Click()
Call Form2.Command1_Click
End Sub
'In form2
Public Sub Command1_Click() 'Scope is Public
MsgBox "You have called a procedure of form2"
End Sub

When you'll click the CommandButton on Form1, Command1_Click procedure of the form2 module will be called. As the scope of the procedure in Form2 is Public, the procedure is accessible from anywhere of the current project, and if the procedure is Private, the procedure cannot be invoked from other modules. In the Form2 module, if you change the procedure scope to Private, it can then only be called from the Form2 module. This aspect will be clearer when you'll learn about working with multiple forms in the next lessons.

Understanding the general sub procedure:

General sub procedure can be very useful when you want to use a block of code repeatedly in your program. Instead of writing the same lines of code again and again, define a General Sub Procedure, and call it where you need. These procedures do not return values.

Say you need to print some lines of text again and again. So define a general sub procedure xprint , and call it wherever in the program its necessary.
Note: Public is the default scope for procedures, so you may omit it.

Example:
Sub txt()
MsgBox "welcome"
End Sub

Example:
Private Sub Command1_Click()
Call xprint 'Function calling
End Sub
___________________________________________________________ Private Sub xprint() 'Function Definition
Print "Hello World"
Print "";
Print "*****"
Print "New"
End Sub
___________________________________________________________ Private Sub Command2_Click()
Call xprint 'Function calling
End Sub

Using sub procedures in your code is a good programmer's habit. Because it reduces the number of lines of code. Besides, a large complex program becomes very easy and comprehensive when you use your own sub procedure in the code.
You can change the scope to Public when you want to access the procedure from outside the module.
Public general sub procedure
Add a new form from menu : Project > Add Form > Form. Public general sub procedures are useful when want to invoke your procedure from wherever you want, from any module.

Example :
'In Form1
Private Sub Command1_Click()
Call Form2.xprint
End Sub

'In form2
Public Sub xprint()
Form2.Show
Print "Hello World !"
Print "";
Print "***"
Print "New"
End Sub
_____________________________________________________________ Private Sub Command1_Click()
Call xprint
End Sub


Understanding the function procedure:

A function procedure has a scope, a unique name, parameter list and return value. You can pass any datatype to a procedure e.g Integer, Boolean, Long, Byte, Single, Double, Currency, Date, String and Variant. Object data types and arrays are also supported. This is same for the return type values.

Difference between argument and parameter : When you pass a value to a procedure, it is argument, and the value is called parameter when it is received by the procedure. Both the terms are used for the same value.

A function procedure may not return a value.

Example: In this example, 32 and 54 are passed to the function 'sum' from Form_Load procedure.

'Function Definition
Private Function sum(n1 As Integer, n2 As Integer)
Text1.Text = n1 + n2
End Function
__________________________________________________________________ Private Sub Form_Load()
Text1.Text = ""
Call sum(32, 54) 'Function calling
End Sub
Output :



Function procedure that returns value

Example:
'Function Definition
Private Function sum(n1 As Integer, n2 As Integer) As Integer
'Returns a value
sum = n1 + n2
End Function
____________________________________________________________ Private Sub Form_Load()
Text1.Text = ""
'Function calling and assigning the returned value
Text1.Text = sum(60, 40)
End Sub
Passing arguments: By Value or By Reference
you can pass an argument either by value or by reference. Arguments are passed by value using the ByVal keyword and by reference using the ByRef keyword or by omitting any specifier.

While passing the arguments by reference, references of the variables are passed. So if the argument is passed by reference, it can be modified by the called procedure and the original value of the argument in the calling procedure will be changed. But the argument value will be unchanged if you call the procedure using constants or expressions as parameters.
Example :
'Calling procedure
Private Sub Command1_Click()
Dim a As Integer 'The value of a is 0 after declaration
Call num(a)
Print a 'Value of a is 1, modified
End Sub
___________________________________________________________________ 'Called procedure
Public Function num(ByRef x As Integer) 'You may omit ByRef
x = x + 1
End Function

On the other hand, when the arguments are passed by value, the actual values are passed. So the called procedure cannot change their original values in any way. Example :
'Calling procedure
Private Sub Command1_Click()
Dim a As Integer 'The value of a is 0 after declaration
Call num(a)
Print a 'The value of a is 0, its unchanged
End Sub
___________________________________________________________________ 'Called procedure
Public Function num(ByVal x As Integer)
x = x + 1
End Function

Note : Both Sub and Function procedures can accept parameters.
Property Procedures
A property procedure is a series of Visual Basic statements that manipulate a custom property on a module, class, or structure. Property procedures are also known as property accessory.

Visual Basic provides for the following property procedures:
· A Get procedure returns the value of a property. It is called when you access the property in an expression.

· A Set procedure sets a property to a value, including an object reference. It is called when you assign a value to the property.

You usually define property procedures in pairs, using the Get and Set statements, but you can define either procedure alone if the property is read-only (Get Statement) or write-only (Set Statement (Visual Basic)).

You can omit the Get and Set procedure when using an auto-implemented property. For more information, see Auto-Implemented Properties (Visual Basic).
You can define properties in classes, structures, and modules. Properties are Public by default, which means you can call them from anywhere in your application that can access the property's container.
Declaration Syntex:
A property itself is defined by a block of code enclosed within the Property Statement and the End Property statement. Inside this block, each property procedure appears as an internal block enclosed within a declaration statement (Get or Set) and the matching End declaration.
The syntax for declaring a property and its procedures is as follows:
Example:
[Default] [Modifiers] Property PropertyName[(ParameterList)] [As DataType]
[AccessLevel] Get
' Statements of the Get procedure.
' The following statement returns an expression as the property's value.
Return Expression
End Get
[AccessLevel] Set[(ByVal NewValue As DataType)]
' Statements of the Set procedure.
' The following statement assigns newvalue as the property's value.
LValue = NewValue
End Set
End Property
- or -
[Default] [Modifiers] Property PropertyName [(ParameterList)] [As DataType]

Data Type:

A property's data type and principal access level are defined in the Property statement, not in the property procedures. A property can have only one data type. For example, you cannot define a property to store a Decimal value but retrieve a Double value. 



To Download Their Program File and Program Source Code Visit Link Below.


Downloading Links

No comments:

Post a Comment