Visual Basic6.0

Embed Size (px)

Citation preview

  • 7/30/2019 Visual Basic6.0

    1/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 1

    ~ ~ ~ INDEX ~ ~ ~TOPIC PAGE

    INTRODUCTION of VISUAL BASIC .................. 02

    INTEGRATED DEVELOPMENT ENVIRONMENT...... .................. 02 ~ 03

    STANDARD TOOLBAR ................................................. .................. 04

    TOOL BOX..................................................................... .................. 05 ~ 06

    CREATING PROJECTS. .................. 06

    MODES in VISUAL BASIC .................. 06

    LABEL, COMMANDBUTTONS and TEXTBOX... .................. 07 ~ 08

    VARIABLE, DATATYPES.. .................. 09 ~ 10

    OPERATORS.. .................. 10 ~ 11

    CONTROL STRUCTURE

    ... .................. 11 ~ 13 LOOPING STATEMENT .................. 13 ~ 15

    ARRAYS.. .................. 15 ~ 16

    PROCEDURES.. .................. 16

    FUNCTIONS.. .................. 16 ~ 17

    MENUand MENU EDITOR .. .................. 17 ~ 19

    MDIand SDI.. .................. 19 ~ 20

    LISTBOX and COMBOBOX.. .................. 21 ~ 22

    IMAGEBOX and PICTUREBOX

    ... .................. 22 FRAME, OPTIONBUTTON and CHECKBOX.. .................. 22 ~ 23

    TIMER CONTROL . .................. 24 ~ 26

    SCROLL BAR . .................. 26

    ADODC.. .................. 26 ~ 27

    RECORDSET PROPERTIES and METHODS... .................. 27

    DRIVELIST, DIRLIST and FILELISTBOX.... .................. 27

    SHAPE and LINE CONTROLS...... .................. 27 ~ 28

    OLE CONTROLS....................... .................. 28

    Note: If you find any typing or printingerror/s, inform immediately to your branch office.

    http://c/Documents%20and%20Settings/All%20Users/Documents/HTML/notes/P/Progra4.htmlhttp://c/Documents%20and%20Settings/All%20Users/Documents/HTML/notes/P/Progra5.htmlhttp://c/Documents%20and%20Settings/All%20Users/Documents/HTML/notes/P/Progra5.htmlhttp://c/Documents%20and%20Settings/All%20Users/Documents/HTML/notes/P/Progra4.html
  • 7/30/2019 Visual Basic6.0

    2/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 2 VISUAL BASIC ADCPM

    Introduction of VISUAL BASIC:Visual Basic6.0 is a powerful programmingsystem that helps one to develop sophisticated, graphical applications. It is themodified version of BASIC (Beginners All purpose Symbolic Instruction Codes)Language (1960). It is in use since 1998. It is also called as Rapid ApplicationsDevelopment system, is used to build applications quickly. These applicationscan be run on Microsoft Windows environment.

    The original Visual Basic for DOS and Windows were introduced in 1991.Visual Basic 3.0 (a vast improvement over previous versions) was released in 1993.

    Visual Basic 4.0 released in late 1995 (added 32 bit application support).

    Visual Basic 5.0 released in late 1996. New environment, supported creation of

    ActiveX controls, deleted 16 bit application support.

    Features of Visual Basic: Visual programming environment provides allfeatures that are required to develop a graphical user interface, as ready to usecomponents.

    1. Visual Basic provides a Common Programming Platform.

    2. Visual Basic support Event Driven Programming.3. Visual Basic offers many Useful Tools for Programming.

    4. Visual Basic provides many Supporting Wizards.

    5. Visual Basic provides ActiveX Support forData Control.

    6. Visual Basic provides Quick Error Detection orCorrection.

    7. Visual Basic is a Faster Compiler.

    8. VB allows Database Integration with Wide Variety of applications.

    9. Visual Basic can be used as a New Data Report Designer.

    10. VB has some Additional Internet Capabilities.

    What is IDE?The working environment in Visual Basic6.0 is integrated with many differentfunctions. Such as: Designing, Editing, Compiling, Debugging with in a commonenvironment. For this reason, the working environment of Visual Basic is alsoreferred to as the Integrated Development Environment (IDE).

    COMPONENTS:

    1. Form Window: In form window you can design a form by the

    different controls are placed on the form.2. Project Window: In the project explorer, window shows the list of

    form and modules in a project.

    3. Property Window: The property window lists the properties of aselected control or for the form as it caption,sizes, color, etc.

    4. Form Layout Window: The form layout window shows how big a formis in relation to the screen.

    5. Tool Bar: A tool bar is a bar that displays icons forcommonly used task.

    6. Tool Box: A tool box is a window that displays a set of toolsthat may be used to places controls on a form.

  • 7/30/2019 Visual Basic6.0

    3/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 3

  • 7/30/2019 Visual Basic6.0

    4/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 4 VISUAL BASIC ADCPM

    Standard Toolbar: Standard toolbar contains some commonly used buttons.

    Icon Name DescriptionStandard EXE project To add the Standard EXE project.

    ActiveX EXE project To add the ActiveX EXE project.

    ActiveX DLL project To add the ActiveX DLL project.

    ActiveX control To add the ActiveX control/User control.

    Add Form To add the new form in your project.

    MDI Form To add MDI form in your project.

    Module To open the module code.

    Class Module To open the Class Module.

    User Control To open the User Control.

    Property Page To open the Property page.Menu Editor To open the Menu Editor dialog box.

    Open Project To open the Open Project dialog box.

    Save Project group To open the Save File As dialog box to save theproject and forms.

    Cut To cut the control from the form.

    Copy To create a copy of the control in the form.

    Paste To paste the copied control in the form.

    Find To open the Find Dialog box.

    Undo Delete To repeat the last delete control.

    Redo Delete To repeat the last Undo delete.

    Start To start compiling/run the form.

    Break To break the form compiling.

    End To end/stop the form compiling.

    Project Explorer To open the Project Explorer.

    Properties Window To open the Properties Window of the controls.

    Form Layout Window To open the Form Layout Window.

    Object Browser To open the Object Browser.

    Toolbox To open the Toolbox.

    Data ViewVisual ComponentManager

    To open the Visual Component Manager.

  • 7/30/2019 Visual Basic6.0

    5/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 5

    Visual Basic Tool Box:The Visual Basic toolbox contains the tools that allowyou to draw controls on your forms.

    Tool Name Description

    PointerControl

    This tool lets you select, resize or move a control.

    Picture Box This control is used to display graphical images.

    Label A label control is a graphical tool that used to display textthat a user can not change directly.

    Text Box A text box is a control that displays information that youentered during design mode.

    Frame A frame control is used to separate different groups ofcontrols on form.

    Command

    Button

    A command button control is used to begin, interrupt or

    end a process.Check Box It is used to allow the user select multiple choices.

    Option Buttonor

    Radio Button

    An option button is used to display an option that can beturned on or off. Option button is used for a group ofoption wherefrom user can select just one.

    List Box A list box control displays a list of items from which theuser can select one or more items.

    Combo Box A combo box combines the features of a Text box and aList box control.

    Horizontal &Vertical

    Scroll Bars

    The Horizontal & Vertical scroll bar controls display a longstripe with an indicator that lets the user select a valuebetween the two ends of a control.

    Timer It is an invisible control which is added to a form if sometask is to be repeated at regular intervals.

    Drive List Box This control displays the names of the drives available on

    a PC.Directory Listbox

    It lists the folder in the current drive, the drive mentionedin its Path property.

    File List Box This control locates and lists files in the directory whoselocation or path has been specified in the Path property ofthe File list box.

    Shape The shape control is a graphical control that is used todisplay a rectangle, circle, square, oval etc.

    Line This control is a graphical control that is used to display ahorizontal, vertical or diagonal line.

    Data This control is used to perform data access operations.

  • 7/30/2019 Visual Basic6.0

    6/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 6 VISUAL BASIC ADCPM

    Image This control is used to display a graphic.

    OLE This control enables you to add insertable objects to yourforms.

    Creating an Application (New Project):The following steps are followed by the user for opining a new project open:

    1. Click at start button.

    2. Click at Program and then Microsoft Visual Studio and click on MicrosoftVisual Basic.

    3. After clicking Microsoft Visual Basic, a wizard will be open. Here you canselect type of new Project.

    A project in Visual Basic is a collection of several different types of file that make

    a pure program.Various types of program that you can create here:

    Standard EXE project.

    ActiveX EXE/DLL

    ActiveX Control etc.Visual Basic Application Modes:

    Visual Basic application works in three modes:

    1. Design mode: When a new application is being created or designed, it

    is in design mode.2. Run mode: when the application is executed, it is said to be in run mode.3. Break mode: while an application is in a state of suspension, it is said

    to be in break mode.

  • 7/30/2019 Visual Basic6.0

    7/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 7

    Adding Controls to a Form: Visual Basic provides a tool box contains thetools that you can used to draw controls on your form. When you want to addcontrol in your form, first click on the control and then go to form and click whereyou want to draw control by pressing left mouse button drag the control.

    Writing Event-Driven Code: Codes in a Visual Basic application is dividedinto smaller blocks are called procedures. Control event procedure is a

    procedure contains codes that executed when an event occurs.To create an event procedures double click the object for which event procedureto be written. It will invoke code editor window where you can write the code foran event associated with the selected control.

    Running an Application:When you want to run a program, then you have to follow these steps:

    1. After creating the code in the code window, then select start option inrun menu through menu bar or use F5 key.

    2. Now your program in the run mode and you can put the data and show

    the result. Now you can run any application created with Visual Basic.

    WORKING with LABEL, COMMANDBUTTONS and TEXTBOX: LABEL Control:The Label control displays text that the user cannot directly change. Basicallylabels are used to identify controls that do not have their own Caption property.The actual text displayed in a label is controlled by its Caption property.

    Property Description

    Caption To display the text in the Label control.

    Font To specify the display font, style and size for the Caption of theLabel. Font is selected by clicking at () button after clicking inFont property box.

    Name To name the label control. VB identifies a label through the textgiven under this property.

    Alignment To specify the alignment of labels caption.

    0 : left Justify

    1 : Right Justify

    2 : CenterWordWrap This property is used to set the word wrapping option for the

    caption text. By default if the text exceeds the width of the Label,the text wraps to the next line. However, if the text goes beyondthe height of Label then it is clipped.

    Autosize Causes the control to horizontally expand and adjust to the size ofits contents.

    BorderStyle If this property set to 1, the label appears with a border- giving it alook similar to a text box.

    TextBox Control:Text boxes are versatile controls that can be used to get input from the user or todisplay text. Basically the Text Box control is a small text editor that provides all

  • 7/30/2019 Visual Basic6.0

    8/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 8 VISUAL BASIC ADCPM

    the basic text- editing facilities. That are,

    You can insert and select text You can scroll the text if it does not fit in its visible area. You can even exchange text with other application through the clipboard.

    Property Description

    Name To specify the name

    Text To specify text to be displayed in the textbox.

    MaxLength To set the maximum number of characters allowed in the textbox.

    MultiLine If it set to true, allows multiple lines of text in the TextBox.

    Scrollbar

    To attach a built in scroll bar to the textbox.

    0 : none

    1 : horizontal

    2 : vertical

    3 : both

    PasswordTo set any special character, which you want to display at theplace of input password.

    SelText Property that returns the selected text.

    Command Button:The command button ActiveX controls allows users to click it to perform actions.When a user chooses the button, it not only carries out the appropriate action, italso looks as if its being pushed in and released. Whenever the user clicks abutton, the click event procedure is invoked. You can place code in the Click

    event procedure to perform any action you choose.

    Property Description

    BackColor This property specifies the command button background color.Before setting the background color for command button youmust change the style property from 0-Standard to 1-Graphical.

    Cancel This property determines whether the command button gets aClick event if the user presses Esc.

    Caption To hold the text that appears on the command button.

    Font Produces a font dialog box in which you can set the caption fontname, style, and size.

    MousePointer This property determines the shape of the mouse curser whenthe user moves the mouse over the command button.

    Height Holds the height of the command button in tips.

    Picture This property holds the name of an icon graphic image thatappears on the command button as long as the Style property isset to 1-Graphical

    TollTipText To hold the text that appears as a ToolTip at runtime.Visible This property determines whether the command button appears

    or is hidden from the user.

  • 7/30/2019 Visual Basic6.0

    9/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 9

    Variable: Sometimes you need to store some values while your program isrunning .For example, a program handling banking transaction might need tostore the total amount deposited on a day. Since this value is going to vary fromtime to time, it is to be stored location whose contents can be changed or varied.This storage location is called a variable.

    A named storage location, whose contents can be varied, is

    called variable.Datatype:Visual Basic provides facilities to work with different types of data.These different types of data can be worked with through datatypes. VisualBasic provides a following datatypes:

    Boolean, Byte, Currency, Date, Double, Int, Long, Single, String, Variant etc

    Datatypes are actually means to identify the type of data andassociated operations with it.

    Datatype Description

    Boolean This datatype can take one of the two values true or false.Byte This datatype allows positives integers in the range 0 to -255.

    Currency This datatype is used to hold currency data.

    Date This datatype is used to hold date and time.

    Double The datatype to hold numeric values.

    It is short for double-precision values.

    Integer The datatype to hold non-fractional value (range -32768 to +32767)

    Long The datatype to hold integer values larger than those supported by

    integer datatype. It is short name for Long Integer.

    Single It allows upto 6 digits of precision.

    String Datatype to hold multiple characters textual data.

    It can store data having digits, alphabets and other characters.

    Declaring Variables: Declaring variable means telling the program about it, inadvance. A variable can be declared as per following syntax:

    Syntax: Dim[As],[[As]]

    Where Dim is the keyword that tells VB that a variable is being declared.

    is the variable name.

    As is another keyword that tells VB the datatype of the variable.

    And the is a legal datatype in VB.

    [ ] brackets mean the part in it is optional.Example: Dim rollno as Byte.

    Variable Scope: When you declare a variable, its not necessary that you canuse them everywhere in your application. This is because a variable may not beaccessible everywhere in the application. The area of program is accessible; it is

    called its scope.A part of program in which a variable is accessible is known asits scope. In Visual Basic, three types of scope:

    1. Private or Local Scope: If you are giving a Dim statement within a

  • 7/30/2019 Visual Basic6.0

    10/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 10 VISUAL BASIC ADCPM

    procedure, it means that this variable will be available and accessibleonly within this very procedure. And thus this variable is said to bePrivate orLocal to this procedure.

    A variable that can be used only in one procedure, in which itis declared, is said to havePrivate Scope.

    2. Module Scope: Modules are a place where you can put your

    commonly used routines, functions, constants etc.A variable that is available in side a module that is to all theseprocedure in then module is said to have Module Scope.

    3. Public Scope: The variable available to all the modules andprocedures in an application are said to have Public Scope. Anapplication can have many modules and procedures and they all canshare Public variables Public keyword declares public variables. Sincethe Public variables are available publically or globally to all modules ofthe application, these are also known as global variables, these areavailable with global scope.

    Saving a Form:When you want to save a new form, then you can use thefollowing steps:

    1. First goto File menu and click on it, now a dropdown submenu will open.2. Now goto save form (number) and click on it.3. Now Save File as dialog box will open. Here you can give the name of

    form and click on Save Button.

    In this way you can save a new form.

    Saving a Project: When you want to save a new project, then you can use thefollowing steps:

    1. First go to File menu and click on it, now a dropdown menu will open.2. Now go to save Project (number) and click on it.3. Now Save Project as dialog box will open. Here you can give the name

    of Project and click on Save Button.

    In this way you can save a new Project.

    PROGRAMMING BASICS

    OPERATORS in VISUAL BASIC:Operators are the symbol for word that trigger an action on some data, the dataon which operator operate are called operands.

    Operators can be divided into following categories:

    1. Arithmetic Operator

    Operator Operation Example

    + For addition 12 + 5.5 =17.5

    - Subtraction 12 - 5 =7

    * Multiplication 12 * 5 =60/ Division 7 / 2 =3.5

    \ int division 7 \ 2 =3

  • 7/30/2019 Visual Basic6.0

    11/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 11

    Mod Modulus 7 Mod 2 =1

    ^ Exponentiation 7 ^ 2 =49

    2. Relational Operator

    Operator Function Example

    = Equal Val1=Val2 is false

    < > Not Equal Val1< >Val2 is true> Greater than Val1>Val2 is true

    =Val2 is true

    < = Less than or Equal to Val1=300 AND Val1=300 and Val10) or not.Solution: Dim Num As integer

    Num =Val(txtNum.Txt)

    If Num>0 Then

  • 7/30/2019 Visual Basic6.0

    12/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 12 VISUAL BASIC ADCPM

    MsgBox(number is positive)

    End If

    If-Then-Else Statement: If the expression evaluates to true the statementsbetween, If and Else are executed, otherwise, the statements between Else andEnd If are executed.

    Syntax: If(Boolean Expression) Then

    StatementElse

    Statement

    End If

    Example: WAP to input a number and check the number is Positive or Negative.

    Solution: Private Sub Command1_Click()

    Dim num As Integer

    num =Val(Text1)

    If num > 0 Then

    MsgBox (the number is Positive)

    Else

    MsgBox (the number is Negative)

    End If

    End Sub

    SelectCase Statement: VB supports a statement, called Select Casestatement that handle such multiple choice condition better than if else the selectcase statement can be use when multiple if statement become difficult to read.

    Syntax: Select Case expressionStatement

    End Select

    Example: Obtain a character from uses and check whether it is an uppercasecharacter or a lowercase character or a digit or any other character.

    Solution: Ch =InputBox(Enter a character)

    Select Case Asc(ch)

    Case Asc (A) to Asc (Z)

    MsgBox it is an uppercase character

    Case Asc (a) to Asc (z)

    MsgBox it is a lowercase character

    Case Asc (0) to Asc (9)

    MsgBox it is a digit

    Case Else

    MsgBox it is any other character

    End Select

    Example: WAP to input day number(dno) and print day name.

    Private Sub Command1_Click()Dim dno As Integer

    dno =Val(Text1)

  • 7/30/2019 Visual Basic6.0

    13/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 13

    Select Case dno

    Case 1

    MsgBox (Monday)

    Case 2

    MsgBox (Tuesday)

    Case 3

    MsgBox (Wednesday)Case 4

    MsgBox (Thursday)

    Case 5

    MsgBox (Friday)

    Case 6

    MsgBox (Saturday)

    Case 7

    MsgBox (Sunday)

    Case Else

    MsgBox (Wrong Day number)

    End Select

    End Sub

    LOOPING STATEMENTVisual Basic provides mainly three types of looping structure.

    1. For Next:This loop is used when you know exactly how many timesthe code must be repeated.

    Syntax: For =Start to EndBody

    Next

    Example 1: Loop to print odd numbers between 50 and 100.

    Solution: For x =50 to 100 step 2

    print x

    Next x

    2. Do loop Structure:A do while loop repeat as long as the condition

    evaluated to true and a do until loop repeat as long as then givencondition evaluate to false.

    a. Do While Loop: This loop is used to when you want to use thiscondition first.

    Syntax: Do While ()Body

    Loop

    Example: Count the numbers entered and calculate their sum.

    Dim sum As Single, num As Single, coun As Integer

    num =InputBox(enter a number)

    coun =1

    sum =sum + num

  • 7/30/2019 Visual Basic6.0

    14/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 14 VISUAL BASIC ADCPM

    Do While num 0

    num =InputBox(enter a number)

    num =sum + num

    coun =coun + 1

    loop

    print coun;

    print sumb. Do Loop While: In this loop the bodies of loop execute first

    and then check the condition.

    Syntax: Do

    Body

    Loop while ()

    Example: Do

    num =InputBox (enter a number)

    sum =sum + num

    Loop while num 0

    c. Do Until Loop:It is similar to do while loop but its body repeatsuntil the condition false.

    Syntax: Do until ()

    Body

    Loop

    Example: Do until num =0

    num =InputBox(enter a number)

    sum =sum + numLoop

    d. Do Loop Until: It is opposite to do until loop. In this loop,execute the body first.

    Syntax: Do

    Body

    Loop until ()

    Example: Loop to print first n squares lesser than 50.

    Solution: dim squar as integer, num as integernum =1

    squar =num * num

    Do

    print squar

    num =num + 1

    squar =num * num

    Loop until squar>=50

    3. While Wend: This loop is replaced do while loop at this time.Syntax: While ()

    BodyWend

  • 7/30/2019 Visual Basic6.0

    15/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 15

    Example: While num 0num =InputBox(enter a number)sum =sum + numWend

    TERMINATING CONTROL STATEMENT: Some time you may need to exitfrom a loop even through terminating condition has not reached. In such case,you can use exit statement of Visual Basic. The syntax of exit statement is.

    Syntax: Exit For for exiting from a For loop

    Exit Do for exiting from a Do loop

    Example of Exit For: For x =1 to 10

    For y =1 to x

    If (y mod 3 =0) Then

    Exit For exit from inner For

    End If

    Next y

    Next xExample of Exit Do: Dim check, counter

    Check =true: counter =0 initialize variables

    Do outer loop

    Do While counter

  • 7/30/2019 Visual Basic6.0

    16/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 16 VISUAL BASIC ADCPM

    For i =1 to 5

    arr (i) =i

    Next i

    CONTROL ARRAY: A control array is a group of controls that share the samename and type.

    Adding and Removing Controls:Adding controls with Control arrays uses few

    resources then simply adding multiple controls of the same type to a form atdesign time. Control array are useful if you want to several controls to share code.Example: If there option buttons are created as a control array, the same code isexecuted regardless of which button was clicked.

    Using Control Array: Using control array mechanism each new control inheritsthe common event procedures already written for the array.

    Example: If your form has several text boxes that each can written receive adate value. Control array can be setup so that all of the text boxes share thesame validation codes.

    PROCEDURES - Writing Modular Codes:A procedure is a name unit of a group of program statementsthat perform a well define task.

    There are several types of procedure used in Visual Basic:

    1. Sub Procedure: A sub procedure or subroutine or sub is a procedure thatperforms a task but does not return a value. A sub or subroutine is a block ofcode that is executed in response to an event. A sub ca receive a valuesperform a series of statement and change the value of its arguments. These

    are of two types: General procedure: A General procedure is the one that you create for

    your own specific purpose. A general procedure tells the application howto perform a specific task.

    Event procedure:An Event procedure is a procedure associated with aspecific event of an object and is named in a way that indicates theobject and the event clearly.

    2. Function Procedure: A function procedure that performs specific task thatreturns a value. A function procedure is a separate procedure that can take

    arguments, perform a series of statements and change the value of itsarguments.

    3. Property Procedure: Property are the code which when a property of anobject gets a new value or when the value is retrieved.

    Syntax: Sub procedure name (argument list)

    Statement

    End sub

    Event procedure is always a sub procedure.

    FUNCTION:Functions are very similar to subs but there is big difference between the two.This is because a function is a procedure that returns a value.

    Syntax: Function [procedure name] (argument list) [As type]

  • 7/30/2019 Visual Basic6.0

    17/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 17

    Statement

    End function

    Type of Function: There are two types of functions:

    a. User-Define Function: User-define function is created by user.

    b. Built in function or Library: Built in functions are parts of Visual Basicand these are predefined.

    User-Define functions have two step processes:Functions Definition: The actual code of a function is written, this is known asdeclaring function there must be a function call. (The function is invoked this isknown as using function).

    Passing the Parameters: You can use two different methods for passing inparameters to a procedure.

    1. Call by value

    2. Call by ReferenceFor passing, the call by value parameter by Val Keyword is used and if you wantthe value by reference then be used By Ref Keyword.

    Modules: A module is a code container in Visual Basic that contains someprocedure and definition. There are three types module in Visual Basic.

    1. Form Module: Form module is a module that stores all the procedureand declaration pertaining the single form. The form module is savedwith .FRM extension.

    2. Standard Module: The standard module are the modules that storesgeneral purpose code of the application that is the code and declaration

    that are not specific to one single form of the application. The standardmodule is stores with .BAS extension.

    3. Class Module: Class module is a special code module that stores theblue print for user created custom object. Class module is saved with.CLS extension.

    INTRODUCTION of MENUS:A menu is continent and constant way of grouping commands so that theybecome greatly and easily accessible to user. There are two types of menu:

    1. Horizontal menu known as Menu Bar.2. Vertical menu known as Pop up or pull down menu.

    Creating Menu in Visual Basic:

    To add menu in a VB application, you need to invoke Menu Editor. The menueditor of VB is an interactive way to create and modify menu and get to withminimum coding you can also creates shortcut menus through Menu Editor.

    Design a Menu:

    To add menu to a Visual Basic application on you need to invoke menu editor.The menu editor of Visual Basic is an interactive way to create and modify menu

    and get two with minimal coding you can also creates shortcut menus throughMenu Editor.

    Menu Editor: When you open the menu editor first goes to tools menu and then

    Click on menu editor option or directly press CTRL+E.

  • 7/30/2019 Visual Basic6.0

    18/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 18 VISUAL BASIC ADCPM

    Menu Title and Naming Guidelines:

    When assign captions from menu items you should try the following guide lines.

    1. Items name should unique with in a menu.

    2. Items name may be singlecomponent and multiplewords.

    3. Each Items name shouldhave a unique mnemonicsaccess characters for userwho choose commandwith Keyboards.

    The access character should bethe first letter of menu title.

    Ellipses (...): An ellipsis shouldfollow names of command that

    required more information beforethey can be completed.

    Keep items name Short.

    Access Key:

    Access keys allow the user toopen a menu by pressing the ALT key and typing the designated letter. Once a

    menu is open, the user can choose a control by pressing the letter assign to it.

    To assign an access key to amenu control in the Menu Editor:

    1. Select the menu item towhich you want to assignan access key.

    2. In the Caption box, type anampersand (&)immediately in front of theletter you want to be theaccess key.

    Access key allow the user to open amenu by pressing ALT key &

    assigned letter.

    Example:

    Menu Control Caption Caption Property Access Keys

    Cut Cu&t T

    Copy C&opy O

    Paste &Paste P

    Delete De&lete LSelect All Select &All A

    Time/Date Time/&Date D

  • 7/30/2019 Visual Basic6.0

    19/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 19

    Creating Popup Menus:

    A popup menu is a floating menu that is displayed over a form independent ofthe menu bar. The items displayed on the popup menu depend on where thepointer was located when the right mouse button was pressed; therefore popupmenus are also called context menus.

    A Pop-up menu is a floating menu that is displayed over a form,

    independent of the menu bar.To create a popup menu, you first need to define a menu through menu editor.And also to make sure that this menu will not display on menu bar, you make itinvisible i.e., make sure that Visible check box in Menu Editor is not checked for

    this menu.

    After this, you need to write a special code for it that uses Popup Menu method.

    Any menu that has at least one menu item can be displayed at run time as apop-up menu. To display a popup menu, use the Popup Menu method.

    This method uses the following syntax:

    Syntax: PopupMenu menunameExample: The following code displays a menu named mnufile when the userclicks a form with the right mouse button. You can use the MouseDownor eventto detect when the user clicks the right mouse button, although the standard is touse the MouseUpevent:

    Private Sub Form_MouseUp (button as integer, shift as Integer,

    x as single, y as single)

    If button=2 then check if right mouse button was clicked.

    PopupMenu mnuFile display the file menu as a popup menu.End if

    End Sub

    MDI (MULTIPLEDOCUMENTS INTERFACE):

    MDI supports multipledocuments at the same time;thus, multiple- documents canbe opened simultaneously

    and worked upon in any MDIapplication.

    Example:Ms Office.SDI (SINGLE DOCUMENTINTERFACE):

    The single document interface supports single document in its window. Whenyou want to open new document then the previous one will be closed.

    Example:

    Paint, Notepad, WordPad, etc.

    Explorer:Creating Parent Form: An MDI application must have at least two forms. Oneparent forms and one or more child forms must be in an MDI. A parent form can

  • 7/30/2019 Visual Basic6.0

    20/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 20 VISUAL BASIC ADCPM

    contain many child forms. However, it can use only single parent form.

    Steps to create Parent form:

    1. Start a new project.

    2. Click on project

    3. Add MDI by clicking.

    4. After clicking, an MDI dialog box will be open.

    5. Now click on open button.

    6. MDI form will now add to your project.Create the application Child form:

    When you want to create an MDI child form:

    1. First go to project and click on it.

    2. A drop down menu will be open.

    3. In it click on add formCommand.

    4. And set its MDI Childproperties to true.

    Working with MDI:

    1. Start a new project.

    2. Click on project

    3. Add MDI form (click)

    4. After clicking, an MDIdialog box will be open.

    5. Now click on open button.6. MDI form will be get added to your project.

    a. First, ADD the simple form than one.

    b. Now click the MDI and open the MNO Editor.

    c. Create the Menu bar on the MDI Form.

    d. Now Click on First menu form and go to Code view and type thecode as

    If you want to show the form when you click the first menu

    option than write the codeForm1. Show

    e. When you run the MDI than get the properties of MDI by

    Go to Project and then Project Properties

    Now open the project properties dialog box. Where you go tostartup object and Set the MDI Form.

    f. Now Run the project -MDI will show in Run mode.

    g. Now click on form 1 menu. Form1 will be open in MDI form.Using of Common Dialog Box: The common dialog box control provides astandard set of dialog boxes for operations such as opening and saving filessetting print options and selecting color and font etc.

  • 7/30/2019 Visual Basic6.0

    21/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 21

    To use the Common Dialog Box Control follow these Steps:

    If you have not already done so, at the common dialog control to thetoolbox by selecting Components from the project menu.

    Select Microsoft Common Dialog Control 6.0 in the controls tabbeddialog, and then click the ok button.

    On the toolbox, click on common dialog control and draw it in a form.

    Note (It is invisible at the run time). At the run time, use the appropriate method as given below to display

    the desired dialog.

    Use of these Method:

    Method Dialog Displayed

    ShowOpen Open

    ShowSave Save

    ShowColor Color

    ShowFont FontShowPrinter Printer

    ShowHelp Help

    Use of this Function:

    If you want to invoke the file open dialog, box and retrieve selective file name.

    CommonDialog 1. ShowOpen

    filename=CommonDialog1. FileName

    File name: File name is the full path name of the file selected by the user. You

    can also set the File name property to a file name that displayed when thecommon dialog box is first opened. This allows the user to click the open buttonto open the preselected files.

    WORKING with CONTROLS:

    The List Box Control: The list box control presents a list of choices to theuser. The choices are presented one or more column if number of itemspresent in exceed what can be displayed in a list box. Scroll barautomatically appear on the control. Major property of list box control are:

    Property Description

    1. Multi-select: The property determines the user can select the listsmultiple item or not. It can take following values.

    0- Multiple selection not allowed

    1- Simple multiple selection.

    2- Extended multiple selections

    2. Sorted: Its property set to true.

    3. Style: This property determines the appearance of the control. 0, 1

    Common method of List Control:

    1. Add item:Syntax:

    List1. Add item ,

  • 7/30/2019 Visual Basic6.0

    22/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 22 VISUAL BASIC ADCPM

    Example:

    List1. Add item Bareilly, 4

    2. Remove item: An item is removedusing remove item method on thebasic of its index value.

    Syntax:

    List1. Remove item Example:

    List1. Remove item 0

    3. Clear list: List 1. Clear

    Combo box Control: It is similar to the list box in the since it containsmultiple item of which the user may select by but it takes less space onscreen. Combo box displays one line with the selected item.

    Difference between Combo and List Box Control:

    Is that combo box control allows the user to specify items those do not exist inthe list box.

    0- Drop down Combo

    1- Simple Combo

    2- Drop down list.

    WORKING with IMAGEBOX and PICTUREBOX:The Image control is used to display graphic. Image controls respond to the clickevent and can be used as the substitute or CommandButton.

    Image control can display graphics in following formats:

    .pnt (paint file)

    .icon, Meta files and hence to metafiles curser, .jpeg/.jpg files.

    Properties Description

    Appearance 0 for flat 1 for 3D

    BorderStyle 0 for none 1 for fixed single

    Picture This property determines which picture/graphic will bedisplayed in the Image. You can set it in the design time.

    Stretch The image is resized to fill the area of ImageBox, if the stretchproperty is set to false. The ImageBox controls behave likethe PictureBox with its autosize property.

    Loading a Graphic into ImageBox:

    When you want to load and change image at run time, you can do it by usingPicture Property and the load picture function.

    Image1. Picture =LoadPicture

    WORKING with FRAME, OPTIONBUTTON and CHECKBOX:

    Frame Control:A Frame control is used to group various controls. A Framecontrol is a container control, it contains other controls in it; it does not carryout any job/action by itself, it does not respond to any events by itself.

  • 7/30/2019 Visual Basic6.0

    23/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 23

    Property Description

    Caption To display text in Frame control

    Font To set font, style and size of caption-text of frame.

    Name To name the frame.

    Visible To set a value indicating whether the frame should be visible(value true) or not (value false).

    This value can be accessed also.

    BorderStyle Property used to specify the style of border.

    It can be either 0 - flat or 1 - fixed single.

    It appears under Appearance category.

    Appearance Property used to set the look of the frame.

    It can either be 0 - flat or 1 - 3D.

    It also appears under Appearance category.

    CheckBox:The CheckBox controls are used for offering a smallest of choices from which a

    user can choose one or more options.

    A CheckBox indicates whether a particular condition is on or off. You can usecheck boxes in any application to give users true/false or yes/no options.Because check boxes work independently of each other, a user can select anynumber of check boxes at the same time.

    Property Description

    Caption To specify text to be displayed.

    Name To specify the name

    Alignment To determine the alignment of the CheckBox with respect to caption

    Enabled To set a value that determines whether the checkbox can respondto user generated events.

    Value To specify the state of checkbox. If its value is true or 1, it meansthe checkbox is checked, otherwise not.

    OptionButton:The option buttons are used to offer a small set of options from which a user canchoose one. Option buttons present a set of two or more choices to user. Unlikecheck boxes, however, optionbuttons should always work as part of a group;selecting one option button immediately clears all the other buttons in the group.

    Properties Description

    Caption To display text for OptionButton

    Name To name the OptionButton

    Value To set the state of Optionbutton, if set to true the

    Optionbutton appears selected.Style 0 : for standard 1 : for graphic

    Alignment 0 : for left 1 : for right justify

  • 7/30/2019 Visual Basic6.0

    24/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 24 VISUAL BASIC ADCPM

    WORKING with TIMER CONTROLThe Timer control is a control that is actually invisible at run time but is used toperform some task or activity at regular intervals. Instead of making a noise or beeping, the timer control executes code, when

    the specified interval is complete. The Timer control counts down repeatedly. As long as the Enabled property

    is set to True. The Timer control is designed to work with very small amounts of time; themaximum setting is just a little longer than a minute.

    An important property of Timer control is the Interval. The Interval propertycontains a value that must range from 1 to 65,535. The value is in milliseconds,so an Interval value of 500 would equate to half a second. The Timer controlgenerates only one event: the Timer event.

    Example: Designing a digital clock using Timer control as shown below:

    Solution:

    1. Create a Form with 5 Labels on it:

    2. Now set the properties of these Labels as specified below:

    Control Property Setting

    Label1 Name

    Caption

    Font

    Alignment

    hr

    00

    TmsRoman,36pts,Bold

    2-center

    Label2 Caption

    Alignment

    Font

    :

    2-center

    TmsRoman,36pts,Bold

    Label3 Name

    Caption

    Font

    Alignment

    min

    00

    TmsRoman,36pts,Bold

    2-center

    Label4 Caption

    AlignmentFont

    :

    2-centerTmsRoman,36pts,Bold

    Label5 Name sec

  • 7/30/2019 Visual Basic6.0

    25/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 25

    Caption

    Font

    Alignment

    00

    TmsRoman,36pts,Bold

    2-center

    3. Now set the Form properties as follow:Caption Digital clock

    4. Now click on Timer control and add one timer control on your form. Theform would now look somewhat like the one shown below:

    5. Now add following code in the Timer event of Timer control:Private Sub Timer1_Timer()

    Dim hh As Byte

    Dim mm As Byte

    Dim ss As Bytess =Int(sec.Caption)

    mm =Int(min.Caption)

    hh =Int(hr.Caption)

    ss =ss + 1

    If ss =60 Then

    ss =0

    mm =mm + 1

    End IfIf mm =60 Then

    mm =0

    hh =hh + 1

    End If

    If hh =24 Then

    hh =0

    End If

    sec.Caption =Format(ss, 00)

    min.Caption =Format(mm, 00)

    hr.Caption =Format(hh, 00)

    End Sub

  • 7/30/2019 Visual Basic6.0

    26/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    Page - 26 VISUAL BASIC ADCPM

    6. Save your project and run it by pressing F5. Now running application willlook somewhat like the one shown in given screenshot.

    Scroll Bar: A scroll bar is a long strip with a moveable indicator that lets theuser selects a value between the two ends of a control.

    The scroll bar controls presents in two forms:

    1. Horizontal

    2. VerticalThe left and upper ends are for the minimum value and right and lower ends arefor the maximum value.

    To add a component ADODC:

    (Active data object dialog control)

    1. Click on project.2. Go to component and click on it.3. Component dialog box will be open.4. From where select the ADO data control 6.0 OLEDB.5. Write the check box.6. Apply and close.

    To add ADODC with Access database:

    1. Right click on ADODC.

    2. Click on ADODC property.3. Click on build event.4. Now open a data link property dialog box.5. Select the MS JET 4.0 OLEDB provider.6. Click on Next.7. Now select the database by clicking on 3dotes ().8. Open a select Access database dialog box will be open.9. Select BIBLIO.

    10. Click on open.11. Check the Blank password.12. Click on test connection.13. A message will show test connection succeeded.

  • 7/30/2019 Visual Basic6.0

    27/28

    BLY / June - 2010 COMPUTER PROGRAMMING Module 5

    ADCPM VISUAL BASIC Page - 27

    14. Click on ok.15. Click on Advanced.16. Select read and write permission.17. Now click ok.18. Click on Record source.19. Select the cmd table.

    20. Apply and ok.21. Now select all the text boxes and go to properties.22. Click on Data source select ADODC123. Apply the Data field property one by one.

    Working with Recordset Properties and Methods:

    Coding for ADODC:

    ADODC1. Recordset. MoveNext

    ADODC1. Recordset. MovePrevious

    Updating Recordset Data:ADODC1. Recordset. UpdateWhat is ADO?

    ADO stands for ActiveX Data Objects.ADO is the successor to DAO (DataAccess Objects)/RDO (Remote Data Objects).ADO is flattened it contains fewerobjects and more properties, methods and events.Working with DriveList, DirList and FileListBox:The three available controls are the drivelistbox, dirlistbox and filelistbox control,let you access the computers file system.

    Using these controls the user can troubles the host computer file system, locateany folder or files or any hard disc even on network drives the files controls areindependently of one another and each can exist on its own, but they are rarelyused separately.

    Functionality of these three files:

    1. DriveListBox: It displays the name of the drives within and connected to thePC, the basic property of this control is Drive.

    2. DirListBox: It displays the folder of the current time; the basic property ofthis control is Directory, which is the name of the folder.

    3. FileListBox: It displays the files of the current folder. The basic property ofthis control is also called path. The path name of the folder, whose files aredisplayed and other important property is pattern which lets you specifywhich file will be displayed with a file matching string such as *.doc or *.txt;you may write code in the change event procedure(Drive1_change())

    Dri1.path=Drive1.Drive

    Change event procedure of DirListBox:

    namely Dir1

    file.path=Dir1.pathSHAPE and LINE Controls:

    Shape and Line control are useful for drawing graphical elements on the surfaceof a form. These controls do not support any type of events; they are strictly for

  • 7/30/2019 Visual Basic6.0

    28/28

    Module 5 COMPUTER PROGRAMMING June - 2010 / BLY

    decorative purpose.

    The main properties ofShape and Line controls are:

    1. BorderColor2. FillColor3. BorderStyle4. BorderWidth

    5. FillStyle.Shapeis also an important property. It can be displayed rectangle, square, oval,circle, rounded rectangle and rounded square.

    Working with OLE Container ControlThe OLE container control is a way of supporting Object Linking andEmbedding. Object Linking and Embedding is a way to link applications. Theobject can be a graphic image or a piece of text or a table or even a soundclip.OLE provides the users the capability to create compound document thatcontain one or more objects from other applications.

    Properties of OLE Container Control:Property Description

    Class Returns the name of the application that produced theobject.

    SourceDoc Returns the data or a reference to the data.

    SourceItem Returns the image of the data. This applies to linked objectsonly.

    SizeMode Returns or sets a value specifying how the OLE container

    control is sized or how its image is displayed when itcontains an object.

    OLETypeAllowed This property setting determines whether the sourcedocument is embedded in or linked to the OLE containercontrol.

    Creating an Embedded Object:

    1. Create empty OLE container at design time. Draw an OLE containercontrol on your form by dragging. When Insert Object dialog boxappears. Choose Cancel or right click the control and select DeleteEmbedded Object if an embedded object already exists.

    2. Load embedded object at runtime. You can create an embedded objectat runtime by using CreateEmbed method in an empty OLE container

    control.

    The CreateEmbed method takes two parameters: SourceDoc and Class nameof the object. You can leave the source document parameter empty if you wantto a blank object. This line of code creates a Paintbrush picture object in an OLEcontrol named OLE1:

    OLE1.CreateEmbed ,Paint.Picture