Download - SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

Transcript
Page 1: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

SYLLABUS

B.Com (Computer) VI SEM Subject – Visual Basic

Unit – I

UNIT – I Introduction to Visual Basic: Introduction Graphics User Interface (GUI), Programming Language (Procedural, Object Oriented, Event Driven), The Visual Basic Environment IDE; Introduction to VB Controls: Textboxes, Frames, check Boxes, Option Buttons, Setting a Border & Style, the shape Control, The line control, Working with multiple controls and their properties, Designing the user Interface, Keyboard access, tab controls. Default & controls property, Coding for Controls, list box and combo box and their properties, filing the list box using property window/ add item method, picture/ image box and their properties.

UNIT – II Variables, Constants and Calculations: Variables, Variables Public, Private, Static, Constants, Data Types, Naming rules/ conventions, Named & intrinsic, Declaring variables, Scope of variables, Val Function, Arithmetic operations, formatting Data. Error functions and types. Introducing to menu editor.

UNIT – III Decision & conditions and Controls: If Statement, If then-else Statement, Comparing String, Compound conditions (and, or Not), Nested if Statements, Case Structure, Using If Statements with option Buttons & Check Boxes, Displaying Message in Message Box, testing whether input is valid or not. Using call Statement to call a procedure. Do loop, while and for next loop.

UNIT – IV Working with forms and procedures: Introducing to forms and types of forms and setting form properties, Creating, adding, removing Forms in project, hide, Show Method, Load, Unload, Statement, Me Keywords, Referring to objects on a Different Forms.

UNIT – V Introduction to database connecting tools (ADO, DAO, ADODC, ADODB), Creating the database file for use by Visual Basic (Using MS- Access),, Using the Data control, Setting its property, Using Data control with forms, using list boxes & combo boxes as data bound controls, updating a database file (adding, deleting records): PS – (Lecturers should be on Basic concepts only i.e. Definition, Diagrams and working Principles)

Page 2: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

B.Com (Computer) VI SEM Unit I

Introduction to Visual Basic Microsoft visual basic is a powerful development platform developed by Microsoft that can be used to create feature rich application. It is based on event driven programming model. Visual basic consists of two part i.e. “VISUAL” and “BASIC”. The visual part refers to the method used to create the graphical user interface: rather than writing numerous lines of code to describe the appearance and location of interface elements. This part makes visual basic a true RAD (Rapid Application Development) tool. The Basic part refers to the BASIC (Beginners All purpose Symbolic Instruction Code) language since visual basic is a descendant of BASIC. Features of VB :

1. Event –driven programming language 2. Object orientation 3. GUI/Windows Environment 4. Internet Based Application.

Introduction Graphical User Interface (GUI) : Since we know two types of interface is available for performing any types of work in computer. First is called CUI (Character User Interface) in which all operation is performed with the help of character. Second is GUI (Graphical User Interface) In which all operation is performed with the help of graphics. Programming language Programming language are those language that is specially developed, so that you could pass your data and instructions to the computer to do a specific job. Examples are FORTRAN, BASIC, COBOL etc. Mainly following types of programming are used:

1. Procedural Programming: in this approach the problem Is considered to be a sequence of things to be performed. for every task , a function is written which is the center of interest for this approach. Examples are COBOL, FORTRAN etc.

2. Object oriented programming: in object orientated programming the program is divided into objects rather than function. They are the basic run time entities, they may represent a person , employee. In object oriented programming language we use class in which many object is used. in object oriented programming we also use data abstraction , polymorphism , data encapsulation and inheritance

3. Event Driven Programming : visual basic is a event driven programming language . in visual basic user is in the control of the application. Every time the user clicks a command button or presses the mouse , an event stream is generated and code that has been written behind the event is executed.

The visual basic environment IDE:

Visual basic is not just a language, but is an integrated development environment. An IDE is basically a term commonly used in the programming world to describe the interface and environment where you can develop, run, test and debug your applications.

Environment consist of

Tool box : - it is a container that contain different objects and controls. The objects and controls in the toolbox can be added to the form to crate graphical user interface. Form designer window ; - it is a workspace where you actually design the visual part of the form. A form is just like a canvas on which a painter or an artist paints his work Project explorer window : - it contains the list of all the files, which were used to build an application. In project explorer window ,collection of files is called project.

Page 3: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Properties window : - the properties window is used to customize the object for an application by assigning values to different parameters listed in the properties window. For example properties can be viewed alphabetically or by categorized. F4 is the shortcut to display the properties window. Code editor window: - it is window to write the user defined codes. Code editor window can be opened by double – clicking on a form or control in the form layout window. The code editor window contain two drop down list : the object list and procedure list. Standard tool bar: -it includes buttons for many of the most common commands used in visual basic, such as open project , save project ,start , break and end. The menu bar : - it contain file , edit and view menu.

VB Controls Textboxes: - text box control allows to display text on the from, this text can be some record from the tables in the database or some other text. Textbox control is capable to handle thousands of characters of text. In textbox property setting Multiline to true enables a text box to accept or display multiple line of text at run time. A multiple line text box automatically manages word wrap as long as there is no horizontal scroll bar. The scrollbar property is set to 0 by default. It can be set to 1 – Vertical , 2 – horizontal or 3- both. Frame : - It is used to provide and identifiable grouping for other controls. This control has the effect of grouping them together so that when the frame is moved the other To add other controls to the frame, draw them inside the frame. If you draw a control outside the frame , or use the double click method to add a control to a form ,and then try to move it inside the frame control , the control will be on top of the frame. Properties are caption , enabled and visible. Check box : - A check box allows the user to select one or more choices from a list of values. Common check box properties are caption, name, picture and alignment. Option/Radio button: - option buttons are used to display options. They are usually grouped in a option form which one can be selected by the user. Common option button properties are style , picture and value.

Page 4: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Setting a Border & Styles : - it Returns or sets the border style for an object. For the Form object and the Textbox control Syntax object.BorderStyle = [value] object : - an object expression that evaluate for an object in the applies to list. value :- a value of constant that determine the border style. Example : - Give a Textbox a flat look with no border.

Text1.BorderStyle = 0 Shape Control : - It is used to draw a rectangle , square ,oval ,circle on a form.

VbShapeRectangle – 0(the default); rectangle VbShapeRectangle – 1; square VbShapeRectangle – 2; oval

The line control: - it is used to draw lines on your form. The line control’s primary properties are X1,X2,Y1,Y2 and those values form the coordinates of the line segment:(X1,Y1) and (X2,Y2). You can even change those values at runtime or resize the line. Working with multiple control: - Label: - it display text on a form that is used to describe another control. Properties are word wrap, border style etc. Command button: - a command is used when you want the user to perform some action on a form. The action may be to close the form, save information on the form. Once the command button is clicked, it will generate and event. Properties are cancel , enabled , style etc.. Timer control : - timer control work like a stopwatch .the timer control always show up as an icon at design time ,but it does not show at run time. it can execute code at regular intervals.. Properties are interval: it is measured in milliseconds, and it has value from 1 to 65535 And enabled: it is set to true when timer control will invoke the timer event in the time. Scroll bar control: - scroll bar controls allow us to create a scrollable graphics viewport application, it has two types first are vertical scrollbar and other is horizontal scrollbar. Designing the user interface: - the user interface have toolbox , menu bar and tool bar . Keyboard access: - we can use all control with the help of keyboard also. for example ,if the user must be able to press ALT + 0 to run an open file command button ,the caption property is set to &Open. The ampersand does not show up on the button, but the letter for the access key is underlined. Default and cancel properties: - A command button can be selected to be the default command button that is, whenever the user press the enter key , the command button is clicked. This is done to specify a command button as default set the default property to true. A default cancel button can also be spectified.when the cancel property of a command button is set to true. List box : the list box presents a list of items in which the user can click and select the items from list . To add items to the list we use the Additem method. For example Private Sub Form_ Load () List1.AddItem “Sunday” List1.AddItem “Monday” List1.AddItem “Tuesday” List1.AddItem “Wednesday” End Sub Properties are ListIndex.

Page 5: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Combo box :- the Combo Box also represent list item in which the user can click and select the item. The user needs to click on small arrowhead. Filling the list box using property window/add item To add items to the combo box we use the Additem method. For example Private Sub Form_ Load () Combo1.AddItem “Jan” Combo1.AddItem “Feb” End Sub Combo box have three types of styles : the drop – down combo box , the simple combo box and the drop down list. Picture box: - the picture box is one of the controls that is used to handle graphics. For example the following statement will load the picture grape.gif into the picture box . Picture1.Picture=LoadPicture(“C:\VB program\Images\grape.gif”) Image box: - it function almost identically to the picture box. there is one major difference , according to an image box is stretchable ,which means it is resizable. This feature is not available in the picture control box. Example:- Image1.Picture=LoadPicture (“C:\VB program\Images\grape.gif”)

Visual basic building block : - 1. Object : - An object can be said to be a programmable entity ,containing both code and data.

This entity can be treated and controlled as a unit. Examples of object includes command buttons, text box etc.

2. Properties : - properties consist of the object’s data , setting and attributes. It describe the object ,their appearance and behavior. Like text caption ,and name are common example of properties. Properties can be set both at runtime and design time.

3. Events: - any action recognized by a form or control is called an Event. Visual basic is event – driven – programming language. Some common event that apply to form is Load and Unload Event that apply controls are Click , Double Click and Change.

Page 6: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

4. Forms : - A from is a window to which you can attach Visual basic controls. Every form is associated with its own properties , events , and methods ,which can be used to control of form appearance and behavior.

5. Controls : - To make forms more interactive or to make the front end of the application a totally graphical user interface, control are added to forms. For example for a textbox control properties can be set the size of textbox, the font for the text and color of the text.

6. Modules : - A module is a set of statements written in visual basic. Modules are Form modules Standandrad modules Class modules Resources files

Each module can contain procedure and declarations. 7. Methods : - A method in VB is a predefined action that operates on a specific object. There

are a lot of methods in VB ,some of them are Drag, move, refresh and Set Focus

8. Event Procedure: - When a event occurs in your application ,Visual basic automatically produce the event and runs the code that you have written for it . This code is called as an Event Procedure. Part of event procedure are Private Sub , The objects name , an underscore and the event name

9. General Procedures ; - A general procedure is one that isn’t associated with a particular event. General procedure can be used for Sharing code among controls on one form. Sharing code among multiple forms.

Page 7: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

UNIT II Variable: - variable are used to store information in the computer’s memory while the program is running. Variable are nothing but storage spaces for the values. For example : - Dim var as Integer Where dim for dimension , var for variable name and integer is data types. Naming rules of the variable : - 1. The name of the variable can start only with a letter. It can contain letters , numbers and underscores only. 2. Name should be unique within its scope. 3. The name can not be longer than 255 characters. 4. The name can’t be one of the VB reserve word. 5. Variables are case sensitive Variable Declaration There are three ways for a variable to be typed (declared): 1. Default 2. Implicit 3. Explicit

1. If variables are not implicitly or explicitly typed, they are assigned the variant type by default. The variant data type is a special type used by Visual Basic that can contain numeric, string, or date data.

2. To implicitly type a variable, use the corresponding suffix shown above in the data type table. For example, TextValue$ = "This is a string" creates a string variable, while Amount% = 300 creates an integer variable.

3. To explicitly type a variable, you must first determine its scope. There are four levels of scope: ⇒ Procedure level ⇒ Procedure level, static ⇒ Form and module level ⇒ Global level Scope of the variable: The scope of the variable refers to its visibility within the program. A variable which is declared implicitly in a procedure has a scope that is limited to the procedure. Scope Private Public Procedure level Variable are private to the

procedure in which they appear

Public variables can not be declared within a procedure.

Page 8: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Module level Variables are private to the module in which they appear

Variables are available to all modules.

Public variable: - the variable which are declared in the general section as public, are the variables which are public in scope i.e. the variable across all the forms in a project. Example ; - public var as Integer Private sub form_load() Var=12 Msgbox var & “ in form 1 “ Load form2 End sub Private variable: - private variables are also declared in the general section of the code window .private variable can be used and accessed by all the procedure of the module Example ; - Private sub form_load() Var=12 Msgbox var & “ in form load “ End sub Private sub command1_click() Msgbox var & “ on click “ End End sub Static variable : - when a procedure is called multiple times. To create a variable that retains its value , the variable is declared as static. Private sub command1_click() Static ctr as integer Ctr= ctr+1 Text1.text=ctr End sub Constants Many times in Visual Basic, functions and objects require data arguments that affect their operation and return values you want to read and interpret. These arguments and values are constant numerical data and difficult to interpret based on just the numerical value. To make these constants more understandable, Visual Basic assigns names to the most widely used values - these are called Symbolic constants. Appendix I lists many of these constants. As an example, to set the background color of a form named for Example to blue, we could type: frm Example. BackColor = 0xFF0000 Defining Your Own Constants You can also define your own constants for use in Visual Basic. The format for defining a constant named PI with a value 3.14159 is: Const PI = 3.14159 Named and intrinsic constant Named constant are those constants which are defined by yourself. It is defined with the help of Const keyword.you can give the constant a name,a data type and a value. Syntax:- const identifier[as datatype]=value Example:- const strcompanynameas String =”Renaissance college” Const strcompanyaddressas string =”indore”

Page 9: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Intrinsic constant:- this are system defined constant. Several set of intrinsic constants are stroed in library files and available for use in your visual basic program. Visual Basic Data Types

Functions A function is similar to a normal procedure but the main purpose of the function is to accept a certain input from the user and return a value which is passed on to the main program to finish the execution. There are two types of functions, the built-in functions (or internal functions) and the functions created by the programmers. The general format of a function is Function Name (arguments) The arguments are values that are passed on to the function. Function Value Returned

Data Type Suffix Boolean None Integer % Long (Integer) & Single (Floating) ! Double (Floating) # Currency @ Date None Object None String $ Variant None

Abs Absolute value of a number Asc ASCII or ANSI code of a

character Chr Character corresponding to a

given ASCII or ANSI code Cos Cosine of an angle Date Current date as a text string Format Date or number converted to a

text string Left Selected left side of a text string Len Number of characters in a text

string Mid Selected portion of a text string Now Current time and date Right Selected right end of a text

string Rnd Random number Sin Sine of an angle Sqr Square root of a number Str Number converted to a text

string Time Current time as a text string Timer Number of seconds elapsed

since midnight Val Numeric value of a given text

string

Page 10: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Mathematical Function The mathematical functions are very useful and important in programming because very often we need to deal with mathematical concepts in programming such as chance and probability, variables, mathematical logics, calculations, coordinates, time intervals and etc. The common mathematical functions in Visual Basic are Rnd, Sqr, Int, Abs, Exp, Log, Sin, Cos, Tan , Atn, Fix and Round. The Numeric Functions The numeric functions are Int, Sqr, Abs, Exp, Fix, Round and Log. a) Int is the function that converts a number into an integer by truncating its decimal part and the resulting integer is the largest integer that is smaller than the number. For example, Int(2.4)=2, Int(4.8)=4, Int(-4.6)= -5, Int(0.032)=0 and so on. b) Sqr is the function that computes the square root of a number. For example, Sqr(4)=2, Sqr(9)=2 and etc. c) Abs is the function that returns the absolute value of a number. So Abs(-8) = 8 and Abs(8)= 8. 60 d) Exp of a number x is the value of ex. For example, Exp(1)=e1 = 2.7182818284590 e) Fix and Int are the same if the number is a positive number as both truncate the decimal part of the number and return an integer. However, when the number is negative, it will return the smallest integer that is larger than the number. For example, Fix(-6.34)= -6 while Int(-6.34)=-7. f) Round is the function that rounds up a number to a certain number of decimal places. The Format is Round (n, m) which means to round a number n to m decimal places. For example, Round (7.2567, 2) =7.26 g) Log is the function that returns the natural Logarithm of a number. For example, Log 10= 2.302585 Format function and String Formatting output is a very important part of programming so that the data can be presented systematically and clearly to the users. Data in the previous lesson were presented fairly systematically through the use of commas and some of the functions like Int, Fix and Round. However, to have better control of the output format, we can use a number of formatting functions in Visual basic. 6 The three most common formatting functions in VB are Tab, Space, and Format (i) The Tab function Tab (n); x The item x will be displayed at a position that is n spaces from the left border of the output form. There must be a semicolon in between Tab and the items you intend to display (VB will actually do it for you automatically). Example1 .Private Sub Form_Activate Print “I”; Tab(5); “like”; Tab(10); “to”; Tab(15); “learn”; Tab(20); “VB” Print Print Tab(10); “I”; Tab(15); “like”; Tab(20); “to”; Tab(25); “learn”; Tab(20); “VB” Print Print Tab(15); “I”; Tab(20); ; “like”; Tab(25); “to”; Tab(30); “learn”; Tab(35); “VB” End sub The Output for example 1 is shown below: ii) The Space function The Space function is very closely linked to the Tab function. However, there is a minor difference. While Tab (n) means the item is placed n spaces from the left border of the screen, the Space function specifies the number of spaces between two consecutive items. For example, the procedure Example 2

Page 11: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Private Sub Form_Activate() Print “Visual”; Space(10); “Basic” End Sub Means that the words Visual and Basic will be separated by 10 spaces iii) The Format function The Format function is a very powerful formatting function which can display the numeric values in various forms. There are two types of Format function, one of them is the built-in or predefined format while another one can be defined by the users. These errors can be grouped into three categories: 1. Syntax errors 2. Run-time errors 3. Logic errors • Syntax errors occur when you mistype a command or leave out an expected phrase or argument. Visual Basic detects these errors as they occur and even provides help in correcting them. You cannot run a Visual Basic program until all syntax errors have been corrected. • Run-time errors are usually beyond your program’s control. Examples include: when a variable takes on an unexpected value (divide by zero), when a drive door is left open, or when a file is not found. Visual Basic allows you to trap such errors and make attempts to correct them. • Logical errors are the most difficult to find. With logic errors, the program will usually run, but will produce incorrect or unexpected results. The Visual Basic debugger is an aid in detecting logical errors. Introduction to menu editor: Menu editor is available a form is being designed. It is present in tools menu of VB6. The first step to create a menu is to enter the caption of the menu item. After the caption of the menu item has been set, the menu requites an object name for the programmatic reference. The menu name can be any valid object name. menus use the three letter prefix “mnu” before the selected name. To bring up the Menu Editor, make sure the Form designer is the active window and click Tools/Menu Editor or press Ctrl+E. The caption and the name fields are required. There are three logical sections in the menu editor dialog box.

Figure 6.1: Menu Editor Caption: This is what the user will see. Make the caption as short and as descriptive of its action as you possibly can.

Page 12: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Use proper case. Allows us to enter the menu or command name that we want to appear on our menu bar or in a menu. Name: Menu items are controls and must have a name, the same as any other control. Allows us to enter a control name for the menu item. A control name is an identifier used only to access the menu item in code; it doesn’t appear in a menu. But the Menu Editor does not supply a default, so use the mnu prefix. Example: mnuFile, mnuEdit

Page 13: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

UNIT III Decision condition and controls Decision structure : - in this program conditionally executes the portion of the code depending on the value of a variable or on the user input or on any other condition. Types ; 1. If ………then 2. If……….then……else 3. If……….then……elseif…end if 4. Select case 1. If…..then :- the if….then structure tests the conditions specified and if it is true , executes the statements that follows. If the condition is false then it continues with the statement. Syntax If condition then statement 2. If……then….else :- this statement executes one block of statements if the condition is true and another if the condition is false. Syntax If condition then Statement block -1 Else Statement block -2 End if Example ; If startdate = “11/05/14” then Msgbox “course is vb” Else Msgbox “ course is oracle” End 3. If….then..elseif..end if : The final extension of the if is if …. Then…elseif….end if which can handle several conditions. Syntax If condition then Statementblock-1 Elseif condition2 then Statementblock-2 Elseif condition3 then Statementblock-3 Else Statementblock-4 End if Looping : - it has two types 1. Counter loops: - a counter loops is also known as a for….next loop. The for…next loop is useful in situation where it is known that how many times a particular block of statement is to be run. Example For counter = 1 to 3 step 2 Statement block Next counter 2. Condition loops: - it has tree types While…wend: - syntax While condition Statement-block

Page 14: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Wend If the condition is true, all the statements are executed and when the wend statement is reached to the while statement which evaluates the condition again. Example: - Private sub form_load() Dim ctr as integer Ctr=0 While ctr<=10 Msgbox “visual basic” Wend Msgbox “over” End sub Do…while:- it tells the program that the loops will be repeated while the condition expression is true. When the condition becomes false, the program moves on to the next statement after the loop. Syntax Do Statement1 Statement2 Statement3 Loop while condition is true Do…until : - statements inside do…until are run only as long as the condition is false. When the condition becomes true , the loop terminates. Sytax Do Statement 1 Statement 2 Statement n Loop until expression is true Or Do until expression is true Statement 1 Statement 2 Statement n Loop Compound conditions(and,or,not):- We will use three logical operators Operator Operation Not Logical not And Logical and Or Logical or The Not operator simply negates an operand. The And operator returns a True if both operands are True. Else, it returns a False. The Or operator returns a True if either of its operands is True, else it returns a False. Case structure: - Select Case - In addition to If/Then/Else type statements, the Select Case format can be used when there are multiple selection possibilities. Say we've written this code using the If statement: If Age = 5 Then Category = "Five Year Old" ElseIf Age >= 13 and Age <= 19 Then Category = "Teenager"

Page 15: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

ElseIf (Age >= 20 and Age <= 35) Or Age = 50 Or (Age >= 60 and Age <= 65) Then Category = "Special Adult" ElseIf Age > 65 Then Category = "Senior Citizen" Else Category = "Everyone Else" End If The corresponding code with Select Case would be: Select Case Age Case 5 Category = "Five Year Old" Case 13 To 19 Category = "Teenager" Case 20 To 35, 50, 60 To 65 Category = "Special Adult" Case Is > 65 Category = "Senior Citizen" Case Else Category = "Everyone Else" End Select Notice there are several formats for the Case statement. The Message Box One of the best functions in Visual Basic is the message box. The message box displays a message, optional icon, and selected set of command buttons. The user responds by clicking a button. The statement form of the message box returns no value (it simply displays the box): MsgBox Message, Type, Title where Message Text message to be displayed Type Type of message box (discussed in a bit) Title Text in title bar of message box You have no control over where the message box appears on the screen. The function form of the message box returns an integer value (corresponding to the button clicked by the user). Example of use (Response is returned value): Dim Response as Integer Response = MsgBox(Message, Type, Title) The Type argument is formed by summing four values corresponding to the buttons to display, any icon to show, which button is the default response, and the modality of the message box. The first component of the Type value specifies the buttons to display: Value Meaning Symbolic Constant 0 OK button only vbOKOnly 1 OK/Cancel buttons vbOKCancel 2 Abort/Retry/Ignore buttons vbAbortRetryIgnore 3 Yes/No/Cancel buttons vbYesNoCancel 4 Yes/No buttons vbYesNo 5 Retry/Cancel buttons vbRetryCancel The second component of Type specifies the icon to display in the message box: Value Meaning Symbolic Constant 0 No icon (None)

Page 16: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

16 Critical icon vbCritical 32 Question mark vbQuestion 48 Exclamation point vbExclamation 64 Information icon vbInformation The third component of Type specifies which button is default (i.e. pressing Enter is the same as clicking the default button): The value returned by the function form of the message box is related to the button clicked: Value Meaning Symbolic Constant 1 OK button selected vbOK 2 Cancel button selected vbCancel 3 Abort button selected vbAbort 4 Retry button selected vbRetry 5 Ignore button selected vbIgnore 6 Yes button selected vbYes 7 No button selected vbNo Message Box Example: MsgBox “This is an example of a message box”, vbOKCancel + vbInformation, “Message Box Example” You've seen message boxes if you've ever used a Windows application. Think of all the examples you've seen. For example, message boxes are used to ask you if you wish to save a file before exiting and to warn you if a disk drive is not ready. Compare string:- Unlike any other computer languages , you can compare strings with the relational operators in VB and expect to receive reliable results. String class methods called Tolower and toupper. These two methods to convert all the characters of a string to either lowercase or uppercase.the most useful comarision methods are given below. Method name Parameter type Returns Operation performed Equals String Boolean Tests for equality of

string contents Compareto String Integer Returns 0 if equal , a

positive integer if the string in the parameter comes before the string associated with the method and a negative integer if the parameter comes after it.

Page 17: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Unit IV WORKING WITH FORMS AND PROCEDURES Introduction to form The form is the most important visible object,without it no control can be displayed. It is a window that can be designed and modified to fit user applications. In the standard project the form Designer creates and modifies visual forms .When user starts visual Basic program a form is automatically displayed in Designer window. The designer can add any number of forms to the project of his application by pressing: add form from project menu. There are two modes: design mode and running mode. User can interchange between them, by pressing on start icon or stop icon on tool bar Form properties Properties list has a predefined value (numeric or string) and could be changed, some properties could be rewritten like caption, and some could be selected from option list by pressing on down arrow on the side. Others could be rewritten or by browsing the computer files when the user clicks on the dotted button on the right side a dialogue box appears. The browsing button appears when the user clicks inside the box. The most important properties of the form are listed in the following table:

Page 18: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Creating adding and removing forms in project:- Create :- Select add form from the project menu or click on the form button on the toolbar. In the add form diaglog box , select the new tab and indicate the type of form you want , such as form , about ,splash. Click on open. The new form will be displayed on the screen and be added to the project explorer window. Add :- Select add form the project menu or click on the form button on the toolbar. In the add form dialog box , select the existing tab and select the folder and file which you want. Click on open. Remove :- Select the name of the form , from the project explorer window. Choose either the remove file from the project menu or right click on the filename to display the shortcut menu. Select remove filename. Hide method:- The hide method of a form removes the form from the screen. But the form still remains in memory. Syntax – Formname.hide To refer to the form in which code is currently running.we can refer to the form by its name like Form.hide When a form is hidden ,we can still access its properties and code. For example we can change the setting of its control properties or call , any public function in the form, even its event handlers. After manipulating controls on the form we can display it with show method. Show method:- If the form is loaded but invisible , the show method brings the specified form on to of every other window on the desktop. Syntax - formname.showmode The formname variable is the form’s name and the optional argument mode determines whether the form will be modal or modaless. Values are 0 – modeless(default) 1 – model Modeless forms are the norm. they interact with the user and allow the user to switch to any other form of the application. If the option mode is not specified the show method displays the form as modeless. Example:- if we are in form1 and we want to display form 2 the syntax would be Form2.show Load statement : - The load statement loads a form into memory but does nto display it. When you will code the statements for a form, the form_load event of the form will be triggered. Syntax- load formname Unload statement : - the unload statement removes a form the memory and screen.when you code the unload statement for a form, the form_unload event of that form will be triggered. Syntax - unload formname The unload event is also triggered when the user clicks the windows “close” button(“X”)on the form. Me keyword : - the keyword me is used to refer the current form. You can use me in place of the form name when coding form statements and methods.

Page 19: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Example - Sub changeformcolor(formname as form) Randomize() Formname.backcolor=color.formargb(rnd()*256,rnd()*256,rnd()*256) This procedure can be called and passed into the current instance of the form class as an argument using the following statement Changeformcolor(me) Referring to objects on a different forms - Each form is a separate module with its own variables ,objects , properties and code, One way to reference an object in another form module is to expand the reference to include the name of form Formname.objectname.property Using this you can set the properties of objects in a form before displaying it. Example - Frmsummary,lbltotalamount.caption=mcrtotal Frmsummary.show vbmodal Consequently , you can use the same name for the objects into two different forms.

Page 20: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Unit V Introduction to database connecting tools : -

ADO:- The ADO (ActiveX Data Object) data control is the primary interface between a Visual Basic application and a database. It can be used without writing any code at all! Or, it can be a central part of a complex database management system. This icon may not appear in your Visual Basic toolbox. If it doesn’t, select Project from the main menu, then click Components. The Components window will appear. Select Microsoft ADO Data Control, then click OK. The control will be added to your tool box. The basic steps which are required in order to access and manipulate data by using ADO given below.

Create a connection object to connected to the database. Create a recordset object in order to receive data Open the connection. Populate the record set. by opening it and passing the desired table name or SQL statement as a

parameter to open function. Do all the desired searching/processing on the fetched data. Commit the changes you made to the data by using update or updatebatch methods. Close the record set. Close the connection.

DAO :- When Visual Basic first started working with databases, it used the Microsoft Jet database engine, which is what Microsoft Access uses. Using the Jet engine represented a considerable advance for Visual Basic, because now you could work with all kinds of data formats in the fields of a database: text, numbers, integers, longs, singles, doubles, dates, binary values, OLE objects, currency values, Boolean values, and even memo objects (up to 1.2GB of text). To support the Jet database engine, Microsoft added the data control to Visual Basic, and you can use that control to open Jet database (.mdb) files. Microsoft also added a set of Data Access Objects (DAO) to Visual Basic: • DBEngine—The Jet database engine • Workspace—An area can hold one or more databases • Database—A collection of tables • TableDef—The definition of a table ADODC:- ADODC are active x data controls and are different form ADODB. It is suitable for small projects. As compared to ADODB, it is better to use but some times it produces eroor for “multiple operation”. ADODB:- ADODB are Active X data objects which are used to create and establish connections to database and other data sources. It is suitable for making big software. Creating the database files for use by visual basic(using ms- access):- Neither ADO.Net nor ActiveX Data Object(ADO) provides the means to create Microsoft access database. Howe ever , we can create access database by using the Microsoft jet OLE DB provider and Microsoft ADO Ext. 2.7 for DDL and security(ADOX) with the COM interop layer. Use the following code to create database: Public class form Private sub btnload_click(byval sender as system.objcect, _byvalue as system.eventargs)_handlesbtnload.click Createaccessdatabase(“c:\test\testdb.mdb”)msgbox(“database created”) End sub Public function createacess database(byval databasefullpath as string) as Boolean Dim bans as Boolean

Page 21: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

Dim cat as new ADOX.Catalog() Try Dim screatestring as string Screate string=”_provider=Microsoft.jet.OLEDB.4.0; Data source=”&_databasefullpath Cat.create(screatestring) Bans=true Catch excep as system.runtime.interoservices.comexception Bans=false Finally Cat=nothing End try Return bans End function End class Data control and their properties:- data control can be used to write VB programs than can easily manipulate database created with ms acess,foxpro,oracle and sql and many more.Various data controls of VB are data aware and can be bound to data control. The two key properties are included in data control Databse name is the filename of a compatible database and record source is the name of the table in the database. Steps to display data on data control:

1. Select the control from the toolbox and draw it on the form. Its default name is data1. 2. Set the databasename property to the filename of the database you want , in the properties

window. 3. Click on the ellipsis button to use a “file open” dialog. 4. Set the recordsource property to the name of a database table within the data you selected. 5. Kept a textbox on you form. 6. Set the datasource property or the textbox to data1. 7. Set the datafield property for the textbox to the field you wish to display. 8. You can place a lable control infront of the text box with the fieldname selected in step 7. 9. For each field , you want to display , repeat steps 5-8. 10. Run your application.

Using list boxes and combo boxes as data bound controls - When you have a list of acceptable values for a field, you can see the values by using data bound lists. By using a combo box, you can create a lookup table of acceptable values for a field. You can bind the combo vox to a field in the database and specify initial values for the list property. Below figure shows a combo box as a data bound control, displaying the subject field from the database. When the user selects a subject from the dropdown combo box, the contents of the field are updated to the new value. Updating a Database File – A project that displays data from a database allows updates automatically. If you do not want the user to change the data, you must set the read only property of the data control to true. You can also keep the user from making changes to the data by displaying fields in labels rather than in text boxes. The recordset objects have an update method, which can be used to save any changes in the data. Updating is performed automatically most of the time because visual basic automatically executives the update method any time the user clicks one of the navigation buttons or executes the move methods. Adding a New Record: In order to add a new record, invoke the “AddNew” method, which will create a blank record in the memory. This blank record becomes the current record. After you enter the data, you can add new record to the database by either of the following methods:

a) Moving to another record.

Page 22: SYLLABUS B.Com (Computer) VI SEM Subject Visual …rccmindore.com/wp-content/uploads/2015/06/Visual Basic...45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, B.Com

45, Anurag Nagar, B/H Press Complex, Indore (M.P.) Ph.: 0731-4262100, www.rccmindore.com

b) Invoking the ‘Update’ method. The following code is used to for the Add button:

Private Sub CmdAdd_Click Customerdata.Recordset.AddNew

The AddNew method adds a new record at the end of the file and clears the contents of the textboxes. In order to set the focus on the TextBox where you will enter the Customer Code, add the next statement. Ccode.SetFocus End Sub Deleting a record: In order to delete a record, invoke the ‘delete’ method. The current record that is displayed will get deleted. Once this record is deleted, we move the record pointer to the next record. This is required because the record pointer will continue pointing to non-existing record, once the current record has been deleted. Although the record is deleted in the database, the data will be still visible to the user. If the user clicks on the delete button second time, an error will occur. To avoid this situation, we need to add some code, which described below: Private Sub CmdDelete_Click Customerdata.Recordset.Delete The delete method deletes the current record. In other to move to the next record, add the next statement: Customerdata.Recordset.MoveNext End Sub If you invoke the ‘MoveText’ method after deleting the last record, you will get a message displaying ‘No current record’. In order to avoid this, your code needs to be modified. The modified code will look like this: Customerdata.Recordset.Delete If not Customerdata.Recordset.EOF Then Customerdata.Recordset.MoveNext Else Customerdata.Recordset.MoveLast End If