Beruflich Dokumente
Kultur Dokumente
Page 1 of 121
Note:
This question-bank contains three sections.
Section-A contains 1 mark Multiple Choice Objective type questions.
Section-B contains 5 marks subjective questions.
Section-A contains 10 marks subjective questions.
Q8. In the IDE, which of following is used to design the layout of an application?
(a) Form Designer window (b) Project Explorer window
(c) Context Menu (d) Form Layout window
Q9. The location of the form on the desktop during execution is determined by the:
(a) Form Designer window (b) Project Explorer window
(c) Context Menu (d) Form Layout window
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 2 of 121
Q11. The location of the form on the desktop during execution is determined by the:
(a) Form Designer window (b) Project Explorer window
(c) Context Menu (d) Form Layout window
Q13. C is:
(a) A language widely used in UNIX
(b) A language based on BASIC
(c) Used primarily for business applications
(d) A GUI
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 3 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 4 of 121
Q36.A GUI:
a.) uses buttons, menus, and icons.
b.) should be easy for a user to manipulate.
c.) stands for Graphic Use Interaction.
d.) Both a and b.
e.) All of the above.
Q39.Which type of project can a developer choose in the New Project dialog box?
a.) Visual Basic Projects b.) Visual C# Projects
c.) Visual C++ Projects d.) Both a and b.
e.) All of the above.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 5 of 121
Q43.For which task does the IDE provide multiple ways to accomplish the task?
a.) Putting a control on the form
b.) Running the program
c.) Activating the property window for a control
d.) Both a and b.
e.) All of the above.
Q44.Which are the standard prefixes for the Button and Combo box controls respectively?
a.) btn and chb b.) btn and cbo
c.) bto and chb d.) bto and cbo
e.) cmd and cbo
Q45.Which are the standard prefixes for the text box and label controls respectively?
a.) tex and lbl b.) tex and lab
c.) txb and lbl d.) txb and lab
e.) txt and lab
Q47.Which is not a feature of a GUI that makes learning a program easy for users?
a.) Online help
b.) WYSIWYG formatting
c.) Dialog boxes
d.) Detailed key strokes and commands
e.) Icons
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 6 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 7 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 8 of 121
e.) Long
Q67.The Date data type does not hold which type of information.
a.) Seconds b.) Hours
c.) Days d.) Months
e.) Quarters
Q70.VB.Net identifiers:
a.) are case sensitive. b.) can begin with an underscore.
c.) can begin with a number. d.) Both a and b.
e.) All of the above.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 9 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 10 of 121
Q90.Which value for the ComboBox DropDownStyle property allows a user to type in
data?
a.) DropDown b.) DropDownSimple
c.) DropDownList d.) Both a and b.
e.) All of the above.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 11 of 121
Q98.What happens in a Select Case construct when a test value matches the test
expression?
a.) The corresponding block of statements is run.
b.) The next Case test value is checked.
c.) The Case Else statement is run.
d.) The Select Case construct is exited.
e.) An error is generated.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 12 of 121
Q101. Which DoLoop statement should be used to process test scores where a test
score over 100 is a signal to stop the processing?
a.) Do While Score > 100 b.) Do Until Score > 100
c.) Loop While Score > 100 d.) Loop Until Score > 100
e.) All of the above are valid for this situation.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 13 of 121
Q110.Code is:
a) Updateable in the form editor b) Instructions
c) Seldom used d) An object
Q111.Controls are:
a) Code b) Part of the menus
c) Rules d) Objects
Q112.In the IDE, which of following is used to design the layout of an application?
a) Form Designer window b) Project Explorer window
c) Context Menu d) Form Layout window
Q113.The location of the form on the desktop during execution is determined by the:
a) Form Designer window b) Project Explorer window
c) Context Menu d) Form Layout window
Q115.The location of the form on the desktop during execution is determined by the:
a) Form Designer window b) Project Explorer window
b) Context Menu d) Form Layout window
Q118.dim is a _______
a. data type b. keyword
c. operator d.symbol
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 14 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 15 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 16 of 121
Q143. Which argument in the KeyPress parameter list contains the Handled property?
a.) Sender b.) e
c.) Object d.) KeyPressEventArgs
e.) None of the above.
Q144. What is the name of the control for putting menus on a form?
a.) FormMenu b.) MenuForm
c.) MenuControl d.) MainMenu
e.) Menu
Q146. Which symbol creates an access key in the text of a menu item?
a.) @ b.) # c.) $ d.) % e.) &
Q149. What is the method used to activate the color dialog box?
a.) ActivateDialog b.) DisplayDialog
c.) ExhibitDialog d.) ShowDialog
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 17 of 121
Q150. The name of the class used to programmatically create a color dialog box is:
a.) Color b.) ColorBox
c.) ColorDialog d.) ColorDisplay
Q151. The name of the class used to programmatically create a font dialog box is:
a.) Font. b.) FontBox.
c.) FontDialog. d.) FontDisplay.
e.) FontDialogBox.
Answers
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 18 of 121
Q3. What is the difference between Event driven programming and procedural
Programming?
Ans. There different types of programming. And different languages at least support one of
them. We can categorize the programming into the following types.
1. Unstructured 2. Structured / Procedural 3. Object Oriented 4. Event driven.
1. Unstructured programming: - There is no specific flow of the program code, control can be
transferred from any where to any part of the program. More and more use of Go To statement in
the program helps to make the program unstructured.
2. Procedural programming: - In procedural programming, the program is divided in to the
small sub blocks. These blocks are called procedures and these procedures execute their source
code by calling. Procedural programming helps to make debugging easy.
3. Object Oriented programming: - This programming is based on the objects and classes.
4. Event driven programming: - Event or actions are performed on the specific component and
components respond according to these actions and execute the code written under the event
procedure.. Normally event procedures are local to the form in which they are declared and have
private access.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 19 of 121
Q4. Write down the names of different controls and their purpose. (v.imp)
Ans. Toolbar: - It contains a set of controls that are used to place in a form at design time
thereby creating the user interface area. User can include additional controls in the toolbox by
using the Components menu item on the project menu. A Toolbox contains the following controls-
The pointer helps to move and resize the control and Forms.
Label displays a text that user cannot modify or interact with.
Frame control serves as a visual and functional container for controls.
Check Box displays a True/False or Yes/No option.
Textbox is a control used to display message and enter text.
The List Box displays a list of items from which a user can select one.
Combo Box contains a Textbox and a List Box. This allows the user to select an item from the
dropdown List Box or to type a selection in the Textbox.
HScroll Bar and VScrollbar controls allow the user to select a value within the specified
range of values.
Timer control executes timer events at specified intervals of time.
DirListBox allows the user to select directories and paths, which are displayed.
Shape control adds a shape (rectangle, square or circle) to a Form.
Image control is used to display icon, bitmaps, metafiles, etc.
OLE controls used to link or embed an object display and manipulate data from other
Windows based applications.
PictureBox displays icons/ bitmaps and metafiles. It displays text or acts as a visual container
for other control.
CommandButton carries out the specified action when the user chooses it.
The OptionButton control, which is a part of an option group, allows the user to select one
option even if it displays multiple choices.
The DriveListBox displays the valid disk drives and allows the user to select one of them.
The FileListBox displays a set of files from which a user can select the desired one.
Line control draws a straight line to the Form.
Data control enables the user to connect to an existing database and display information from
it.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 20 of 121
Multi Line property allows the Textbox to accept and display multiple lines at run time.
Text property of the Textbox control sets a particular text in the control.
Visible property is used to unhide the object at run time.
Width property sets the Textbox to the desired width at design time.
The properties that are discussed above are design time properties that can be set at the design
time by selection the Properties window. But certain properties cannot be set at design time.
A method is an action that can be performed on objects. For example, a cat is an object. Its
properties might include long white hair, blue eyes, three pounds weight, etc. A complete
definition of cat must not only encompass on its looks, but should also include a complete
itemization of its activities. Therefore, a cat's methods might be move, jump, play, breathe, etc.
Similarly, in object-oriented programming, a method is a connected or built-in procedure, a
block of code that can be invoked to impart some action on a particular object. A method requires
an object to provide them with a context. For example, the word Move has no meaning in Visual
Basic, but the following statement, performs a very precise action.
Text1. Move 700, 400
The textbox control has other associated methods such as Refresh, Set Focus, etc.
The Refresh method enforces a complete repaint of the control or a Form. For example,
Text1. Refresh refreshes the Text Box.
The Set Focus method moves the focus to the control .For example
Tex1. Set Focus sets the focus to Textbox control Text1.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 21 of 121
Q7. Name and explain in brief various components of Visual Basic IDE.
Ans. IDE is Integrated Development Environment, which is commonly used in the
programming world to describe the interface and environment that we use to create our
applications. It is called integrated because we can access virtually all the development tools that
we need from one screen called an interface.
Main elements or components of IDE are:
a.Menu bar: - It contains different types of menus and a group of similar type instructions are
store in a single menu.
b.Toolbar: - It contains the shortcuts of the commands appearing in different menus. By using a
toolbar, user can use many items contained in different menus without opening a menu again &
again. User can directly select them from the toolbar.
c.Project Explorer :- it contains the list of forms, module and class modules used in the project
window
d.Properties:- It contains the list of properties for the selected control.
e.Toolbox: - It contains all the implicit controls used in VB.
f. Form Designer: - here we can design the form for vb project.
g. Object Browser: - It allow the user to browse through the various properties,
Q8. What is control? Can we create our own control in visual Basic?
Ans. A control is an object, which we place on the form to create a user interface. Examples of
controls are command button, textbox control, label, picture box etc. We can add more controls
by right clicking on the toolbox and then by selecting components from pop up menu. All the
controls can be accessed from the toolbox. Nearly every control has its own properties &
methods. We can set the properties of a control at design time and through coding. We can make
controls responding to the happening of events. For example: change method of textbox
responds to the code written in change method whenever the text in the textbox changes.
We can create our own controls in visual Basic & these are called ActiveX controls. User can
design their own controls or can extend the properties of existing controls. For example: you can
design a textbox control that accepts only numeric data.
Q10.What are classes and objects? Discuss how to add properties, events and methods to
a class module?
Ans.A class is a self-sufficient piece of code, which means you can move the class from one
project to another and it should, work properly without any modification. A class contains
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 22 of 121
properties and methods that all work together to perform a set of related operations. Object is an
instance of a class, which is used to access the members of class.
Methods: - Adding method to a class is just like adding procedure to a class module. You can
prefix the method name with Private or Public, depending on the scope of method.
Properties: - are variables used to store and retrieve data from classes. Properties that are
declared as public are called exposed properties. This means the data within the class can be
accessed from another procedure outside the scope of the class. The exposed properties make
up a portion of the classs interface. Private properties can be used to keep data and procedures
hidden behind the interface. This prevents programs from directly or unintentionally modifying the
contents of data required for the class to function properly.
Events: - are most interesting portion of class module. They allow the class to communicate
information back to an application when sets of conditions are met. This makes the application an
event driven application means it relies on events to make program operate.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 23 of 121
Q14.Write a program in which When you click on add button the new option button is
added and when a delete button is clicked, the option button got deleted. And when you
click an option button the color of the picture changes.
Ans. DIM MaxID as integer
Private Sub Add_Click ()
If MaxID =0 then MaxID =1
If MaxID >8 then Exit Sub
MaxID = MaxID +1
Load optbutton (MaxID)
Opt button (0). Set focus.
Opt button (MaxID). Top = Opt button (MaxID 1). Top + 400
Opt button (MaxID). Caption = Option&MaxID + 1.
End sub
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 24 of 121
up & down along a document. The scroll bars are the prime mechanism to view a large image or
text that doesnt fit entirely in a window. While scrolling a long document horizontally or vertically,
the user doesnt have to bother about line numbers or pixels.
The slider control is similar to the scrollbar control but it does not cover a continuous range of
values. The slider control has a fixed number of tick marks, which the developer can label. The
user can place the sliders indicator to the desired value. Scrollbar is used where continuous
value is important and slider control is used where discrete values are required, for example slow,
medium, fast etc. Scrollbar control is a long stripe with an indicator that lets the user select any
value between two ends of the control scrollbar can be of two types horizontal & vertical. The
basic properties of scrollbar control are Min, Max and value. Slider control is similar to the
scrollbar control but there is no continuity but there is some specific range e.g. slow, fast and very
fast. Slider control is not an intrinsic control of VB and we can add it from components dialog box
by selecting Microsoft common controls 6.0.
Q16.Write a program to make a text editor with cut and paste facilities.
Ans. Open a standard EXE project. Place a textbox and place four buttons on it. Name the
command button as cmdcut, cmdquit, cmdpaste and cmdreset. Name the text box as text editor
& add the following code.
DIM store as string
Private Sub cmdcut_click ()
Cuttext=Texteditor.Seltext.
store=Texteditor.Seltext.
Texteditor.Seltext=
Cmdpaste.Enabled=true
Texteditor.Set focus.
End Sub
Private Sub cmdQuit_Click ()
Unload me.
End Sub
Private Sub cmdreset_Click ()
Texteditor. Text=
Texteditor. Set focus
End Sub
Private Sub Text editor_Change()
Cmdcut.Enabled=True
Cmdpaste.Enabled=True
Cmdreset.Enabled=True
End Sub
Private sub cmdpaste_Click()
Texteditor.Seltext=store
Texteditor.Set focus
End Sub
Q17.Write a program having one command button and on pressing the button it should
display labels & text boxes of Name, age, Roll No, and Address?
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 25 of 121
Ans.
Private Sub command1_Click()
Dim captions (10) as string
Dim sizes (10) as integer
Caption (1)=Name
Caption (2)=Age
Caption (3)=Roll No.
Caption (4)=Address
Size (1)=20
Size (2)=30
Size (3)=15
Size (4)=40
For I=1to 5
Load labels (I)
Load text boxes (I)
Labels (I).Top=labels (I-1). Top+400
Labels (I).Left=labels (0). Left
Text boxes (I).Top=Text boxes (I-1).Top+450
Text boxes (I).Left=Text boxes (0).Left.
Text boxes (I).Width=Sizes (I) * 60
Labels (I).Caption = Caption (I)
Labels (I).Visible = True
Textboxes (I).Visible = True
Next
End Sub
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 26 of 121
== to compare equality
!= Not equal to
Logical operators String conciliation
AND, NOT, OR String conciliation -------&
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 27 of 121
Q23.What do you understand by scope of a variable? Differentiate between global variable and
local variable.
Ans. Scope of a variable depends upon the location where it is declared or which keyword is
used to declare the variable. Scope tells the lifetime of a variable. We can declare variables in
two ways.
1. Global variable: - When the variables are to be declared as global variables then they are
declared in the general section of the program. So that they are accessible within the whole form.
If they are declared with public keyword then they are accessible within the whole project with
form reference. If they are declared with in a module even then they could be called globally
within the project. If these variables are declare with private or dim keyword in the general section
then these variables are accessible with in the specified form only.
2. Local variable: - if the variables are declared with dim statement in any procedure of the form
then they are accessible with in that procedure only. We can declare local variable with public or
private keywords too.
Q24.Write a VB program to implement a calculator that coverts a number given a textbox to its
equivalent Binary Hexadecimal and octal number on the click of respective buttons and display
the result in another textbox.
Ans.
Private Sub Cmdbinary_Click()
Text2.text = ""
n = Text1.text
While n > 0
s = n Mod 2
Text2 .text= Text2 .text& s
n = Int(n / 2)
Wend
Text2.text = StrReverse(Text2.text)
End Sub
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 28 of 121
Q26.Write an application that will put a new Textbox control on the form every time you click the
command button?
Ans.
Dim i As Integer
Private Sub Command1_Click()
i=i+1
Load Text1(i)
Text1(i).Visible = True
Text1(i).Top = Text1(i - 1).Top + Text1(i - 1).Height
End Sub
Q27.Create a project that contains a picture box and two buttons labeled smile and frown. The
smile button will display a happy face in the picture box and frown will display a sad face. Use
graphics methods to draw the two faces.
Ans.
Private Sub Command1_Click()
Picture1.Cls
Picture1.FillStyle = vbSolid
Picture1.FillColor = vbYellow
Picture1.Circle (Picture1.ScaleHeight / 2, Picture1.ScaleWidth / 2), 500
Picture1.FillColor = vbRed
Picture1.Circle (Picture1.ScaleHeight / 3 + 130, Picture1.ScaleWidth / 3 + 100), 100
Picture1.Circle (Picture1.ScaleHeight / 3 + 500, Picture1.ScaleWidth / 3 + 100), 100
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 29 of 121
Q28.Briefly describe the difference between the design and run mode.
Ans. The VB provide two types of modes for an application. These modes are
1. Design mode: - When a form is opened in this mode then we can place different controls that
we want, on the form. These controls then appear on the form at run time. If we want that some
the drawn controls must appear on the form, but we do not want them to work. Then we can
specify the disable property of these controls at design time as False. Some of the properties of
controls those we can not specify at run time these can be specified at design time. At design
time using form layout window we can also position the form for the run time appearance. Shape
and line control doesnt provide any event so it is normally used at design time.
2. Run mode: - this mode specifies the codes that will execute when we run the form. In which
we can specify the control position according to form size at runtime. The code for events those
are attached with different controls of the form can only define in the run mode. We can define
different types of procedures those are called by the events of the control at runtime.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 30 of 121
Q29.List out the mouse pointer properties with at least one pointer example.
Ans. Returns or sets a value indicating the type of mouse pointer displayed when the mouse is
over a particular part of an object at run time.
The settings for value are:
Constant Value Description
vbDefault 0 (Default) Shape determined by the object.
vbArrow 1 Arrow.
vbCrosshair 2 Cross (crosshair pointer).
vbIbeam 3 I beam.
vbIconPointer 4 Icon (small square within a square).
VbSizePointer 5 Size(four-pointed arrow pointing north,south,east, west).
vbSizeNESW 6 SizeNESW (double,arrow,pointing,northeast, southwest).
vbSizeNS 7 Size N S (double arrow pointing north and south).
vbSizeNWSE 8 SizeNWSE (double,arrow,pointing,northwest, southeast).
vbSizeWE 9 Size W E (double arrow pointing west and east).
vbUpArrow 10 Up Arrow.
vbHourglass 11 Hourglass (wait).
vbNoDrop 12 No Drop.
vbArrowHourglass 13 Arrow and hourglass.
vbArrowQuestion 14 Arrow and question mark.
vbSizeAll 15 Size all.
vbCustom 99 Custom icon specified by the MouseIcon property.
This example changes the mouse pointer to an hourglass while circles are drawn across the
screen and then changes the hourglass back to a pointer at the end of the procedure. To try this
example, paste the code into the Declarations section of a form. Press F5 to run the program,
and then click the form.
Private Sub Form_Click ()
Dim I .
Screen.MousePointer = vbHourglass
' Set random color and draw circles on form.
For I = 0 To ScaleWidth Step 50
ForeColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255
Circle (I, ScaleHeight * Rnd), 400
Next
' Return mouse pointer to normal. Screen.MousePointer = vbDefault
End Sub
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 31 of 121
graphic is scaled to fit the control or vice versa. Although you can place an Image control within a
container, an Image control can't act as a container.
Properties: - there are two main properties of image control. These are
Picture: - In the picture property the image that we want to display on the image control is
specified.
Stretch: - Returns or sets whether the image/ graphics resizes to fit the size of an image control
or not.
2. Command Button: - The easiest way to allow the user to interact with an application is to
provide a button to click. You can use the command button control provided by Visual Basic. Most
Visual Basic applications have command buttons that allow the user to simply click them to
perform actions. When the user chooses the button, it not only carries out the appropriate action,
but it also looks as if it's being pushed in and released. Whenever the user clicks a button, the
Click event procedure is invoked Command button control has the default event click and its
default property is caption.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 32 of 121
The control is automatically resized to fit the boundaries of the docked edge.
Q34.What is Combo Box, Name and describe the three styles of Combo Boxes?
Ans. Combo Box control presents a set of choices that are displayed vertically in a single
column. If the number of items exceeds the value what can be displayed, scroll bars will
automatically appear on the control. A Combo Combine a feature of a Textbox and a List Box.
There are three types of Combo Box Styles.
Dropdown Combo: It appears as only an edit area with a down arrow button at right side. The
user can either select a value from the list or type a value in the edit area
Simple Combo: It displays an edit area with an attached list box always visible immediately
below the edit area. The user can select an item from the list or type an item in the edit box
portion for the combo box.
Dropdown List: It turns the combo box into a drop-down list box. The user could click the down
arrow to view the list. The user can select only one of the list items and cannot type an item in the
edit area.
Q35.What is the Purpose of Toolbar? How does a menu vary from a toolbar?
(v.imp)
Ans.Toolbars are used to provide quick access to the functions that are also available in menus.
Toolbars are even more helpful if they expose functions that are buried under a menu of options
or hidden deep in nested submenus. When you decide to create large applications, be sure to
use of toolbars and menus effectively.
Menus expose many of the underlying functions built into an application that are not
necessarily be observed at first glance. Menu options can be enabled, disabled, checked, and
even popped-up on an object. For example you can create a window list. Which is a menu, that
has an option for each MDI child form within the form.
Toolbar contains normally standard and frequently used options in an application like debug
and Form start options, Form editing options, IDE control option etc.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 33 of 121
All are useful to develop good component application. Building of an application for components,
provide the following methods.
1. Once a component is designed and coded it provide more interaction than the character-
based program.
2. Any user can interact with components because components raise events when any action is
preformed on component. These different actions can manage only in component-based
programming.
3. After developing a component, it can be reused in different softwares, so it provides
reusability.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 34 of 121
Text1. Text=Time
It will display system time in the text box.
Strcomp function: - This function can be used to compare strings. The strcomp function will
returns a value, such as for example
E.g.-If you set X=strcomp (A $, B $), then value of x is, 1 if A$ is less then B$, 0 if A$ equals to
B$, 1 if A$ is greater than B$ and the value of x will be NILL if any variable is empty.
Instr() function :- Instr function tells you whether the first string is a part of the another string or
not and also it will tell you the position from which the first string starts within the another string.
The syntax is
Instr [string1, string2, compare]
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 35 of 121
Q41.Write a program that reads in the radius of a circle as an integer and print the circle
diameter, circumference and area on the form using print method.
Ans. Place a command button on the form and do the following code in the code window
Private Sub Command1_Click()
Dim r As Integer
Dim d As Integer
Dim cf, a As Double
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 36 of 121
r = InputBox("Enter radius")
d=2*r
cf = 2 * 3.142 * r
a = 3.142 * r * r
Print "Radius :", r
Print "Diameter :", d
Print "Circumference :", cf
Print "Area :", a
End Sub
Q42.WAP that prints 100 asterisks to the form one at a time. After every tenth asterisk your
program should print a line break.
Ans. Place a time control on the form and its interval property to 100.
Timer1.interval=100
Write the following code in the code window
Dim i As Integer
Dim j As Integer
Private Sub Form_Load()
i=1
j=1
End Sub
Q43.What is a variant? What are the advantages and disadvantages associated with the
use of variants?
Ans. Variant is a default data type. When you declare variables implicitly they automatically got
declared in VB, this data type is known as variant type in VB. The variable declared as variant
can hold the value of any type. The memory space taken by the variant variable is 16 bytes.
Variant Type provides a number of advantages -
1. They can hold any type of values
2. It will automatically adjust and convert type accordingly to the value stored into the variable.
3. Very useful, when implicit declaration is used in the program.
Disadvantages
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 37 of 121
1. It automatically declares the variables, which are not declared explicitly in the application, the
use of these variables in the program, makes the program more error prone. The errors caused
by these variables are difficult to find.
2. Another disadvantage of variant data type is that, it consumes more memory spaces as
compare to the other data types.
Q44.Write a VB code in a click event of a Button to draw X across a form by using line
function.
Ans.
Q45.Find the number of items in a combo box. How you will set the item number of 2 top most
items in the visible portion of the list box?
Ans. Following program will count the number of items in the combo box and will set the item
number of 2 top most items in the visible area of the list box. Using following steps.
1. place one combo control,list control and two command buttons.
2. add item in combo using the list property of the combobox
3. Write the following code to count number of items in the combobox with help of button.
Private Sub Command1_Click()
MsgBox Combo1.ListCount
End Sub
4. To place 2 top most items of the combo box in the list control, write the following code
Private Sub Command2_Click()
List1.AddItem (Combo1.List(0))
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 38 of 121
List1.AddItem (Combo1.List(1))
End Sub
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 39 of 121
A Function is a procedure that performs a task and returns a value. Each time the function is
called the statements between Function and End Function are executed. The syntax for a
function is as follows.
Function functionname (arglist)
[Statements]
End Function
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 40 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 41 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 42 of 121
Num = 3 * Num
Print I am in the procedure (pass by reference)
End sub.
Sub triple (Byval Num as integer)
Num = 3*Num
Print I am in the procedure (Pass by value)
End sub.
In visual Basic, passing an argument by reference is default. If we have to pass an argument by
value then we have to specifically mention the keyword By Val.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 43 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 44 of 121
2.Random access: - Random access also works to retrieve data from ASCII files, but you have
greater flexibility when you use it instead of sequential mode. Random access allows you to
position yourself anywhere within the file at any time to get the data you need.
3.Binary Mode: - Binary mode allows you the greatest amount of flexibility when working with
files. When working with binary access mode, you retrieve data sequentially and in chunks; - the
binary mode. The number of bytes read form the file equals the size of the string, in bytes that
you want to read data into.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 45 of 121
Level2 debugging: Second level debugging is the activity required to make a functional unit of
code interact according to plan with other units of code, typically before shipping or deploying the
completed project. Generally this condition arises when the users procedure does not run
properly in a test case.
Level3. Debugging: Level3 debugging is primarily the diagnosis and repair of problems
occurring in applications that have already been deployed. These bugs are often the most difficult
to locate because they occur on a remote machine. They may be the result of unanticipated
circumstances. These may be one of the following:
User actions
Program configuration
System configuration
Q63.Write a VB program that takes a number from the user with an input box and which will
displays its multiplication table in the immediate window.
Ans.
Dim n As Integer
Dim I as integer
Dim c as integer
c=0
n = Input Box("enter number")
For i = 1 To 10
c=n*i
Debug. Print n, "*", i, "=", c
Next
Q64.What modality is and how does it affect the behavior of your dialog boxes?
Ans. Modality means whether the dialog box will give its control to the next dialog box or form
without being closed or unloaded. There are two types of dialog boxes.
1. Modal: - if the dialog box is of modal type then the control will not be transferred to their parent
form until the necessary information has been given to the dialog box or it has been closed. E.g.
of these dialog boxes are msgbox, inputbox in VB.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 46 of 121
2. Modeless: - if the dialog box are modeless type then the control can be transferred to the
parent form without closing the dialog box. E.g. of this type of dialog box is find & replace dialog
box.
Q65.What do you understand by bug (error)? What are the different types of bugs that you
may encounter while programming with visual basic? OR
Q66.What are the common errors in the visual basic? (v.imp)
Ans. Bugs are the errors. Bugs appear in the VB application at different situation and can
terminate the VB application abruptly. Some bugs will not terminate the application but will insist
the produce to produce the wrong result. All these bugs are divided to the following categories.
Syntax bugs(errors): These errors occur when we enter an incorrect line of code. Such as a
mistake in typing a keyword, a punctuation is missing or an incorrectly spelled variable.
Runtime bugs(errors): These occur when a command attempts to perform an action that is not
valid. For example, a runtime error occurs if we try to assign a string to a numeric variable. Error
handler executes only if a run-time errors encounters.
Logical bugs(errors): Occurs when an application executes without syntax error or run-time
errors, but the results are not what we intend or correct. For example, an application might
prompt the user for a password but then may not allow access to the application even if the
password is correct.
Q67.Write a program that verifies the order in which the events occur for the user control.
Use debug. print statement to display the name of each event as it occurs.
Ans. Using the following code we can find the order of the occurrence of events in the user
control and we can print the message on the debug window.
Private Sub UserControl_Initialize()
MsgBox "inalizining event"
Debug.Print "inalizining event"
End Sub
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 47 of 121
Q70.Explain the difference between SDI and MDI applications. Give suitable example. Or
Describe the purpose of different windows in IDE.
Ans. Visual Basic provides a great deal of flexibility, allowing you to configure the working
environment, which best suits, your individual style. You can choose between a single or multiple
document interface and you can adjust the size and positioning of the various Integrated
Development Environment (IDE) elements. Your layout will persist between sessions of Visual
Basic.
SDI or MDI Interface
Two different styles are available for the Visual Basic IDE:
Single document interface (SDI) or multiple document interface (MDI). With the SDI option,
all of the IDE windows are free to be moved anywhere on screen; as long as Visual Basic is the
current application, they will remain on top of any other applications. With the MDI option, all of
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 48 of 121
the IDE windows are contained within a single resizable parent window. Any single document
interface can be converted into a multiple document interface by setting the value of the
MDICHILD property of a form as true.
e.g. in Vb project the following form is created to perform calculate the sum of two numbers. And
another form shows the welcome message. After inserting an MDI form in the project. We can
convert these SDI form into the MDI with the help of MDICHILD properties of these forms
Single Document Interface First Form
set firstform.mdichild=true
set secondform.mdichild=true
to change this interface to multiple document interface.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 49 of 121
GoTo 0 : - Disables enabled error handler in the current procedure and resets it to nothing.
GoTo -1 : - Disables enabled exception in the current procedure and resets it to nothing.
Resume Next
Specifies that when a run-time error occurs, control goes to the statement immediately following
the statement where the error has occurred and the execution continues from that point. Use this
statement rather than On Error or Go To statements while accessing objects.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 50 of 121
Ans. In Component based system design component is created and coded to execute result
on the specific action on the component.
Component based design system has three different phases.
1. Component Design phase: In this phase the controls are placed on the form. With proper
orientation and alignment. Their properties are specified according to the requirement using
property window.
2. Code phase: - In the code phase of the component design system. Programmer will analyse
the requirement of code and when the code will be executed and appropriate event for that code
will be handled in the code window.
3. Executing/Testing phase: -After the coding process, the source will be tested in the
testing/Execution phase by putting the dummy data on the components, if it produce accurate
results then they will finalize the source otherwise the debug process will be performed to find the
logical and syntax errors.
Debugging
Q76.An Array Marks(40) contains marks of 40 students of a class. WAP to count the number of
students who scored more than 60 and who scored below 33.
Ans. Private Sub Command1_Click()
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 51 of 121
Q77.What is a break expression? What is the difference between watch expression and break
expression?
Ans. Break Expression :- A break expression works as a marker in the code that tells visual
basic to suspend the program execution. By setting the breakpoints at a place make it easy to
investigate which line has suspended the program execution. Break expressions are temporary
and not preserved in the code.
Watch Expression : - Visual basic automatically monitor the watch expression that you define.
When the application enters break mode, these watch expressions appears in the watch window,
where you can observe their values. You can also direct watch expression to put application into
break mode whenever the expressions value changes or equal to specific value.
Difference between break expression and watch Expression
Break expression Watch expression
It cab be added using toggle break point It can be added using add watch option of the
option of the debug menu debug menu
It halts the execution of an application and It is monitored automatically by the visual basic
gives a snapshot to its condition at any
instant as the program is still active
In break expression we can check code Watch expression is added into the watch
using different debug tools window and it force the form to enter in a break
mode where we can watch the current result of
the expression.
Q78.Use the common Dialog Control to create an application that asks user for a file to open and
then open that file in NOTEPAD.
Ans. Following steps are used to open a file in richtextbox.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 52 of 121
Q79.What kinds of tools are available for aid in the debugging of vb application? Explain each in
brief.(v.imp)
Ans. There are different kinds of debugging tools provided by vb to the vb program error free.
These tools are.
The Immediate Window: Most programming languages have a way to test program statements
procedures and functions visual Basic users the immediate window for the testing purpose you
can use the immediate window to test statements or to perform quick calculations when you are
in break mode. If you type Print 2+2 in the immediate window and press enter, visual basic
quickly responds with a 4.
The Debug Object: You can also add lines of code to your program that will print values directly
to the immediate window. For this you can use a predefined visual basic object called debug.
E.g.-
Debug.Print variable
Debug.Print expression
Watch Window: In debugging terms, a watch is a variable or object that is specifically monitored
or watched in real-time as the program executes. You can view the properties of watches in the
watch window. The watch window can be called up by pressing the watch window button or by
adding a watch to the window.
Call Stack::- Call stack tool of vb provides the detail information of parent form , and about the
current position of the break control in forms debug process.
Step Into: Step into tool is used to find the logical errors and check the whole code by executing
statements line by line including all the functions and subroutine involved in the process.
Step out: Step out tool only provide the result of the subroutine or function in debug mode.
Step Over: Step over tool too execute the code line by line but excluding the procedures or
subroutines being called by the current event. It only shows the result of the called subroutines or
functions and then move to the next statement present in the current event or procedure.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 53 of 121
The actual number of elements can be allocated using a ReDim statement. This example
allocates the number of elements in the array based on the value of the variable, Y.
ReDim New Array (Y+1)
The ReDim statement can appear only in a procedure , which is an executable statement. The
same way of declaration as used for fixed arrays is used for declaring ReDim statements. ReDim
is an executable statement. The lower and upper limits for each dimension can also be specified
explicitly as in the case of a fixed size array. An example for this is given below.
ReDim FirstArray ( 4 to 12)
Each time on executing the ReDim Statement, the current data stored in the array is lost and the
default value is set. But if we want to change the size of the array without losing the previous
data, we have to use the Preserve keyword with the ReDim Statement. This is shown in the
example given below.
ReDim Preserve NewArray (Unbound (FirstArray) +1)
When the Preserve keyword is used, only the upper limit of the last dimension in a
multidimensional array can be changed. No other dimensions or the lower limit of the last
dimension can be changed.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 54 of 121
control's interface to its appearance has to be written by the developers/users. They also are the
most flexible ones as the user does not have to put up with the limitations of other controls.
2. Existing Visual Basic Control : - The simplest of custom ActiveX control is one that is based
on an existing control. The user can keep most or all of the original control's functionality and then
add custom members.
For example, when the style property of the Combo Box is set to 0 (Dropdown) or 1 (Simple
Combo), the user can enter a new field value in the edit box of the control, but the new entry is
not appended to the list of available choices. It can be enhanced by enrapturing the Enter key.
When the user presses Enter, add the entry in the Edit box to the list of options. This can result in
the development of a new control, say Enhanced Combo.
3. Constituent Control : - In cases where most of the functionality we want to include in our
custom control already exits, a new control can be designed that include existing control. The
control that form part of the newly developed custom control is called constituent control.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 55 of 121
Visual Basic Library for SQL Server (VBSQL) : It is an implementation of the DB library API
is specifically designed to provide access to an SQL server through a visual Basic Application.
Active Data Object (ADO) : This is a programming model that eliminates the need to choose
from DAO (Data Access Object) and RDO (Remote Data Object) and all other data access
methods. It is designed to provide a common bridge between different database, file system and
e-mail servers.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 56 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 57 of 121
Ans. Microsoft masked edit control is used for input validations in a text box. It looks like an
ordinary text box. You can restrict the characters entered without having to write code in the key
events. Most important property is mask property. This property controls what the user can enter.
The event dealing with input to Masked edit box is Validation Error event. This event is fired
whenever user tries to enter invalid character.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 58 of 121
approach is to create a recordset object, associate a command object with it and retrieve the
desired records.
3.Recordset: - This object stores the results produced on the execution of command object in a
database. Recordsets can be created without an explicit connection. To access the fields of a
recordset object, we can use fields collection.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 59 of 121
When ADO data control is placed & data source is specified we can bind various other controls like
textboxes, list boxes, combo boxes etc. to ADO data control by specifying data source and data
member properties. This is why ADO data control is very effective tool for connecting to databases.
Q94.What are ActiveX Controls define it and explain any two applications of ActiveX
controls?
Ans. ActiveX is a set of reusable components that can be created and utilized by several
applications. ActiveX uses the internet technology to assist in creating compact and reusable
applications that can be deployed via internet or a corporate intranet.ActiveX provides a familiar
client server infrastructure to run your applications. Controls like coolbar, flatscrollbar, imageCombo
are ActiveX controls. ActiveX control can also be used in other ActiveX compliant programs. These
can be embedded and distributed through HTML web pages.
Image List Control: While working with toolbar & coolbar each button is assigned an image and
image list works behind the scene. It is not visible at run time and gives a way to store a group of
images in a single place. The key to work with an Image List control is ListImage object and
ListImages collection. The ListImages collection specifies the images stored. The main properties
of Image List control are its custom properties and are shown as dialog box. You can set pictures in
the ImageList control at design time and use their index to refer them. The Image tab on this dialog
box gives a convenient way to add images at design time.
To use image from imagelist control we can write
Set Picture1.picture=ImageList1.ListImage(1).Picture
Toolbar Control: It is one of the most important tools for providing an easy interface to the users.
The toolbar control provides easy access to options available in your application. To add design
time properties to the controls select its custom properties from the property windows.
A toolbar control contains a collection of Buttons objects used to create toolbar. A toolbar contains
buttons that correspond to items in an applications menu, providing a graphical interface for the
user to access an applications most frequently used functions and operations. You can even add
an Image in a button and Image list control is required on the same form, to add images in the
toolbar at design time. You can add buttons at design time in the toolbar using its property pages.
The custom property page has three tabs, General, Buttons and Picture. In the buttons tab you
can add more buttons to the toolbar.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 60 of 121
Q96.What are the different types of data reports? How are they created? (v.v.imp)
Ans.Data report: - It is created by using data environment .It may have grouping on some data.
To create data report we must have data environment. After that add command for the report.
Double click the Data Report object in the Project Explorer window.
Arrange the Data Environment window and the Data Report window so that both the windows
are visible on the screen.
From data base environment drag the fields onto the appropriate section to add selective
fields or drag command object to include all the fields in the report.
Each label control and text box control can be arranged in different section of the report by
dragging and dropping as per the requirement set the data source property of Data Report object
to the Data Environment and data member property to command.
Crystal Reports: - Crystal reports let you customize many of the programs default settings to fit
the layout of your work. These settings affect such things as:
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 61 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 62 of 121
e.g
msgbox rs(0)
or
msgbox rs.fields(Name)
Q100. What is the difference between List View and Tree View? Explain with example.
Ans. Tree View :- The Tree View control provides a hierarchical view of folders or other items
that can be neatly categorized in a tree- style layout. It is often used in conjunction with a List
View control, which is used to display the contents of the folder selected in the Tree View. This is
the ActiveX control, which is available in common window control.
Main properties of Tree View
Name, ImageList, Indentation OLEDragMode, OLEDropMode, LineStyle, Scroll etc
List View :- List View control provides a detailed view of the items appear in the specified folder
that we select in the tree view. Normally this is used in conjunction with a tree view. But we
can use it separately too. Any selected item can execute. This is also one of the ActiveX
control and is available in Common window controls.
Main Properties of List View
Arrange , SelectedItem ,Name, LabelEdit, LabelWrap, Listitems, view , GridLines etc.
Q101. Explain in detail steps needed to modify the data in a database record. Or
How will you update Records sequentially through Visual Basic?
Ans. Suppose we have a table in Microsoft access called student.mdb and we want to modify
the record then we have to perform the following steps.
1.Include reference of ADODB library
2. initialize connection and recordset using following commands in general section
dim cn as new ADODB.connection
dim rs as new ADODB.recordset
3.open connection and recordset objects on the form activate them by using following command
cn.open provider=Microsoft.Jet.Oledb.4.0;Data source= &app.path &\student.mdb
rs.open select * from student,cn,adopendynamic, adopenlocopticmistic
4.create a find button to find the record to update and for this write following code-
private sub cmdfind_click()
dim rno as integer
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 63 of 121
Q102. What care has to be taken while adding updataing and deleting records from linked
tables?
Ans. Following things we have to care while adding, updating and deleting records
1.if any new record is added in the linked table it must have the reference value from the main
table otherwise no insertion is possible
2.if any record is going to get updated then we should change reference key in the secondary
table
3. before deleting the records from the parent table we all the reference records must be deleted
from the child table.
4.Before updating the key field in parent table must remove the reference of that value from the
child table.
Q103. What do you mean by ActiveX controls ? How they are useful in application
development?
Ans. ActiveX controls add power to your Application form. In addition to creating your own
controls, you can use the Internet controls provided with the Professional and Enterprise editions
of Visual Basic to extend standard applications with Internet technology. For example, you can
use ActiveX controls on a Web page to add customized menus to your Web pages, to add a
scrolling banner you can use to present advertisements or important announcements, or to add
animated effects. Window Common dialog control provides inbuilt dialog mechanism that is used
to create a simple notepad application.
ActiveX provide number of features those helps developers in development.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 64 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 65 of 121
rs.Close
End Sub
2. Performing a Parameter-Driven Stored Procedure:- it's possible to build your own ADODB
Parameters collection. In this case, youre performing a simple two-argument SP,
"AuthorByYearBorn", that returns a small resultset
Private Sub RunSPButton_Click()
Dim Qy As New ADODB.Command
Dim Parm As New ADODB.Parameter
Dim Parm2 As New ADODB.Parameter
Set Qy.ActiveConnection = cn
Qy(0)="1947"
Qy(1)="1948"
Qy.CommandType = adCmdStoredProc
Qy.CommandText = "AuthorByYearBorn"
Set rs = Qy.Execute(ShowRows)
ADOGrid1.ShowData rs
End Sub
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 66 of 121
column width interactively by resizing the column or by changing the column's width in the
Column object's property page.
Use the DataGrid control's Columns collection's Count property and the Recordset object's
RecordCount property to determine the number of columns and rows in the control. A DataGrid
control can have as many rows as the system resources can support and up to 32767 columns.
When you select a cell, the ColIndex property is set, thus selecting one of the Column objects
in the DataGrid object's Columns collection. The Text and Value properties of the Column object
reference the contents of the current cell. The data in the current row can be accessed using the
Bookmark property, which provides access to the underlying Recordset object's record. Each
column of the DataGrid control has its own font, border, word wrap, and other attributes that can
be set without regard to other columns. At design time, you can set the column width and row
height and establish columns that are not visible to the user. You can also prevent users from
changing the formatting at run time.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 67 of 121
Automatic support for data access, Remote Automation, and DCOM features. The wizard
automatically determines whether your project includes functionality that changes the setup
process. For example, if you include certain types of data access, Remote Automation, or DCOM
features, you may need to include drivers or other files in your package. The wizard checks your
projects and displays screens that allow you to specify the appropriate options in these cases.
Shared file capability. The wizard allows you to install some files as shared files. This means
that the files will not be removed from the system during an uninstall if other applications are
using them.
Alternate file locations for Internet packages. In Internet packages, the wizard allows you to
specify whether dependency files should be included in the setup program or downloaded from
an alternate Web site.
Safety settings for Internet packages. If you do not use the IObjectSafety interface in your
project, the Package and Deployment Wizard lets you mark components in your application as
safe.
Custom destination locations for each file in the project. Most files have a default location
to which they are installed, depending on whether they are project files or system files. You can
change these locations if you want to install the files to a different location.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 68 of 121
Ans. Component Object Model: - Component Object model (COM) is the basis for all OCX
controls. COM is a specification. It doesnt tell the user how to implement a
control ; rather it concentrates the way in which two process communicate with
each other and defines some standard interfaces for them to communicate.COM
allows software components to communicate with each other. It is a binary
standard that allows any two components to communicate regardless of the
language the components are written in. The only way to access and interact with
a COM object is through interfaces.
Distributed Component Object Model: - Distributed Component is nothing but a network. It
allows information exchange over the internet , the local intranet or any other network. While
COM is the basis for OCX controls , DCOM is the basis for ActiveX controls as ActiveX itself is
nothing but internet enable OCX.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 69 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 70 of 121
Add new records or update a database based on any changes you make to data displayed in
the bound controls.
Q117.Write the steps to create an ActiveX Control for clock that exposes startclock() and
Stopclock() methods.
Ans. Following steps are used to create an ActiveX Control for clock
1. select ActiveX control project from the new project Dialogbox
2. On the usercontrol form place the following controls
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 71 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 72 of 121
Numeric Functions
String functions
Date and Time functions
Examples are:
Abs(argument):- returns absolute value of the argument
Sqr(argument):- Returns Square root of the argument.
Len(argument):- Returns the total number of characters that are required to store the argument.
UCase(argument):- Returns all characters of the argument in upper case.
Q122. Give any two examples of methods that can be added to a class module?
Ans. A class is a self-sufficient piece of code, which means you can move the class from one
project to another and it should, work properly without any modification. A class contains
properties and methods that all work together to perform a set of related operations. Object is an
instance of a class, which is used to access the members of class.
Methods: - Adding method to a class is just like adding procedure to a class module. You
can prefix the method name with Private or Public, depending on the scope of method.
Examples:
Enter the following code in the Code Window
Public sub Hello()
MsgboxWelcome to the Class Module
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 73 of 121
End sub
Public sub disp()
MsgboxYou are using class module
End sub
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 74 of 121
Ans. A cursor keeps track of the position in the result set, and multiple operations can be
performed row by row against a result set with or without returning to the original table.
The type of cursor used in a Recordset object.
AdOpenForwardOnly : - Forward-only cursor. Default. Identical to a static cursor except that you
can only scroll forward through records.
AdOpenKeyset: - Keyset cursor. Like a dynamic cursor, except that you can't see records
that other users add, although records that other users delete are inaccessible from
your recordset. Data changes by other users are still visible.
AdOpenDynamic: - Dynamic cursor. Additions, changes, and deletions by other users are
visible, and all types of movement through the recordset are allowed, except for bookmarks if the
provider doesn't support them.
AdOpenStatic
Static cursor. A static copy of a set of records that you can use to find data or generate
reports. Additions, changes, or deletions by other users are not visible.
Q129. Give an Example where searching and Sorting both are involved.
Ans. Sorting is a technique that is used to arrange data value in a specific order. Searching is a
technique that is used to find out a specific data value from the recordset entries.
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command1_Click()
rs.MoveFirst
rs.Find "empno=" & Text1.Text
Text1.Text = rs.Fields(0)
Text2.Text = rs.Fields(1)
End Sub
Private Sub Command2_Click()
rs.Sort = "ename ASC"
Text1.Text = rs.Fields(0)
Text2.Text = rs.Fields(1)
End Sub
Private Sub Form_Load()
rs.CursorLocation = adUseClient
cn.Open "provider=microsoft.jet.oledb.4.0; data source=f:\database.mdb"
rs.Open "emp", cn, adOpenDynamic, adLockOptimistic
End Sub
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 75 of 121
Ans. Library functions are the built-in functions which are defined in the VB library. Each library
function performs a specific operation. You can use these functions directly, without implementing
in a programming to perform some operations such as manipulating strings and numbers.
User-defined functions are much like subs except they return a value to the calling procedure.
They are especially useful for taking one or more pieces of data, called arguments, and
performing since tasks with them. Then the function returns a value that indicates the result of the
tasks completed within the function e.g.
Public Function Sum (A as integer)
Statements
End Function
Q133. Give any two examples of Properties that can be added to a class module?
Ans. Properties in a class are used to store or retrieve information from the project. In Visual
Basic there are two ways to add properties to a class module:
Using Public variables
While using public variables to add properties to a class module, you just need to declare the
public variable in the declaration section of the class module.
Using Properties Procedures
Properties procedures are written like functions, but behave like properties for the end users. A
property procedures has three parts:
Declaration:- Declare variables to store the property value
Get:- Retrieves the property value from a class module
Let:- Assigns properties to a class module.
For Example
Public name as string
Public salary as currency
Public property Get empname() as string
Statement
End Property
Public Property Let empname(ByVal VNewValue as string)
Statement
End Property
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 76 of 121
components and database Connection objects to manage the individual components. The Visual
Component Manager may include complete Microsoft Visual Basic package to store and manage
the COM components. Visual component Manager allows a programmer to create and build
newer components based on component based software development. The concept of reusability
allows a programmer to use functionality previously defined for a component.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 77 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 78 of 121
Dim x, y As Integer
x = 10
y = 10
If x <> y Then
MsgBox " Both values are not equal"
Else
MsgBox " Both values are equal"
End If
End sub
Example:- Select case end Select statament
Private Sub Form_Load()
Dim sp As Integer
sp = 19
Select Case sp
Case Is <= 18
MsgBox "you are in juniors"
Case Is <= 50
MsgBox "you are in Seniors"
Case Else
MsgBox "you are in Olders"
End Select
End Sub
Q143. Write a Visual Basic code to accept a string only in lower case and display it by
changing its alternate characters to upper case?.
Ans. Open a new Project and Add a Label control with Caption as Enter your Name
Add a Text Control and a Command button
Then Write the coding as below in Code window
Private Sub Command1_Click()
MsgBox "You have entered String is" & UCase(Text1.Text)
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Form1.BackColor = vbGreen
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= 65 And KeyAscii <= 91 Then
MsgBox "Only lowercase are allowed"
KeyAscii = 0
End If
End Sub
Q144. What are the basic Object Oriented Principles and features ?.
Ans. The Object Oriented principles are the principles provided with the object-oriented
programming concept. The object-oriented programming concept or approach involves grouping
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 79 of 121
of data ad functions into modular entities known as a class. Classes are user-defined data types
that contain data and functions and to initiate a class, you need to create an object of the class.
You can access the data and methods of the classes by using this object only. You can create
any number of objects in a class. The various object-oriented principles such as encapsulation
and inheritance provide security to your data and allow the reuse of objects in a new application.
Various object oriented principles are:
Encapsulation: - Refers to the data hiding. Encapsulation wraps data and methods into a
single entity called class. This principle does not permit any outside programming to
directly access and manipulate the wrapped data and methods. To access data you need
to create an object of the class, which allows you to access the wrapped methods and
data. Therefore, this principle provides security to the data inside a program. Visual basic
use classes or class module to encapsulate the data and methods.
Inheritance: - Inheritance is the process by which objects can acquire the properties of objects of
other class. In OOP, inheritance provides reusability, like, adding additional features to an existing
class without modifying it. This is achieved by deriving a new class from the existing one. The
new class will have combined features of both the classes.
Polymorphism: - Polymorphism means the ability to take more than one form. An operation may
exhibit different behaviors in different instances. The behavior depends on the data types used in
the operation. Polymorphism is extensively used in implementing Inheritance.
Q145. What are the types of ActiveX Components available in visual Basic?.
Ans. You can create various types of ActiveX components in Visual Basic depending on the
requirements of the client application. Various types of ActiveX controls in Visual Basic are:
ActiveX Exe: These components are used to create applications that can be deployed on out-
process server architecture. The file extension for these files is .exe. The ActiveX Exe files run in
their own address space making communication between the separate processes but slow.
These files are more robust in terms of execution as bugs in the file do not lead to the crash of
the container.
ActiveX DLL:- These components are used to create applications that can be deployed on in-
process server architecture. The file extension for these files is .dll. These files cannot be
executed on their own. The ActiveX DLL files run in the same address space as the container
files leading to faster intercommunication between the processes.
ActiveX Control:- These components are used to create applications that can be deployed on
remote server architecture. The file extension for these files is .ocx. These files require
Cabinet(CAB) files for proper deployment of the control on the client machine.
ActiveX Document EXE:- These components are used to create controls for web-based
applications. These controls can be published to a web site. The client downloads the
corresponding control from the web site using the web browser.
ActiveX Document DLL:- These Components are used to create component for Web-Based
applications like ActiveX Document EXE. In Contrast to the ActiveX Document EXE these files
require container files that can call the dll files to execute the required functionality.
Q146. How will you select a Record in a database through Visual Basic?.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 80 of 121
Ans. Suppose we have a table in Microsoft access called database.mdb and we want to modify
the record then we have to perform the following steps.
1. Include reference of ADODB library(Microsoft AcvtiveX 2.0 Library)
2. Initialize connection and recordset using following commands in general section.
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command1_Click()
rs.MoveNext
If rs.EOF Then
MsgBox "You are at Last Record"
Exit Sub
End If
disp
End Sub
Private Sub Command2_Click()
rs.MovePrevious
If rs.BOF Then
MsgBox "You are at First Record"
Exit Sub
End If
disp1
End Sub
Private Sub Form_Load()
cn.Open "provider=microsoft.jet.oledb.4.0; data source=f:\database.mdb"
rs.Open select * from emp", cn, adOpenDynamic, adLockOptimistic
Text1.Text = rs(0)
Text2.Text = rs(1)
End Sub
Public Sub disp()
Text1.Text = rs(0)
Text2.Text = rs(1)
End Sub
Public Sub disp1()
Text1.Text = rs(0)
Text2.Text = rs(1)
End Sub
Q147. How will you play a Sound and a Movie File in visual Basic?.
Ans. To Play a Sound or Movie File in Visual Basic You have to add an ActiveX control as
Window Media Player and an Commondialog control 6.0 in your Project file.
Now Add the following Coding in your Code Window:
Private Sub Command1_Click()
CommonDialog1.Filter = "Movie files|*.avi|Song files|*.mp3"
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 81 of 121
CommonDialog1.ShowOpen
MediaPlayer1.FileName = CommonDialog1.FileName
End Sub
Private Sub Command2_Click()
MediaPlayer1.Play
End Sub
Private Sub Command3_Click()
MediaPlayer1.Stop
End Sub
Private Sub Form_Load()
MediaPlayer1.ShowControls = False
MediaPlayer1.AutoStart = False
End Sub
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 82 of 121
the benefits of the database technology without having transferred data from its place of origin to
a DBMS.
This means OLEDB is not restricted to ISAM, Jet or even relational data sources, but is
capable of dealing with any type of data, regardless of its format or storage method. OLEDB has
what is called providers which let you access the different data sources. For different data
sources you have different data providers. OLEDB provides four services that you will be using in
your application. These are:
A cursor service, which is defined as a temporary read-only table that saves the result of a
query with assigned name.
A service to perform batch updates.
A shape service to build that data in the form of hierarchy.
A Remote data service provider for managing data in multi-user environments over
connected or disconnected networks.
ADO: - ADO enables your client application to access and manipulate data in a database server
through any of the OLE DB providers. According to Microsoft ADOs primary
benefits are ease to use, high speed, low memory overheads and a small disk
footprint. ADO supports key features for building client/server and web based
applications.
The ADO features an object model like the DAO and the RDO but it is much flatter. In the case of
the DAO you had seventeen objects and in the case of ADO you have only seven object besides
you do not have to follow a strict hierarchy when working with these objects in ADO. Let us see
how ADO allows you to access data from a database.
Establishment of a connection to a data source
Extraction of the required data with a suitable command.
Having extracted the data and worked on it, we may have to up-to-date the data source.
Keep an eye on the errors that may occur and take suitable action.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 83 of 121
ActiveX components allow the control to import data from one application to another.
It allows you to make changes in the imported data such that the data in the parent application
can also be updated.
ActiveX components allow the programmer to place different types of data or objects in one
document.
It allows applications to exchange data between various applications. It also allows one
application to send commands to another application.
It also allows various objects from different applications to be visible in a single document. The
imported object gets embedded in the host application document. When a user clicks on the
object, the parent application of that object gets invoked to present the called object.
ActiveX prevents the conversions of data while integrating one application into another.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 84 of 121
3.Print PreviewPreview the report by using the Show method. The data report is then
generated and displayed in its own window.
4.Print ReportsPrint a report programmatically by calling the PrintReport method. When the
data report is in preview mode, users can also print by clicking the printer icon on the toolbar.
5.File ExportExport the data report information using the ExportReport method. Formats for
export include HTML and text.
6.Export TemplatesYou can create a collection of file templates to be used with the
ExportReport method. This is useful for exporting reports in a variety of formats, each tailored to
the report type.
7.Asynchonous OperationThe DataReport object's PrintReport and ExportReport methods
are asynchronous operations. Using the ProcessingTimeout event, you can monitor the state of
these operations and cancel any that are taking too long.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 85 of 121
Q155. Write a program to swap two values using call by reference parameter passing
mechanism.
Ans Function swap(ByRef a As Integer, ByRef b As Integer)
Dim c As Integer
c=a
a=b
b=c
Text3.Text = a
Text4.Text = b
End Function
Private Sub Command1_Click()
swap Text1.Text, Text2.Text
End Sub
Q156. Write a program that request a month of the year and then gives the number days in
the month?
Ans
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 86 of 121
If a = 1 Then
MsgBox "31 days in january"
ElseIf a = 2 Then
MsgBox "28 days in february"
ElseIf a = 3 Then
MsgBox "31 days in March"
ElseIf a = 4 Then
MsgBox "30 days in April"
ElseIf a = 5 Then
MsgBox "31 days in May"
ElseIf a = 6 Then
MsgBox "30 days in June"
ElseIf a = 7 Then
MsgBox "31 days in July "
ElseIf a = 8 Then
MsgBox "31 days in August"
ElseIf a = 9 Then
MsgBox "30 days in September'"
ElseIf a = 10 Then
MsgBox "31 days in October"
ElseIf a = 11 Then
MsgBox "30 days in november"
ElseIf a = 12 Then
MsgBox "31days in December"
Else
MsgBox "wrong month entered'"
End If
End Sub
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 87 of 121
3. In Solution Explorer, right-click your project, and then click Properties. The Project
Designer is displayed.
4. Click the Compile tab.
5. Select the Register for COM Interop check box.
Q160. How do you check whether a condition should be checked at the top of loop or at
the bottom ?
Ans we know that the condition checking in the for and while first or at the top if condition is true
then the loop is executed else not but the condition testing in the last in the do-while loop
because the condition testing statement written in the last in this case statement executed first
and the checked according to the syntax of the do while loop.
for Loop
Syntax
Dim a as integer
For a=1 to 10 step 1
Statement1
Next a
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 88 of 121
In the above given program the condition testing in for loop in beginning of the loop and then
executing the statement if condition is not true then loop is not executed.
Control array is the array of controls. To create the array of control just place the control on form
at design time, then copy paste it again and again. By making the control array, similar type of
code can be used for multiple controls. These Controls have a common index property that is
used to identify a particular control in a control array.
How created control Array
First add two command
Buttons first to add labels and
Second to remove the labels
Add a label and make a control
Array for label.
Private Sub Command1_Click()
Dim a As Integer
For a = 0 To 3
Label1(a).Caption = "label" & a
Next
End Sub
Private Sub Command2_Click()
Dim a As Integer
For a = 3 To 0 Step -1
Label1(a).Caption = ""
Next
End Sub
Control array is used to create a collection of controls. It is easy to maintain them and use
them. It is used to create more than one control of same type then we use control array.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 89 of 121
collection.you can do this using for each next loops. This technique is extremely useful when
you dont know the exact number of items in the collection.
You can use for each loop through all items in the collection using code similar to the
following.
Dim cmd as command1
For each cmd in form1
With cmd
If .index<> index then
Msgbox button & trim$(str$(.index))&was not pressed.
End if
End with
Next
The first line dimension an object of the command button class. The next line tells visual basic to
iterate through every command button object for form1. the with cmd statement on the next line
tells the visual basic to use the current object in the collection set to cmd.
Looping through collection is useful for many tasks including iterating through items in treeview
and listview controls,childs formsin an MDI application and object in a dynamic HTML documents.
Q163. Discuss the string handling in-build function : instr, mid, left, right with suitable
example.
Ans Instr() function :- Instr function tells you whether the first string is a part of the another
string or not and also it will tell you the position from which the first string starts within the another
string. The syntax is
Instr [string1, string2, compare]
Example:
str=instr(1,Visual Basic,a)
mid() function :-It returns a substring containing a specified number of characters from a
string. The syntax is
mid(string,strat,length)
Example:
str=mid(Visual Basic,3,7)
left() function:- It returns a substring containing a specified number of characters from the
beginning of a string. The Syntax is
left(string ,length)
Example:
str=left(Visual Basic,5)
right() function:- It returns a substring containing a specified number of characters from the
end of a string. The Syntax is
right(string ,length)
Example:
str=right(Visual Basic,4)
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 90 of 121
Q165. Give Visual basic code for setting a connection to a data source.
Ans Create a DSN-less Database Connection: - The easiest way to connect to a database is to
use a DSN-less connection. A DSN-less connection can be used against any Microsoft Access
database on your web site.
If you have a database called "northwind.mdb" located in a web directory like
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 91 of 121
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
%>
Note, from the example above, that you have to specify the Microsoft Access database driver
(Provider) and the physical path to the database on your computer
Q166. Give visual basic code for frames. And also explain what are the various properties
associated with it.
Ans frame is often used as a container for check box, option button too, each check box is
completely independent. Thus the setting for one check box has no effect on the setting for the
other in the same group.
CheckBoxes: - Option bars are used quite often in the windows environment as they can only
have two outputs 0 and 1 these get used to process the form. In this example it will be
used to change the some text from normal to bold or to italic.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 92 of 121
End If
End Sub
The checkboxes can be turned on at runtime by simply typing
name.value = 1 ' 1 On , 0 off
Q167. There is a label named lblResult that has a property named caption. There is a
variable named X.
(a) write a statement that display the value of x in the labels caption.
(b) Write a statement that display the variable X in the labels caption.
Ans
Private Sub Command1_Click()
Dim x As Integer
x = InputBox("enter the value of X")
lblResult.Caption = x
End Sub
Private Sub Command2_Click()
lblResult.Caption = "X"
End Sub
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 93 of 121
End Sub
Private Sub mnuChoice1_Click()
MsgBox "Choice 1"
End Sub
Private Sub mnuChoice2_Click()
MsgBox "Choice 2"
End Sub
Private Sub mnuChoice3_Click()
MsgBox "Choice 3"
End Sub
3333
3333
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 94 of 121
3333
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 95 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 96 of 121
The system processes the message and broadcasts it to the other windows. Each window can
take the appropriate action based on its own instructions for dealing with that particular message.
Event : - It is an action recognized by an object such as clicking of the mouse or pressing of a
key and you can write a code as a response to the occurrence of an event. Events can occur as a
result of a user action or program code or they can be triggered by the system. Some events are
like: -
Click
Dblclick
Keyup
Keydown
Load
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 97 of 121
programming world to describe the interface and environment that we use to create our
applications. It is called integrated because we can access virtually to all of the development tools
that we need from one screen called an interface. The IDE is also commonly referred to as the
design environment, or the program.
Components of IDE: -
Title bar
Menu Bar
Standard Bar
Toolbox
Project Explorer
Properties window
Form Layout Window
Form Designer window
Object Browser window
Code and text editor window
The Visual Basic startup dialog box
1) Title Bar : - Title bar is the top most bar on the IDE window that display the title of the project.
Title bar has three buttons minimize, maximize (restore down) and close.
2) Menu Bar : - This Menu Bar displays the commands that are required to build an application.
The main menu items have sub menu items that can be chosen when needed. The toolbars in
the menu bar provide quick access to the commonly used commands and a button in the toolbar
is clicked once to carry out the action represented by it.
3) Standard Toolbar : - It contains small icon buttons which are used to access most commonly
commands through shortcut small icon.
4) Toolbox : - The Toolbox contains a set of controls that are used to place on a Form at design
time thereby creating the user interface area. Additional controls can be included in the toolbox by
using the Components menu item on the Project menu. A Toolbox is represented in figure shown
below.
5) Project Explorer : - Docked on the right side of the screen, just under the toll bar. It is the
Project Explorer window. The Project Explorer as showed in figure services as a quick reference
to the various elements of a project namely form, classes and modules. The entire objects that
make up the application are packed in a project. A simple project will typically contain one form,
which is a window that is designed as part of a program's interface. It is possible to develop any
number of forms for use in a program, although a program may consist of a single form. In
addition to forms, the Project Explorer window also lists code modules and classes.
It also contains a hierarchical tree structure of the forms in the projects. A Project
Explorer consists of a number of forms, modules and controls that make up an application. It
helps to manage & navigate multiple projects. This window provides three buttons:
View Code.
View Object.
Toggle Folders.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 98 of 121
6) Properties Window : - The Properties Window is docked under the Project Explorer window.
The Properties Window exposes the various characteristics of selected objects. Each and every
form in an application is considered an object. Now each object in Visual Basic has
characteristics such as color and size. Other characteristics affect not just the appearance of the
object but the way it behaves too. All these characteristics of an object are called its properties.
Thus a form has properties and any controls placed on it will have properties too. All of these
properties are displayed in the Properties Window
The two tabs of property window are:
0.Alphabetic
1.Categorized
Sometimes it is convenient to display the properties sorted alphabetically. For this, alphabetic tab
can be used. But sometimes you can group the properties according to their categories. For this,
categorized tab can be used. e.g. categories can be appearance, behavior etc.
7) Form Layout Window : - Form Layout Window is used to set the position of the form on the
computer screen where it will be displayed when the project is run.
8) Forms designer window : - A form designer window contains the form. Form is the heart of
Visual Basic application. Form is a canvas or container window on which you will place the
various objects that make up application. We work in form designer window to add controls to the
form. It has a title bar in which it contains Project1- form1 (Form) name, minimum button,
maximum (restore down) button and close button.
9) Object Browser : - The Object Browser allows us to browse through the various properties,
events and methods that are made available to us. Selecting Object Browser from the View menu
or pressing the key F2 accesses it. The left column of the Object Browser lists the objects and
classes that are available in the projects that are opened and the controls that have been
referenced in them. It is possible for us to scroll through the list and select the object or class that
we wish to inspect. After an object is picked up from the Classes list, we can see its members
(properties, methods and events) in the right column.
A property is represented by small icon that has a hand holding piece of paper. Methods
are denoted by little green block while events are denoted by yellow lightning bolt icon.
10) Code and text editor window : - VB objects can recognize many different events. The
objects will not function unless you have written code to tell them what to do when the event
happens. This means that for any event, you must write an event procedure telling VB what to do,
so event procedures are lines of programming code that tell VB how to respond to a given event.
Double-clicking any, blank part of the form will open the code window. VB program code
is entered in this window. It contains two drop-down list boxes. If you pull down the left hand box
in the code window you will see a list of controls on your form. If you pull down the right hand box,
you will see the list of events supported by the selected controls.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 99 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 100 of 121
container object.
Data It is used to connect the program to the database.
ACTIVEX CONTROLS.
3) Then select any one of then which component programmer requires.
4) Then click OK button.
5) Now the control that you selected from the components dialog box, is displayed in the toolbox.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 101 of 121
one is closed.
Most applications in Windows 95 or later one, use a Single Document Interface. Each
window of the application holds a single document. So, if the user wants to open more
documents with that application, he must open a new window. An example of an SDI application
is Windows Notepad.
2. MDI (Multiple Document Interface) : - The Multiple Document Interface (MDI) was designed
to simplify the exchange of information among documents all under the same roof with the main
application. You can maintain multiple open windows, but not multiple copies of the application.
Data exchange is easier when you can view and compare many documents simultaneously.
You almost certainly use Windows applications that can open multiple documents at the
same time and allow the user to switch among them with a mouse-click. Multiple Word is a typical
example, although most people use it in single document mode. Each document is displayed in
its own window and all document windows have the same behavior. The main Form or MDI Form
isn't duplicated. But it acts as a container for all the windows and it is called the parent window.
The windows in which the individual documents are displayed are called Child windows.
An MDI application must have at least two Forms, the parent Form and one or more child
Forms. Each of these Forms has certain properties. There can be many child forms contained
within the parent Form. But there can be only one parent Form.
The parent Form may not contain any controls. While the parent Form is open in design
mode, the icons on the ToolBox are not displayed and you can't place any controls on the Form.
To create an MDI application, follow these steps:
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 102 of 121
For example : - In the following program "command1" and "command2" the variable X is
declared in each of the modules. Each variable X is independent of the other--the variable is only
recognized within its respective procedure.
Module Level Variables: - A module level variable is available to all the procedures in the
module. Using Public or Private keyword declares them. If you declare a variable using a
Private or Dim keyword in the declaration section of a standard BAS module, a form
module, a class module and so on then you are creating a private module-level variable.
Such variables are visible only from within the module and can't be accessed from the
outside. In general these variables are useful for sharing data among procedures in the
same module:
In the declarative section of any module
Private Login As Date ' Private module-level variable
Dim Password As String ' another private module-level variable
You can also use the Public keyword for module-level variables. All module types except
BAS modules. (Public variables in BAS modules are global variables.) In this case, you are
creating module-level variable that can be accessed by all procedures in the module to share
data and that also can be accessed from outside the module. In this case, however, it's more
appropriate to describe such a variable.
' In the declaration section of Form1 module
Public CustomerName As String ' A Public property
You can access a module property as a regular variable from inside the module and as a custom
property from the outside:
' From outside Form1 module...
Form1.CustomerName = "Dharam & Neshan"
Project level variables: - Project level variable is also called global or public scope. It means the
variable is accessible to all procedures in all modules of the project. The variables available to all
the modules and procedures in an application are said to have project level variable. Public
keyword is used to declare variable as project level scope. It is also known as global variable.
Basically project level variables are declared when we want use same variable in whole project. It
is declared in the general section but we cant declared in any private sub block.
Public vs private Variables : - The scope of the public variable is global, it means variable can
be accessible in all procedures and modules. The scope of the private variable is local, it
means variable can be accessible within procedure only where it is declared. A variable
can have the same name and different scope.
For example, we can have a public variable named R and within a procedure we can
declare a local variable R. References to the name R within the procedure would access the local
variable and references to R outside the procedure would access the public variable.
Static Variables : - Static variables are not reinitialized each time Visual Invokes a procedure
and, therefore, retain or preserve value even when a procedure ends. In case we need to
keep track of the number of times a command button in an application is clicked, a static
counter variable has to be declared. These static variables are also ideal for making
controls alternately visible or invisible. A static variable is declared as given below.
Static sta As Integer
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 103 of 121
Variables have a lifetime in addition to scope. The values in a module-level and public variables
are preserved for the lifetime of an application whereas local variables declared with Dim
keyword. It can exist only with the procedure in which they can declare is still being executed.
The value of a local variable can be preserved using the Static keyword. The following procedure
calculates the running total by adding new values to the previous values stored in the static
variable value.
The following is an example of an event procedure for a CommandButton that counts and
displays the number of clicks made.
Private Sub Command1_Click ( )
Static Counter As Integer
Counter = Counter + 1
Print Counter
End Sub
The first time we click the CommandButton, the Counter starts with its default value of zero.
Visual Basic then adds 1 to it and prints the result.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 104 of 121
Long : - The Long data type occupies 4 bytes Memory. The valid range of a long variable is -
2,147,483,648 to 2,147,483,647. Long variables can only contain non-fractional values. Long
data type is over Integers for increased performance. Most Win32 functions use this data type for
this reason.
Single : - The Single data type occupies 4 bytes memory space. The valid range of a long
variable is -3.402823e38 to -1.401298e - 45 for negative values and from 1.401298e -45 to
3.402823e38 for positive values. When you need fractional numbers within this range then we
can use single data type.
Double: - The Double data type occupies 16 bytes Floating-point memory used. These variables
range from -1.79769313486232e308 to -4.94065645841247e-324 for negative values and from
4.94065645841247e-324 to 1.79769313486232e308 for positive values. When high accuracy is
needed then we can use double data type.
String : - The String data type usually used as a variable-length and fixed-length type of
variables. A variable-length string can contain up to approximately 2 billion characters. If we use
fixed-length then it occupies fixed length the length that we define in the declaration time. Each
character has a value ranging from 0 to 255 based on the ASCII character set. Strings are used
when Text is involved.
Boolean : - The Boolean data type has only two states True and False. These types of variables
are stored 16-bit (2 Byte) memory. For example, lets suppose that you have a textbox (Text1) and
a command button (Command1). You want only Command1 to be enabled when there is text in
Text1. You would do something like this...
Whenever you Run the program then Command1 will only be enabled when there is text typed
into Text1, other wise command1 is disabled.
Byte : - The Byte data type is occupies 1-byte, which can store value from 0 to 255. This data
type is very useful for storing binary data.
Currency : - This data type is used for holding values related to item rates, payrolls details and
other financial functions. However this data type should not be used if you need extreme
accuracy beyond the fourth decimal point. For example, if you are working on foreign exchange
details or interest rates for very large values. So user used currency data type. Declaration for
currency is
Dim salary as currency
Date : - This variable holds date and time data. It can hold time from January 1 to December
31,9999 and time from 00.00.00(midnight) to 23.59.59. It occupies 8 bytes of memory. The date
is displayed as per the setting in your computer. You can store it in British format, American
format or any other format that is available in the regional setting on your control panel.
Dim tomorrow as date
Object : - In visual basic forms, controls, procedures and recordsets are all considered as
objects. All programming activity revolves around these objects. Visual basic is very much an
object-based programming language. It is natural to use Object data types.
Variant : - Variant is a special data type that can contain any kind of data except fixed-length
string data. Variant data can also be the special values Empty, Error, and NULL.
Dim a
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 105 of 121
It is a variant data type, because we can mention any data type. It will automatically pick any type
of data in the program.
Q10.Explain the difference between implicit and explicit declarations. Explain with
suitable example.
Ans There are many ways of declaring variables in Visual Basic, depending on where the
variables are declared and how they are declared. We can determine how they can be used by
our application. The different ways of declaring variables in Visual Basic are listed below: -
Implicit Declaration
Using Option Explicit statement
Implicit Declaration
Declaring a variable tells Visual Basic to reserve space in memory. There are two ways to declare
implicit variable.
1. Automatically variable declaration.
2. Using Dim keyword.
1. Automatically variable declaration: - It is not must that a variable should be declared before
using it. Visual basic language provides automatically variable declaration facility, it means we
can use variable without declaration. In this case Visual Basic compiler declares variable
automatically with variant data type and NULL value. This is called implicit declaration.
2. Using Dim keyword: - The variables are declared with Dim (Dimension) keyword and without
using Option Explicit are called implicit variables.
Syntax: -
Dim < variable name > as < date type >
1. Dim is a keyword that tells visual basic that you want to declare a variable.
2. Variable name is the name of the variable.
3. As is a keyword that tells visual basic that you are defining the data type for the variable.
4. Data type is the type of data.
For example
Dim strName As String
Dim intCounter As Integer
In the above example, we declared two variables strName as string type variable and intCounter
is an integer type variable.
Using Option Explicit statement : - It may be convenient to declare variables implicitly. But it
can lead to errors that may not be recognized at run time. For example a variable by name
intcount is used implicitly and is assigned to a value. In the next step this field is incremented by 1
by the following statement
Intcount = Intcunt + 1
This calculation will result in intcunt a value of 1 as intcount would have been initialized to zero.
This is because the intcount variable has been incunt in the right hand side of the second
variable. But Visual Basic does not see this as a mistake and considers it to be new variable and
therefore gives a wrong result.
In Visual Basic, to prevent errors of this nature we can declare a variable by adding the following
statement to the general declaration section of the Form.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 106 of 121
Option Explicit : - This forces the user to declare all the variables. The Option Explicit
statement checks in the module for usage of any undeclared variables and generates error
reports to the user. The user can rectify the error on seeing this error message.
The Option Explicit statement can be explicitly placed in the general declaration section
of each module using the following steps.
Click Tools menu then select Options
Then it will display Options dialog box that contains many tabs.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 107 of 121
- Subtract 10-5 5
/ Divide 25/5 5
\ Integer Division 20\3 6
* Multiply 5*4 20
Mod Remainder of division 20 Mod 6 2
Logical Operators : - These operators are used to combine two or more relational expressions.
They also return their result in a Boolean form.
Various types of logical operators used in visual basic but basically we use three operators in
visual basic. All LOGICAL operators have priorities lower than arithmetic and relational
operators. Therefore if an expression involving arithmetic relational and logical
operators, the arithmetic operators are evaluated first, followed by the relational
operators, followed by the logical operators.
Operators Description
OR Operation will be true if either of the operands values are true
AND Operation will be true only if both the operands values are true
NOT If operand value is true then it returns false.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 108 of 121
selection statement allows a programmer to test the value of a conditional expression and to
select or reject the execution of the statement or block of statements depending on this value. It
is also known as single block statement.
Syntax If...Then selection statement
2. If...Then...Else selection statements : - The If...Then...Else selection statement allows the
programmer to specify that a different action is to be performed, when the condition is True or
when the condition is False. If Expression is true then it executes first statement block, otherwise
it executes second statement block.
Syntax of the If...Then...Else selection statement
If <condition > Then
statements First Statement Block
Else
statements Second Statement Block
End If
3. If .ThenElseif selection statement : - If we have more than two conditions then we
uses if else if statement. In this we can create multiple blocks of statements and it will execute
according to the condition.
Syntax of the If...Then...Elseif Else selection statement
If <condition > Then
statements First Statement Block
Elseif <condition > Then
statements Second Statement Block
Else
Statements Third Statement Block
End If
4. Nested If statement : - Nested If statement means, if statement within if statement. First if
statement is called outer if statement and second is inner if statement. If outer if statement is true
then check inner if statement, but If outer if statement is false then doesnt check inner if
statement.
Syntax of the Nested If statement
If < condition 1 > Then
If < condition 2 > Then
statements
Else
statements
End if
Else
If < condition 3 > Then
statements
Else
statements
End if
End If
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 109 of 121
5. IIF( ) statement : - IIF( ) statement means Immediate IF statement. Visual basic offers IIF( )
is a function that can be used as an alternative to simple IfThen.Else statement. Basically
this function is used to combines a single IfThen..Else statement in one line.
Syntax of the IIF ( ) statement
IIF (Condition, Expression1, Expression2)
6. Select...Case statement
Select case statement is used, when a single variable has multiple choices. In select case
statement we can gave multiple cases, if case will match then it will execute related block
otherwise default case will be executed. Select case statement is an alternative of the
If...Then...ElseIf statement. Select case statement is more convenient to use than the
If...Then...ElseIf statement. The variable used in select case statement is known as select case
variable. The variable in the select case statement can be integer or string type only.
In select case statement we use select, case and else keywords.
Syntax: -
Syntax of the Select...Case statement
Select Case <expression>
Case value
Statements
Case value
Statements
Case else
Statements
End Select
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 110 of 121
3. Do While... Loop Statement : - Do While..Loop is used to repeat the set of statements till
particular condition is true but if condition false then loop stop its works. It is a true loop. In Do
While.Loop initialization, condition and updation statements gave separate(different )lines in
the loop. Its also known as entry control loop or top check loop. It means loop checks the
condition first then it execute the statements.
Syntax: -
Initialization
Do While condition
Statements
updation
Loop
4. Do Until...Loop Statement : - The Do Until.. Loop statement is used to repeat the set of
statements till a particular condition is false if given condition is true then the loop will terminate.
Its also known as false loop. Its also known as entry control loop or top check loop.
Syntax: -
Initialization
Do until condition
Statements
updation
Loop
5. While... Wend Statement : - While..Wend loop is used to repeat the set of statements till a
particular condition is true, if condition is false then loop will stop works. Same as Do
While.Loop. In WhileWend loop have three statements initialization, condition and
updation that written in separate (different) lines. Its also known as entry control loop and top
check loop. The While..Wend loop is an older statement pair from previous versions of BASIC
and was included in Visual Basic for compatibility. WhileWend loop cant terminate using Exit
keyword and another hand Do While..Loop can terminate using Exit keyword.
Syntax: -
Initialization
While condition
Statements
updation
Wend
Q14. What is menu how we can create menu and sub menu in visual basic?
Ans: menu is mostly used in all the application software. A list of commands or options from
which you can choose. Most applications now have a menu-driven component. You can choose
an item from the menu by highlighting it and then pressing the Enter or Return key, or by simply
pointing to the item with a mouse and clicking one of the mouse buttons.
The antithesis of a menu-driven program is a command-driven system, in which you must
explicitly enter the command you want rather than choose from a list of possible commands.
Menu-driven systems are simpler and easier to learn but are generally not as flexible as
command-driven systems, which lend themselves more naturally to interaction with programs.
Steps to Create a Menu Through Menu Editor
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 111 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 112 of 121
Believe it or not, this type of mistake is the most common problem in Visual Basic programs. It's
a matter of code that works most of the time but not all of the time and it's the hardest
problem to track down and fix.
Syntax Errors : - A syntax error (compiler error) is a programming mistake that violates the rules
of Visual Basic, such as a misspelled property or keyword. As you type program statements,
Visual Basic points out several types of syntax errors and you wont be able to run your
program until you fix each one.
Runtime Errors : - A run-time error is any error usually an outside event or an undiscovered
syntax error that forces a program to stop running. Two examples of conditions that can
produce run-time errors are a misspelled file name in a
LoadPicture function and an open floppy drive.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 113 of 121
such as C++, provide programming mechanisms that simplify the implementation of COM
objects. The family of COM technologies includes COM+, Distributed COM (DCOM) and ActiveX
Controls.
BENFITS OF COM : - COM is a technology to create binary executables tha allow the
programmers to reuse.
The components defined for one application into another. Following benefits are provided
by the comp component
Programming Languages : - The COM components can be built using various programming
languages such as C, C++ and Small talk. So the component produced in one languge can
easily deployed and integrated with other languages.
Reusability Concept : - Once the component is written it can be used in numbers of application.
COM component provide Code reusability concept so they can used in numbers of
applications. After created once. Backward Compatibility
All types of COM components generally support Query Interface requests. Query
interface also allows the programmers to create new interfaces with additional functionality. The
new interfaces developed by the Queryinterface are made backward copatible with existing comp
components.
Maintain Version Consistancy : - COM components also provide the version consitancy. When
any version of the com component is created. Prorgrammers keep track the older versions
of the same components and make the backward compatibility. This approach solves the
problem of multiple versions for the COM components.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 114 of 121
ActiveX Control : These Components are used to create applications that can be deployed on
remote server architecture. The file extension for these files is .ocx. These files requires
cabinet (CAB) files for proper deployment of the control on the client machine.
ActiveX Document EXE : These components are used to create controls fo web-based
applications. These controls can be published to a web site. The client downloads the
corresponding control from the web site using the web browser.
ActiveX Document DLL : These components are also used to create components for web
based application like ActiveX document EXE. In contrast to the ActiveX Document Exe, these
files require container files that can call the dll files to execute the required functionality.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 115 of 121
1.DAO stands for Data Access Object. This is the older technique to link front end application
with backend database in vb. The Default control for data connectivity is available with DAO. With
the help DAO we can connect access 97 , excel , dbase3 etc.
2.RDO stands for Remote Data object used to connect front end application that is available on
different computer can connect with server to access data remotedly.
RDO helps to make remote application can only connect client / server application.
3.ADO stands for ActiveX Data Object this is the newest technique to connect front end
application with back end database. ADO provide two method to connect database 1. Active Data
Control and ActiveX Data Object Library.
For e.g
Text1.text=a
Text2.text=Asc(Text1.text)
The output will be - 97
Instr ( ) - It returns the position of first occurrence of a string within another.
For e.g. :
For e.g.
Output - olleh
Len ( ) - It returns the number of characters in a string.
For e.g.
Text1.text = HELLO
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 116 of 121
Text1.text = hello
Text2.text = Ucase (text1.text)
Output - HELLO
Left ( ) - Removes specified number of characters from beginning of a string
For e.g.
Text1.text = hello
Text2.text = Left (text1.text,3)
Output - hel
Right ( ) - Removes specified number of character from the end of a string
For e.g.
Text1.text = Hello
Text2.text = Right (text1.Text,2)
Output - LO
Mid ( ) - Removes specified characters from middle of a string.
For e.g.
Text1.text = Visual Basic
Text2.text=Mid (text1.text,8,5)
Output - Basic
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 117 of 121
14 And click on finish button to finish the wizard now you setup is ready for deployed
When you click the Finish button, the package will be created and ready for distribution. Here
are the packaged files for the start program for download at Setup.exe
SETUP.LST You must download all files into a folder and then run the setup program.
Q23.What are various control structures in VB? Give their syntax. (v.imp)
Ans.Select Control structure
Control structure is used to control the flow of program's execution. Visual Basic supports control
structure such as If. Then, If. Then Else , Select .. Case and loop structure such as
Do While .Loop, Do Loop while and For Next method.
If. Then Else Statement
The If.. Then block is used for conditional execution of one or more statements.
If condition then
Statements
End If
The statement is executed only if the condition is true. The condition is usually a comparison, but
it can be any expression that evaluates a numeric value. If then Else block is used to define
several blocks of statements, in order to execute to one block. The following block of statement
illustrates if. Else.. End if statement.
If A =1 Then
Statements
Else
Statements
End if
The following example illustrates the If .. Then.. Else statement.
E.g.- Illustrate an example to check for the number of digits in a number and display a message
indicating the number of digits in it within 1000.
A new Standard EXE project is opened and a TEXT Box and Command Button is added to the
Form. The following code is entered in the Command 1_Click () procedure.
Private Sub Command 1_Click ( )
If Val (Text1. Text)<10 then
MsgBox " It is a Single Digit Number"
Else if Val (Text1.Text)<100 then
MsgBox "It is a two Digit number"
Else if Val (Text1. Text )<1000 then
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 118 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 119 of 121
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 120 of 121
A For .Next loop can be terminated by an Exit For statement . Consider the following
statement block.
For I= to 100
Text1.Text =Str (I)
If 1=50 Then
Exit for
End if
Next
The proceeding code increments the value of I by 1 until it reaches the condition I =50. The
Exit for statement is executed and it terminates the For Next loop. The following statement
block containing Do While loop is terminated using Exit do statement.
I=1
Do while I<1001
Text1. Text <1001
I=I+2
If I>500 then
Exit Do
End if
Loop
With End With Statement
When properties are set for objects or methods are called, a lot of coding is included that acts on
the same object. It is easier to read the code by implementing the With-End with statement.
Multiple properties can be set and multiple methods can be called by using the With End With
Statement. The code is executed more quickly and efficiently as the object is evaluated only
once. This concept can be clearly understood with the following example.
With Textprice
. Font. Size =16
. Font. Italic =True
. Height =235
. Text="Welcome to Visual Basic6.0"
End with
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100
SUBJECT: VB (PGDCA - 2)
Page 121 of 121
In the above coding, the object Text price (The Name property of Textbox has been changed to
text price) is evaluated only once instead of being evaluated for every associated property or
method. This makes the coding simpler and efficient.
Prepared By: - Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala.
Ph. 0175-2205100, 2215100