Visual Basic is a Windows programming language that has been developed at Microsoft Corporation. Visual Basic is a powerful programming language to develop sophisticated widows program very quickly. Visual Basic is one of the RAD (Rapid Application Development) tools as it enables the programmer to develop applications very easily and very quickly. It contains the same BASIC –like language that has been popular over the years and it includes all the necessary extensions required to produce widows programs.

Visual Basic applications are very popular as front end to many client/Server database systems like MS.ACCESS, SQL Server, Oracle etc.

The Integrated Development Environment

Much of the popularity of Visual Basic comes from its Integrated Development Environment or IDE for short. We can edit your Visual Basic programs using any editor, including the aged Notepad, but I never met a programmer insane enough to do that. In fact, the IDE gives you everything you need to create great applications, to write code for them, to test and fine-tune them, and, finally, to produce executable files.

                                                            Fig 1.1




Above shown screen of Visual Basic shows a Standard EXE Project with following components.


  • Pull down Menu: - Provides access to every available visual basic commands.

                                                Fig 1.2

  • Toolbar: - Displays icons that represent the most commonly used visual basic commands.

                                          Fig 1.3

  • Toolbox: - Displays the type of Objects such as (Command Button, Checkbox) that you can draw on a form.
  • Project Explorer: - Lists all the files that make up a single visual basic program such as project, form modules, class modules , user controls etc.
  • Properties Window: - Displays the properties of the currently selected form or object.
  • Form Layout Window: - Enables you to arrange the location where your forms appear on the screen.
  • Form: - Provides a window you can draw objects to design your program’s User Interface.
  • Immediate Window: - Enables you to debug your visual basic program.








The Toolbox


When we design our interface we use the Toolbox to draw objects on a form, following section gives a brief description of various tool available on toolbox along with their functions.




Fig 1.4


The Visual basic toolbox contains the tools we use to draw controls on our forms.

Each tool in the toolbox represents a control


·         Pointer: - The pointer provides the way to move and resize forms and controls(Note: this is not a control)

·         Picture Box: - The Picture box control displays bitmaps, icons, windows Metafiles; it displays text or acts as a visual container for other controls.

·         Label: - the label control displays text a user can not interact with or modify.

·         Textbox: - the textbox control provides an area to enter or display text.

·         Frame: - the frame control provides a visual basic and functional container for controls.

·         Command button: - the command button control carries out a command or action when a user chooses it.

·         Checkbox: - the checkbox control displays a true/false or yes/no option. You can check any number of checkbox on a form at one point in time.

·          Option Button: - The option Button control, as a part of an option group with other option buttons, displays multiple choices, from which a user can choose only one.

·         Combo box: - The combo box control combines a textbox with a list box, and allows a user to type in a section or select an item from a drop-down list.

·         List box: - the list box control displays a list of items that a user can choose from.

·         Horizontal & Vertical Scrollbar: - the horizontal and vertical scrollbars allow a user to select a value within a range of values. 

·         Timer :- the timer control executes timer events at specified time intervals

·         Drive List box :- It displays and allows a user to select valid disk drives;

·         DirListbox: - It displays and allows a user to select directories and paths.

·         FileListbox: - It displays and allows a user to select from a list of files.

·         Shape: - The shape control adds a rectangle, square, ellipse or circle to a form.

·         Line: - The line control adds a straight line segment to a form.

·         Image: - The image control displays bitmaps, icons or windows metafiles; acts like a command button when clicked.

·         Data Control: - It enables us to connect to an existing database and display information from it on forms.

·         OLE: - OLE Container control embeds data into a visual basic application.











Project explorer displays the list of current forms, reports, modules. We can select the particular form, report, module and change their code and designing.


                 Fig 1.5


If we click on code view in project explorer then following window appears where we can write code for subroutines and functions.



                       Fig 1.6


If we click on design view in project explorer then following window appear where we can place the component selected from toolbox.



                        Fig 1.7





Property window can be used to change the attributes of components placed on form or report. We can set name and modify other properties such as enable/disable, visible/hidden, font, fore color etc. of components such as button, text box etc.



              Fig 1.8



Immediate Window found at the bottom can be used for arithmetic operation such as addition, multiplication, subtraction, division.



                        Fig 1.9



Running a Visual Basic Project


To execute a visual basic project click on start button in the toolbar or select start from the run menu.



Suspending execution


The execution can be temporarily suspended by clicking on break button.


Ending the execution


To end the execution click on the End Button or select end from Run menu.


Program comments


Comments provide a convenient means to document a program. A comment consists of a single apostrophe (‘), followed by a textual message.


Type of variable


Visual Basic recognizes six types of variables


  • Numeric
  • String
  • Boolean
  • Date
  • Object
  • Variant



Control Structures used in Visual Basic 6.0


  • If ...then
  • If ...then...else
  • Select case


Loop Statements used in Visual Basic 6.0


  • Do...Loop
  • For…next
  • While…wend















Menu bar and sub menu as created using menu editor.  To open menu editor click on Tools->Menu Editor. You can set the caption, name, shortcut key, index in menu editor window.


                                    Fig 2.1

We can also set the menu items enabled, visible in menu editor.


Run time adding and deleting menu items

Open standard exe and click on tool-> menu editor and do the setting as shown in following figure.



                                     Fig 2.2


Type place two command buttons on form as shown in following figure


                Fig 2.3


Write the following code in code view



Option Explicit

Dim rtt



'code to add menu item at run time


Private Sub Command1_Click()

rtt = rtt + 1

If rtt = 1 Then menu_item(0).Caption = "menu item"

Load menu_item(rtt)

menu_item(rtt).Caption = "Item" & rtt

End Sub




'code to delete menu item at run time


Private Sub Command2_Click()

If rtt = 0 Then

MsgBox "Menu is empty"

Exit Sub

End If

Unload menu_item(rtt)

rtt = rtt - 1

End Sub











Run the program


Click on add menu item to add menu item at runtime. Suppose we click this button 3 times, the result is shown in following figure.


                           Fig 2.4


When we click on Delete menu item then menu items are deleted. If all menu items are deleted and then we press this button then following message box appears.


         Fig 2.5


Creating popup menu


To create popup menu add the following code in above program.

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

If Button = vbRightButton Then PopupMenu rt

End Sub


When we right click on the form then popup menu appears.

                    Fig 2.6











A Subroutine is a block of statement that carries out a well defined task. The block of statement is placed with a pair of Sub/End Sub statement and can be invoked by name.


When user clicks a control the control’s click Event Handler executes. This handler is a subroutine which performs all the actions you want to perform when control is clicked.

Here is an example of subroutine. This subroutine is called each time the command1 button is clicked.





            Design view                                                       Code view

                        Fig 3.1                                                      Fig 3.2


When we run the program then following form appears




                        Fig 3.2


And when we click on button the following message box appears



        Fig 3.3
















A Function is similar to a subroutine, but a function returns a result. Subroutine performs a task and don’t report anything to calling program whereas functions commonly carry out calculations and report the result. Functions can be predefined and user defined.


Using predefined functions: There are some predefined functions in Visual Basic 6.0 such as math functions, String functions, Date Time functions etc.


Following example implements Sqr() math functions which accepts double value as argument and return square root of that value.



         Design view                                                      code view


               Fig 4.1                                                           Fig 4.2


When we run the program then following form appears



                        Fig 4.3


When we type 8 in text box and click on command1 then following message box appears



            Fig 4.4





User defined function: These functions are defined by user using function keyword. In following form a command button and a text box are placed.


                        Fig 4.5

We have declared a separate function named fact() to find factorial.


                                    Fig 4.6

When we run program following form appears.

                    Fig 4.7

We insert 7 in text1 and click on factorial then following message box appears


        Fig 4.8











It is an interactive object that can resides on any forms in any application that support OCX controls, including web ages. Any Active X control can be added in VB toolbox and can be used on a form. An end user can interact with control to enter data and select or display info.


Building Active X control


  • Start Visual basic and

            Select ActiveX control

            and press Open.



















                                                                                                            Fig 5.1


  • Place one or more control on

            usercontrol form. We also can

            specify the events according to

            over requirement.










                                                                                                            Fig 5.2



  • We also can insert property page, by clicking on project in menu bar.





                                                            Fig 5.3



  • We can select property page and design it.














                                                Fig 5.4




  • To connect property page with user control. Select the property page in properties on usercontrol.




                                                            Fig 5.5 


  • After connect to property page

            click on file in menu bar and click

            on make v1.ocx to create OCX file.




  • Active X control is saved with

            OCX extension in folder












                                                                                                            Fig 5.6











Using OCX in another application


  • Start visual basic and select standard exe.


  • CLICK on project and then click on components, a component dialog box will appear click on browse and select the OCX file saved in folder and click on Apply.























                                                Fig 5.7

  • Place the control on form to see property page right click on control and click on properties the property page connected will be displayed.













                                   Fig 5.8                                                                                                                                      

                          6. BUILDING ACTIVE X DLL




Fig 5.8









Active X DLL file may me combination of more than one functions, classes, subroutines.


Open visual basic.

Click on new project.

Select Active X dll and click open




























                                                  Fig 6.1                                                                                               


In code view we can make a subroutine. In the given example we have

made subroutine a and it displays the message.




                       Fig 6.2


Set the name of class cl1 and to set the name of project follow the following steps.


Click on project in menu bar and select project properties then the

following dialog box appears. Here we have set the name of project P




                                                        Fig 6.3


Click on file menu and then click on p.dll to create dll file,

save the file in any folder.


USING ACTIVE X DLL in another application


Start visual basic and select standard exe.


Click on projectin menu bar and select references


Click on browse and select dll file and click ok.


Place a command button on screen.











                             Fig 6.4





Write the following code in code view.










                                                 Fig 6.5





Execute the program, at run time when we click on the command button then a message box is displayed.



                                                   Fig 6.6

























Start visual basic and select standard exe application


Select Adodc control and rich text box from components


On the form1 place the command buttons, labels, text boxes, Adodc control and rich text box and set their captions as shown in following fig.




                                              Fig 7.1                                                                                                   






Now open MS.ACCESS and create new database , in this database create a new table table1.


Design view of table1 is as follow.


                     Fig 7.2


Now save the table1.



Connecting Adodc1 to table


Right click on adodc1 and select the properties, then following Property page appears.



                                       Fig 7.3






Now click on build and select Microsoft jet 4.0 OLE DB Provider and click on next


                              Fig 7.4


Now select database


                                 Fig 7.5

Now click on test connection the following msgbox appears


                  Fig 7.6


Now click on Ok button. Now click on recordsource tab of property page select command type adCmdTable and select name of table Table1 ,click on apply and then click on ok button.



                                            Fig 7.7

Now select text box  of code name and set datasource adodc1 and datafield code name.


            Fig 7.8


Similarly select all text box and set datasource adodc1 and corresponding data fields




Click on code view type the following code


‘ Code to add new record


Private Sub ADD_NEW_Click()


End Sub


‘ Code to delete record


Private Sub DELETE_Click()

On Error GoTo x


MsgBox "Record deleted"



End Sub



‘code to save record


Private Sub SAVE_Click()

On Error GoTo x


MsgBox "Record Saved"



End Sub


‘code to search record on the basis of code name


Private Sub SEARCH_Click()

a = InputBox("enter the code name")

i = 0


While (Adodc1.Recordset.EOF = False)

If (a = codename.Text) Then

i = 1

GoTo x



End If


If i = 0 Then

MsgBox "Record Not Found"


End If


End Sub


‘code to calculate total cost by multiplying rate and quantity


Private Sub total_cost_Click()

total_cost.Text = Val(rate) * Val(quantity)

End Sub

Testing the program


Run the project by pressing F5 button of keyboard.


Click on add new, the boxes in form got blank .

Now insert new record, total cost is automatically calculated when we click on text box of total cost.

After filling all fields click on save button following message box appears when record is saved.


      Fig 7.9


We can also save record after modification.


To search record  click on search button and following input box appears


                               Fig 7.10


Type the code name and click on ok if record is not found then following message box appears.


           Fig 7.11


If record is found then it is displayed on form





Navigating records using navigator



To goto first,previous,next, last record we can click on navigator.



                                            Fig 7.12


To delete record click on delete button ,when record is deleted then following message box appears.


        Fig 7.13















Select Microsoft common dialog control from components



Place image control, common dialog control, command buttons on form as shown in following figure.





                            Fig 8.1



Now in code view type the following code


‘code to load image at runtime.

Private Sub Command1_Click()


'if selected file is not image then run time error occurs

'so on error picture will not be loaded

On Error GoTo x

Image1.Picture = LoadPicture(CommonDialog1.FileName)


End Sub



‘code to change background color of form at runtime

Private Sub Command2_Click()


Form1.BackColor = CommonDialog1.Color

End Sub


Run the program. Click on load image button a open dialog box appears now select image file and click on ok.



                                             Fig 8.2



If file is image file then it is loaded otherwise nothing happens.


                                                      Fig 8.3


To change background color click on change background color, a color dialog box appears


                Fig 8.4


Select the color and click on ok the background color is changed


Opening and saving text file at run time


Select rich text box and common dialog box and place then on form as shown in following figure


                                           Fig 8.5

In code view type the following code


'code to open text file

Private Sub Command1_Click()


On Error GoTo A

R1.LoadFile (CommonDialog1.FileName)


End Sub


'code to save text file

Private Sub Command2_Click()


On Error GoTo b

R1.SaveFile CommonDialog1.FileName


End Sub


'code to clear rich text box to create new file

Private Sub Command3_Click()

R1.Text = ""

End Sub

Now run the program.


To open file click on open file button following dialog box appears


                                      Fig 8.6


Select the text file and click on ok if file is text file then it is opened.


                                     Fig 8.7


To create new text file click on new file button type text in text box.



Save file by clicking on save file button save as box appears set the name of file and click on save button.





                                                  Fig 8.8









ODBC is a Microsoft specific technology. To make use of ODBC drivers installed the operating system (M.S. WINDOW) provides an interactive tool via which DATA SOURCE NAMES (DSN) can be created.

Using the operating system’s interactive tool


  1. An appropriate system DSN will be created.
  2. This DSN will be bound to a database specific ODBC driver.
  3. The ODBC driver will be in turn bound to a specific database on the hard disk.


The DSN will then be a unique named object that knows

  1. The ODBC driver to be used.
  2. Where the database to which the driver needs to connect is located on the hard disk.

The system DSN is often referred to a Middleware in a Client Server based application. System DSN sits between the Visual Basic form and the database/table in which application data is stored for manipulation.




The focus is to create a named , 32 bit ODBC driver , via which a Visual Basic form will communicate with an M.S. Access database/table.

  1. Click on Start -> Control Panel->Administrative tools.



                                                Fig 9.1

  1. Select the ODBC Data Source (32 bit) from the control panel.



                                                            Fig 9.2


  1. The ODBC data source administrator allows the creation of several types of named data Source. Select the System DSN.




                                                Fig 9.3

  1. Click on Add button.




  1. This will open up interface, which will display all the ODBC drivers installed with the operating system on the computer.




  1. Select the appropriate driver. In the case of Oracle ODBC  will be selected.




                                                            Fig 9.4




  1. Once the Oracle  ODBC driver has been selected, click on finish on the interface to complete creating the system DSN.










  1. Now type the unique name to the system DSN being created. Once done this system DSN needs to know location at which the database resides on the computer’s hard disk.




                                                                Fig 9.5






                                                                Fig 9.6



  1. Click on Ok.





  1. Once these steps are completed , a 32 bit ODBC, named Data Source is created and registered with the operating system.




















ActiveX Data Objects

Microsoft ActiveX Data Object (ADO) is an object oriented database access technology. It is easier to connect to a client/server database using ADO than any of the other techniques offered by Microsoft such as DAO and RDO. ADO technology uses middleware built around OLE DB technology. Middleware built around this technology is called an OLE DB data provider.


OLE DB data provider sits between ADO interface and a database. Programmers interact with the ADO interface, the ADO interface interacts with the OLE DB data provider and the OLE DB data provider in turn interacts with the database to which it is bound.


Open Database Connectivity  is designed to allow access to relational data, OLE DB is an open standard designed to allow access to all kinds of data.




                                                            Fig 10.1



Data provider are application such as MS SQL server, MS Exchange, Operating System components such as the file system that have data that other application may need to access.


Data consumers are application that use the data exposed by the data providers. Any application that uses ADO is an OLE DB data consumer.


Service Components are components of OLE DB that process the transport data. These components include query processors and cursor engines.


Steps of connecting ADO to database

  • Click on project menu in menu bar then following menu bar appears



                  Fig 10.2

  • Click on Components then Components dialog box appears select MICROSOFT ADO DATA CONTROL 6.0(OLEDB) and click on Apply.



                                                Fig 10.3


            ADO control appears on toolbar select control and place it on form.



  • Right click on control then following menubar appears.






                   Fig 10.4



  • Click on ADODC properties. Then following Property page appears





                                                                Fig 10.5




  • Click on build button and select OLE DB PROVIDER for ODBC drivers



                                                                Fig 10.6


  • Click on  Next>> button. In data link properties select data source name and click on test connection.



                                           Fig 10.7


            Then following dialog box appears





                                    Fig 10.8


  • Click on ok. Connection string is automatically generated in property page click on apply and click on ok. Now ADO has been connected to database.





                                                Fig 10.9