VISUAL BASIC TUTORIAL GENERAL INTRODUCTION ABOUT VB 6.0 VB is a GUI based Programming Language It is developed based upon the Client-Server Model It is an object based Programming It is developed based on Intelli Sense Technology Auto Quick Info, Auto List Members VB is a package cum language VB is a compiler cum interpreter Each object has Properties, Methods and events OBJECT: 1All real world enterprises are considered as objects Eg: Human Being a)Properties: Characteristics of an object. Eg :Human being width, height, color b)Methods : work done by the objects Eg: Human being sleeping , talking c)Events : Work done at specific instance Eg: Morning Walk. VB is an Event Driven Programming BASIC HARDWARE REQUIREMENTS FOR VISUAL BASIC: I)Hard Disk Space : 250 MB II)OS : Win 95 / NT III)Processor Intel 486 IV)RAM :16MB VB Comprises of IDE: Integrated Development Environment Used to develop RAD Application Rapid Application Development Components of IDE: 1.Menu Bar 2.Menus 3.Tool Bar 4.Tool Bar controls 5.Tool Box 6.Object Browser 7.Property Window 8.Project Explorer Window
9.Immediate Window 10.Code Window 11.From Layout Window
Window
Short Cut Keys
1. Property window 2. Project Explorer Window 3. Form Code Window 4. Code Window Form 5. Immediate Window 6. Object Browser 7. Run the program
F4 ctrl + r F7 Shift + F7 ctrl + g F2 F5
VARIABLE: It is an identifier used to store and manipulate objects In VB all variables may be declared by means of DIM statement DIM (dimension statement) Syntax: Dim as Data Type: It is the type of value that the user is going to store Data Type in VB can be categorized in to7 major types. 1)Integer 2) Long 3) Double 4) Boolean 5) Currency 6)String 7) variant Variant is the default data type Variant=Integer + String. Properties: Design Time (static) f4 ii) Run Time Properties (dynamic) (coding) SCOPE OF THE VARIABLE: Scope: It is the longevity and activity of the variable There are 3 scopes for a variable: 1.Local Scope : Variables declared within the sub procedure 2.Global Scope : Variables declared outside / top of all procedures 3.Module Scope : Accessible to all forms FORM OBJECT: Form object has many events out of which important events are as follows:
1Load Event () 2Activate Event() 3Unload Event() 4Terminate Event() 5Click ()
Triggered when the form is loaded Triggered after the Load Event Triggered when the form is unloaded Triggered after the unload event Triggered when the form is clicked
General: ASCII a-z 97-122 A-Z 65-90 0-948-57 Extension: Form .frm VB Project File .vbp Event Sequence: Form Load Activate unload terminate Specify all names in Hungarian Prefix – 3 letter word Command button cmd COMMAND BUTTON CONTROL: F4: style 0-standard 1 graphical F4: Picture, back color Setting shortcut key using ‘&’ symbol in the caption for a specific character TEXT BOX OBJECT /CONTROL F4: Text, Name, Appearance 0-flat, Alignment, password char, Multi line, scroll bar a)Used to get the input from the user. b)Restricting the user from typing a specific set of keys. Events: i) got focus ii) Lost Focus iii) Key press Method: set focus Properties: I) selstart ii) sellength iii) password char COMBOBOX CONTROL Combo box = text box + list box There are 3 styles in a combo 1.drop down combo 2.simple combo 3.drop down list combo
Properties of Combo Box: F4: Properties: List used to add items in to the combo. Style Run Time: Methods: Add Item used to add item in to the combo. Remove Item used to remove item from the combo based on List Index. Properties: List Count used to count the number items in the list. List Index used to find out the list index. By default list index starts with 0. List Extract the item from the combo. Note: & - called concatenation operator ‘ Remark or non executable or comment line Dialog Boxes in VB 6.0: 1)Modal Dialog Box: This kind of dialog box does not allow the user to activate other windows without disposing it. E.g.: msg box, input box Msg box: used to display messages Can be used in 2 forms i)as a statement contain only messages ii)as a function contain specific icons and buttons Input box: used to get input from the user 2)Modeless Dialog Box: This kind of dialog box allows the user to activate other windows without disposing it. E.g.: Forms Built In Functions in VB6.0 All these functions are tested in VB using Immediate Window ctrl+g | View Immediate Window Built-in functions in vb can classified in to 5 major types
1) Date and Time Functions: 2) 3) 4) 5)
Format Functions String Functions Math Functions Array Functions Array index will always have 0 as its first index Array functions can be classified in to 5 major types i)Array() ii)L bound () lower bound of an array. iii)U bound () upper bound of an array. iv)Join () converts an array in to expression. v)Split () Converts expression in to an array.
All these functions can be tested immediately using immediate window For invoking immediate window Short cut ctrl + g View Immediate Window ‘?’ Print operator CONTROL ARRAYS: More than one control of same control type that share the same name with different index value. Index value starts from 0 to n. Control arrays can be created by 2 ways. 1)Design Time (F4) cut/copy paste calculator 2)Through coding run time Color Functions in VB6.0 There are 2 color functions available in VB 1)qbcolor(integer) which produces 16 colors ranging from 0 to 15 2)RGB(red,green,blue) which produces 256 colors ranging from 0 to 255 for each red green blue value. 3)Rnd: Produces random numbers. Eg: qbcolor(rnd * 15) Graphics in VB: There are 4 graphics functions available in vb 1)Line Method: Used to draw a line (x, y)---------------------(x1, y1) Syntax: Line(x, y)-(x1, y1), color, bf
2)Circle method: Used to draw a circle Syntax: Circle(x, y), radius, color, xdia, ydia 3)Pset Method: Used to set or plot points on the form Syntax: pset( form width, form height), color 4)Cls Method: Used to clear the form TIMER CONTROL: This control used to execute events in milliseconds as specified by intervals Property: Interval (milliseconds) Events: Timer Common Properties: Enable Property It will be in inactive state Visible Property will not appear on the screen Menu Creation: Menu can be created in VB by means of Menu Editor Menu Editor can be invoked by 2 ways Tools Menu Editor Shortcut Key ctrl +e Menus: It is a collection of items. Each Menu will have some set of sub menus. There can be 5 levels of submenu levels. Each sub menu can have short cut keys unless they themselves act as main menu. MDI MULTIPLE DOCUMENT INTERFACE Multiple forms can be made to interact using one medium 6In an MDI form controls cannot be placed, except menus 7Ordinary forms that are going be part of MDI , should be child forms. 8Set the MDI Child Property true for ordinary Forms SDI SINGLE DOCUMENT INTERFACE: Eg: Form
Usage of common Dialog Control: Open Dialog Box Save Dialog Box Color Dialog Box Font Dialog Box Help Dialog Box Cut, Copy Paste Microsoft Windows Common Controls 5.0 SP2 1 Tree View 2 List view 3 Progress Bar 4 Status Bar 5 Slider 6 Tab Strip Control 7 Toolbar Control 8 Image List Z-Order Method: It is a property available for all the controls for bringing the control back and forth. 0 Bring to back 1 bring to front OLE OBJECT LINKING AND EMBEDDING OBJECT LINKING: One object of an application is linked with another object application. OBJECT EMBEDDING: Entire application is embedded in to the current application. CREATION OF ACTIVEX CONTROL: ACTIVEX CONTROL: Controls that is external to the application which can be used to create custom / user defined components. ActiveX controls has no entry point, only the effects alone can be rendered. ActiveX Controls can be used in the internet also. In VB these ActiveX controls are termed as “Custom Controls”. Extension (.ocx) These controls are created in ActiveX Control Project. New Project Dialog ActiveX Control Project. In order to load the user defined control in your standard EXE component dialog box the user should saved it in the following extension “C:\WINNT\SYSTEM32”.(2000 OS) WIN XP -> C:\WINDOWS\SYSTEM32\. Converting ActiveX control File
File Make <project name>.ocx ActiveX Control forms has the extension .ctl Custom Controls has the extension .ocx CREATION OF ACTIVEX DLL: DLL: DYNAMIC LINK LIBRARY These are the library files that external to your current environment and can be included in to your application during run time alone. DLL files are always the procedure / function oriented files (non-visualizable) These files has the extension .DLL In Vb It can be created by means of ActiveX Dll project All DLL project will have class files with extension .cls To convert DLL project FileMake <projectname>.dll FILES IN VB 6.0 Files can be categorized in to 2 major types: 1.Sequential Access File Used to write and read the data sequentially 2.Binary Files: Reading and writing the data in binary format FUNCTIONS AND STATEMENTS USED IN THE FILES 9free file used to extract the available free space in the file 10Open used to open the file in specific mode. Mode: specifies purpose of opening the file Sequential: For Reading Input Input For writing Output write Binary: For Reading Binary get For writing Binary put Random: For Reading Random get For writing Random put Syntax: Put #file number, start position, text content get #file number, start position, text content
Syntax: Open For <mode> As 11Close #file number used to close a file.
ACTIVEX DATA OBJECTS ADO ACTIVEX Data Object Model Is an Interface to access the data from the database. It is an Object Model which is Microsoft’s Newest and most powerful paradigm. OLEDB / ADO ARCHITECTURE: VB Pic: 1 To use ADO Objects in code the following reference library should be included. Project References ActiveX Data Object Library 2.0 Using ADO Objects in VB: 1.Connection Object: Used to establish a connection between front end (vb) and database (back end) Properties of Connection Object: a)Provider used to provide the OLEDB Provider connection string. Every Database has Software called provider that will link that particular database with your Front End tool. Some of the Providers are: i)Ms Access : Microsoft.Jet.oledb.4.0 ii)Sql server : sqloledb.1 iii)Oracle : MSDAORA Connection can be established by 2 ways namely: A)Through ODBC ODBC: OPEN DATABASE CONNECTIVITY It is an interface software used to connect different databases with desired from end tools via “Data Source Name” shortly called as DSN. Driver: A driver is software for connecting specific database Establishing ODBC Connectivity: Steps:
1Start Settings Control Panel Administrative Tools Data Sources (ODBC) 2ODBC Data Source Administrator Click Add Button 3Create New Data Source Dialog pop ups 4Choose an appropriate Driver depends upon your database. For Eg Choose Microsoft Access Driver(* . mdb) for access database 5Microsoft Access Set Up Dialog will pop up 6Click the Select button and select the database 7Specify the Data source name in the text box. 8Click OK. 9Specified Data Source name will appear in the ODBC Data Source Administrator Dialog Box Through Pure Coding CREATING A TABLE IN VB USING ADD-INS: STEPS: 1Choose ADD-INS Visual Data Manager 2Vis Data Dialog Box will pop up 3Choose File New Microsoft Access Version 7.0 MDB CONNECTING DATA THROUGH DATA CONTROL Set the Data Control’s Data Base Name choose the location of your database Set the Data Control’s Record Source Name choose the table from popping up Set the Text Box Control’s Data Source choose it as Data1 Set the Text Box Control’s Data Field choose it as field B OF 1 2 3 EOF
CREATING PROPERTIES: Apart from built-in properties available in VB user can also define own properties. Property should be created as a global so that all forms can be used. Property should be coded in the module section.
Modules will have the extension *.bas Project Add Module Property in VB can be created by means of the following accessor Let accessor: used to set the value to the property Get Accessor: Used to retrieve the value from the property. Syntax: Let Accessor: Public property Let <property name> (variable as data type) End Property Get Accessor: Public property get<property name> () as data type End Property TYPE Statement: Used to create records with specified fields Syntax: Type Local Variable declaration End Type CREATION OF DATA REPORTS: Reports: Reports are nothing but the extract of your project. The result or final output of the project is displayed as reports. Reports in VB can be produced by means of i)Data Environment project Add Data Environment ii)Data Report Designer project Add Data Report Note: Default Data Base NWIND, BIBLIO Path: D:\Program Files\Microsoft Visual Studio\VB98\NWIND.mdb STEPS FOR DATA REPORT PART – I inside the Data Environment:
1)Open Add Data Environment 2)Right click Connection1 Choose Add command 3)Connection1 right click properties Data Link Properties choose provider click next select the data base click test connection test connection succeeded Click Ok. 4)Right Click Command1PropertiesGeneral Tab Source Of Data select Data Base Object =Table Object=Employees OK 5)Expand and View the command1 tree. PART – II in the Data Report: 1. Project Data Report 2. Windows Menu Tile Vertically 3. Data Report Extension Report name. Dsr 4. Data Environment Data Environment name. Dsr 5. Data Reports has 5 Sections namely: a) Report Header Section Right Click Insert Control Current Date (short date) b) Page Header Section: Contains the name of the field. c) Detail Section: All Fields are going to be stored From the Data Environment choose the fields to be placed on the Detail Section Select Drag and Drop Click on the detail section out of 2 copies drag one copy and drop it on the page header section. Drop another copy as it is on the details section. d) Page Footer Section Right Click Insert Control Current Page e) Report Footer Section Choose F4 Data Report1 Data Source set Data Environment1 Data Member set command1 CODING IN THE FORM Data Report1.show Run the form Store it in \.........\ VB98\