Edp
Edp
Edp
What are the differences between Combo box and List box? (3) [07]
List Box Combo Box
Definition:- It contains lists of items and provides Definition:- It combines a dropdown list with the
the facility to select items from the list of items. data control and the dropdown list is populated at
Items can only be shown at runtime. runtime depending upon the data supplies to it.
(*) It displays a box with a list of item from which a (*) It provides a combination of a list box & text
user can make a selection. box control.
(*) It doesn’t have any edit area to enter (*) Combo box control has an edit area where user
information at runtime. can enter or edit information at runtime.
(*) It has 2 style properties (*) It has 3-type properties
• 0 (zero) Standard • 0 (Zero) dropdown combo.
• 1 (One) Checkbox list box. • Simple combo.
• Dropdown list.
(*) It has multi select property to make multiple (*) It has no multi select property.
selections.
(*) It has “selcount” and ‘selected” Properties. To (*) No such property Is available.
count the no. of selected item in the list & the
selection status (True if selected, False if not
selected) of an item in the list respectively.
How many types of modifiers are there in class modules? Explain them shortly. (9) [07]
Access modifier Access level granted
Visual Basic Notes by SATYAJIT, ANAND 2
Public unrestricted
Any code that can see a public element, can access it.
Protected derivational
Code in the class that declares a protected element or a class
derived from it can access the element.
Friend Assembly
Code in the assembly that declares a friend element can
access it.
Protected friend Union of protected & friend
Code in the same class or the same assembly as a protected
friend element on within any class derived from the elements
class, can access it.
Private Declaration content
Code is the type that declares a private element, including
code within contained types, can access the element.
In visual basic, we declare structure using the type ….end type construction. The structure and its members
are all default to public access. Explicit access declaration is optional. The following example shows a valid
structure declaration.
Type employee
Emp number as integer ‘default to public access
Emp of pice as string ‘connot be declared
Emp home phone as string ‘ private in to access
“Visual Basic provides us a complete set of tools to specify Rapid Application Development (RAD)”. –
Justify. (3) [08]
In the early days of computer programming even a single program could take days or even weeks to
complete. When Visual Basic was first introduced in 1991, it revolutionized programming we no longer
needed to write code to create a user interface, and we no longer needed to worry about memory
management. This new way of programming was known as Rabid Application Development, or RAD.
The main benefit of RAD programming is increased productivity – Visual Basic of 6.0 has many features that
can help us to create better applications, faster than ever before, the following are few of features-
1. Code snipper ->small block
2. Development with Mg
3. Intelligence.
Distinguish between module level and procedure level variables. (3) [08]
Module level Procedure level
i) Module level variables defined at module i) Procedure level variables defined inside a
level are known is member variable. procedure are known as local variable.
ii) Member variable have a default public ii) Local variable nominally save public
access which means any code that can access, although only code within their
access them. procedure can access them.
iii) We can change this accessibility by iii) We can’t change the access level of a
specifying an access modifier. local variable, but we can change the access
level of the procedure that contains it.
What is record set? Differentiate between Active X DLL & Active X EXE. (5) [08]
Record set objects are used to manipulate data from a provider. When we use ADO(Active X data object),
we manipulate data almost entirely using record set objects. All record set objects consists of records (rows)
and fields (columns). Depending on the functionality supported by the provider, some record set method or
properties may not be available.
The three types of record set are:-
• Dyna sets, which are updatable views of data
• Snap shorts, which are static (read-only) views of data.
• Tables, which are direct views of tables.
Active X DLL Active X EXE
What is Dynamic Array? In what situations is it used? Explain with an example. (5) [08]
Sometimes we may not know how large to make an array. Instead of making it large enough to hold the
maximum of data (which means the most of the array may be empty) we can declare a dynamic array. The
size of a dynamic array can vary during the course of the program, or we might need an array until the user
has entered a bunch of data and the application has processed it and displayed the results. With a dynamic
array, we can discard the data and return the resources if occupied the system.
Example:= private sub command1_click()
Dim dynastring() as string
ReDim dynastrings(10)
Dynastring(1)=”the first string”
Need more data space
Redim dynastrings(100)
Dynastrings(50)=”the fiftieth string”
End sub
How can you add the ADO control in your application? (2) [08]
To create the ADODC project, follow the steps:-----
• Start a new project, & add the ADO data control component to the project’s toolbox.
• Place an instance of the ADO data control on the form.
• Right click on the control & from the shortcut menu, select properties to open the property pages
of the ADO data control.
• Select the general tab and check the use ODBC data source name option.
• Now specify data source by selecting new button to open the create new data source window.
Among the three data source,
Fill data source
User data source
System data source, select system data source
So that we can test the locking mechanism by logging on the mechine from the network.
• Click next and specify the driver and set if to access database.
• Click finish to create the data source.
What do you mean by Connection String and Record Source in ADO data control? (3) [08]
Connection string property specify a data source by passing a detailed connection string containing a
series of argument=value statement separated by semicolons.
ADO supports five arguments for the connection string property:
• Provider:= specify the name of a provider to use for the connection.
• File name:= specifies the name of a provider specific file ( for example:= a persisted data source
object) containing present connection information.
• Remote provider:= specifies the name of a provider to use when opening a client side
connection. (Remote data service only)
• Remote server:= specifies the path name of the server to use when opening a client side
connection. (Remote data service)
• URL:= Specifies connection string as an absolute URL identifying a resource , such as a file or a
directory.
** Connection string property is read/write when the connection is closed and read-only, when it is open.
Record source:=
a) Gets or sets the statement or query that returns a Record set.
Visual Basic Notes by SATYAJIT, ANAND 7
b) The Record source contains both the name of a database table and a valid SQL string using syntax
appropriate for the data source.
What is menu structure? How can we use menu in a window application? (3+4) [08]
Creating a simple menu:
Most windows application have a file menu that allows users to open and save data, exit the application and
so on.
To make it:
• Start up a new standard EXE project in visual basic and make sure that the form is visible.
• Click on the menu editor icon on the visual basic toolbar.
• When the menu editor dialog appear, enter and file in the caption property and menu file as the
name property.
• Now press Enter. Visual Basic will store the menu item in the list area at the bottom of the dialog and
move the highlight down to the next line. Now click on the right direction arrow.
• Use the same process to enter the open and save as menu items. First enter and open for the
caption , mnuopen as the name. then enter save & as...(note the space!) as the caption, and finally
mnuSave As as the Name property. The Name and caption properties are the bare minimum we can
specify when creating a menu.
• Now click on ok button.
Differentiate between Input Box and MSG Box? (3) [07, 08]
We can use msgbox for display the message and getting input from user like VB yes/no method, but
inputbox only for input. For msgbox we can't set the x, y margin, but in the input box we can set.
Differentiate between property Let property Get property Set procedures. (5) [08]
Property Let: Which is used by the outside code to store a value in the private property variable. It is
similar to a procedure in the sense that it does not return a value. A Property Let procedure must accept at
least one argument. If not explicitly specified using either Public or Private, Property Let procedures are
public by default, that is, they are visible to all other procedures in our script.
If the private variable we are using is an object then the process of assignment and data validation is
handled by Property Set. Property Set is similar to Property Let, but used for object based properties. By
default, the Property Set procedure is Public.
Property Get: This is used by code outside of our class to read the value of a private property
variable. It is similar to a function in the sense that it returns a value to the calling code — this value is the
private variable value. The Property Get procedure does not accept any arguments. We can add an argument
to it, but then we have to add an additional argument to the property’s corresponding Property Let or
Property Set procedure, because Property Let/Set procedure must always have exactly one more argument
than its corresponding Property Get procedure.
A procedure is a set of one or more program statements that can be executed by referring to the
procedure name. We can reuse the code written in a procedure, as it can be executed any number of times
by making a call to the procedure.
Advantage:-
1) Allow us to break an application into discrete logical units, thus making the application more readable.
2) Procedures help in debugging an application because debugging separate units of an application is easier
them debugging the application as a whole.
3) Procedures are reusable across programs with little or no modification.
How many types of procedures are there in in Visual Basic? (1) [08]
a) Sub procedure
b) Function procedure
c) Property procedure
In sub procedure has two types:---------
General procedure
Event handling procedure
Explain with example “call by value” and “call by reference” function call? (5) [07, 08]
Let's say we have an integer variable named x.
A call to a function by value using x means (a copy of) the value that x stores is passed in the function call and
no matter what the function does with that value, the value stored in x remains unchanged.
A call to a function by reference using x means a reference (also called a pointer or alias) to the variable x is
passed in the function call and so any changes the function makes using this reference will actually change
the value stored in x.
Visual Basic Notes by SATYAJIT, ANAND 10
When referencing something by ByVal in our method we are only taking a 'copy' of the value, so any changes
are only made to our copy. When using the ByRef we are taking the reference of our object so any changes
made are made to the actual object.
Call by value Call by reference
Private sub button1_click() Private sub button1_click()
Dim a as integer b as integer Dim a as integer, b as integer
A=10 A=10
B=2 B=2
Sum=add(a,b) Sum=add(A,B)
Debug.print A Debug.print A
Debug.print B Debug.print B
Debug.print sum Debug.print sum
End sub End sub
Function add(byval num1 as integer, byval num2 Function add(num1 as integer, num2 as integer)as
as integer)as integer integer
Add=num1+num2 Add=num1+num2
Num1=0 Num1=0
Num2=0 Num2=0
End function End function
Output Output
10 0
2 0
12 12
What is the difference between call by value and call by reference? (4) [08]
Call by reference Call by value
What is a menu? How can you add menu to particular VB application. What is control menu? What is
purpose of menu editor? How is menu editor accessed?
Visual Basic Notes by SATYAJIT, ANAND 11
A menu is a list of commands or options from which you can choose. You can choose an item from the
menu by highlighting it and then pressing the Enter or Return key. There are several different types of
menus:- pop-up menu, cascading menu, menu bar etc.
VB programmers can create menus by first selecting the form that will host the menu and then using the
VB Menu Editor, as shown in Figure. The Menu Editor is
available only when a form is being designed. It is
located on the Tools menu in VB6.
In how many ways a visual Basic application can connect to a database? (3)
Visual Basic Notes by SATYAJIT, ANAND 12
What is Seek Method? Which type of record set is available in this? (3)
The seek method of Record Set object can instantly locate a record in a table based on the value of an
index field. The seek method is extremely fast because it uses the index of a table, and in effect, locates an
item in a sorted list. The seek method has the following syntax: Seek operator, key
The operator argument is one of the following relational operators:
• = (equal)
• > (greater than)
• < (less than)
• >= (greater or equal)
• <= (less or equal)
The key argument is the value to be compared with the key field of the index. If the index is made up of a
single field, key is a single value. If the index is made up of multiple fields, the key argument may contain
multiple values, separated by commas.
only table type record set support the Seek method.
Visual Basic Notes by SATYAJIT, ANAND 13
What is MDI? What are the advantages of MDI forms and its features? (3+2+2)
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. Multiple Word is a typical example.
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.
At run time, an MDI form and all of its child forms take on special characteristics:
• All child forms are displayed within the MDI form's workspace. The user can move and size child
forms like any other form; however, they are restricted to this workspace.
• When a child form is minimized, its icon appears on the MDI form instead of the taskbar. When the
MDI form is minimized, the MDI form and all of its child forms are represented by a single icon. When
the MDI form is restored, the MDI form and all the child forms are displayed in the same state they
were in before being minimized.
• When a child form is maximized, its caption is combined with the caption of the MDI form and is
displayed in the MDI form's title bar (see Figure 6.6).
• By setting the AutoShowChildren property, you can display child forms automatically when forms are
loaded (True), or load child forms as hidden (False).
• The active child form's menus (if any) are displayed on the MDI form's menu bar, not on the child
form.
Advantages of MDI:
MDI applications can often handle multiple documents more readily than SDI programs. For example, many
MDI text editors allow the user to open multiple text files side by side in the same window, making it easy to
compare and look up information from a second document while working on the first.
MDI applications tend to perform more quickly than SDI programs, since only one version of the application is
loaded into memory.
Uses MDI applications can be used for a variety of purposes - for example, working on one document while
referring to another document, viewing different presentations of the same information, viewing multiple
Web sites at the same time, and any task that requires multiple reference points and work areas at the same
time
MDI &SDI: - Single Document Interface (SDI):- The applications that allow the opening of single data
window at any point of time are called SDI.
With SDI applications, we can’t open multiple data windows simultaneously.
For e.g.: In window Note Pad application, only one data document can be opened at a time.
Therefore, it is an SDI application.
Multiple Document Interface (MDI):- The applications that allow the users to open multiple data windows at
the same time are said to provide MDI.
For ex, we can open multiple data documents simultaneously in MS-Word. Thus MS-Word is an MDI
application.
Difference Between MDI & SDI:-
SDI MDI
Visual Basic Notes by SATYAJIT, ANAND 14
(i) It stands for single Document Interface. (i) It stands for Multiple Document Interface.
(ii) There is no concept of parent window (ii) In MDI all the windows are a part of a larger parent
rather every window is independent of each window
other. (iii) It acts as an container for other windows or forms.
(iii) It act as an container for other controls or (iv) It has MDI-child property which is used to setup a
objects window as child.
(iv) No such type of property is available (v) The windows can be moved around only with the
(v) All the windows can be moved freely parent window.
What do you mean by event-drive model? How is it differing from procedural model of programming?
(2+2)
In procedural model we write instructions and expect them to operate in the order specified (or decided by
logic, etc.). Event driven model waits for certain events to happen and then reacts accordingly. For example,
we might calculate a complicated math problem using procedural style because that scenario works well for
that paradigm (the algorithm for the math problem can be programmed sequentially). Events are good for
managing a lot of things at once or stuff we might not expect to happen (or expect to happen, just not sure
when), i.e. a GUI like our web browser.
Input Box() function:- we can use the InputBox() function to get a string of text from the user. the syntax for
this function:
InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile,
context])
The prompt argument is a string displayed as the message in the dialog box.
Visual Basic Notes by SATYAJIT, ANAND 15
The title argument is a string displayed in the title bar of the dialog box. (If you don_t specify the title, the
application name is placed in the title bar.)
The default argument is a string displayed in the text box as the default response if no other input is
provided.
The xpos argument is a number that specifies (in twips) the horizontal distance of the left edge of the dialog
box from the left edge of the screen.
The ypos argument is a number that specifies (in twips) the vertical distance of the upper edge of the dialog
box from the top of the screen.
The helpfile argument is a string that identifies the Help file to use to provide context-sensitive Help for the
dialog box.
The context argument is the Help context number assigned to the appropriate Help topic.
Common Dialogs Control:- The Common Dialogs are all part of one control: the Common Dialog control.
Adding a Windows Common Dialog control to our program is easy: these steps are as follows:
1. Select the Project|Components menu item.
2. Select the Controls tab in the Components box that opens.
3. Select the entry labeled Microsoft Common Dialog Control, then click on OK to close the Components box.
4. Add a Common Dialog control to a form in the usual way_just double-click the Common Dialog tool in the
toolbox, or select it and paint the control on the form.
5. Add the code we want to open the dialog box and make use of values the user sets.
To display various dialog boxes, we use these Common Dialog methods:-
CommonDialog1.ShowOpen_Show Open dialog box
CommonDialog1.ShowSave_Show Save As dialog box
CommonDialog1.ShowColor_Show Color dialog box
CommonDialog1.ShowFont_Show Font dialog box
CommonDialog1.ShowPrinter_Show Print or Print Options dialog box
CommonDialog1.ShowHelp_Invokes the Windows Help engine
Common Dialog box:- A Common Dialog Box is a window used to display and for accept information.
Dialog Boxes can either be modal or modeless.
• A Modal Dialog Box does not let the user continue working with rest of the application until it is
closed.
• A Modeless Dialog Box, on the other hand lets the user shift the focus between the dialog box and
another form without having to close the dialog box
Rich Text Box Control:- This control is use to display text with multiple font & size with the help of third
party custom control. More over it is not related to 64K character like text box control.
It is generally not visible the standard tool bar rather we need to add it form component dialog box by
choosing “Microsoft rich text box control 6.0”
Properties:-
Sellentgth :- It return or set the no. of characters selected .
Selstart :- Returns or set the starting point of the selected text.
Seltext :- Returns or set a string equal to the currently selected text.
SelBold :- Set a string Bold.
SelIalic :- Set a string Iatlic
SelUnderline :- Set a string underline.
Sel Strikethrou:- Set a string Strike Through.
Visual Basic Notes by SATYAJIT, ANAND 16
Native code:- When we select this option, Visual Basic compiles a project using native code, which is the
machine language that the CPU understands and executes. The generated executable is faster than the
equivalent p-code executable by as much as 20 times. When we compile to native code, we have the
following options:
• Optimize for fast code
• Optimize for small code
• No optimization
• Favor Pantium Pro
• Creates symbolic debug info
Benefit compile to native code compiles a project using native code, which is the machine language the
CPU can understand and execute.
As like,
Book tittle= Data1.recordset.fields(0)
Book tittle= Data1.recordset.fields(title)
b) The number of rows in the record set of the data data control is:
data1.record set.record control and the number of columns in the same record set of the
data1 data control is data1.recordset.fields.count.
Dynaset snapshot
Dynaset are updated every time user changes the Snapshots are static views of the same data. A
database and changes they make to the snapshot contains the records requested. The
corresponding record set are reflected in the in snapshot was generated and changes made to the
designing table. order laying table are not reflected in snapshots.
The dyna set with a most flexible and protect full The base flexible record set type, the snapshot is
type of record set but more overhead. the most efficient in terms of overhead.
(*)Control Array: - Array of controls having same type & same name which share common properties is
known as control array. They also share the same event procedures. Since controls have the same name VB
needs a way to distinguish them. This is done by a property called index. This index property identifies the
specific control & gives the flexibility to organize them at runtime.
Control Array can be created either in design time or in runtime.
The maximum index we can use in a control array is 32767.
Elements of the same control array have their own property settings.
Common uses for control arrays included menu controls and option button groupings.
If you want to create a new instance of a control at run time, the control must be a member of a control
array
Using the control array mechanisms, each new control inherits the common event procedures already
written for the array.
(*) What is dynamic array? How to allocate space for dynamic array? How to preserve information in a
dynamic array?
An array which can be resized or re-dimensioned at runtime is known as dynamic array. It can be
declared by the “dim” keyword with empty parenthesis & re-dimensioned by the “redim” statement.
“Redim” statement can only appears within a procedure because it is an executable statement which makes
the application carries out an action only at runtime. E.g
Dim x () as integer
Redim x (5) as integer
Visual Basic Notes by SATYAJIT, ANAND 18
For example the dynamic array matrix one is created by first declaring it at the module level: Dim Matrix 1
() As Integer
A procedure then allocates space for the array:
Sub CalcValuesNow ()
ReDim Matrix (19,29)
End Sub
The ReDim statement shown here allocates a matrix of 20 by 30 integers (at a total size of 600 elements).
Alternatively, the bounds of a dynamic array can be set using variables: ReDim Matrix1(X,Y)
Preserving the content of dynamic array:-
Each time executing the “ReDim” statement al values currently stored in the array are lost. VB reset
the values to empty for variant type, to zero for numeric type , ‘0’ length for string type and nothing for
object type.
In order to preserve the data in an existing array the “preserve” keyword is used when there is a
need to change the last dimension.
Eg: ReDim preserve x(10) as Integer.
ReDim preserve x (<old size> + 1) as integer by this declaration the array size is increased by one but
all the current information remains intact.
RDO:= Remote data object has partly designed to overcome the complexity issue of ODBC(open database
connectivity). It had the simpler programming style of DAO(data access object). (Which is used to allow
programmer to access the set database engine, as supplied with Microsoft access), but used ODBC, thus
allowing connection to different database.
ADO:= Active X data object being the most recent addition in visual basic 6. Visual basic data access tools
DAO, allowed programmer to Access database only. ADO can access all major databases and is Microsoft
foundation for a universal technology for accessing all types of data in all environments.
OLE:=(object linking and editing) The incense of OLE is component s/w with OLE; we can use both visual basic
objects and object exposed by other applications to do the same.
Ex: With OLE, a visual basic application can row functionality from both a word processor and a spreadsheet.
Set focus:- This function sets the keyboard focus to the specified window.
Public :- Indicates that the Function procedure is accessible to all other procedures in all scripts.
Private :- Indicates that the Function procedure is accessible only to other procedures in the script where it is
declared or if the function is a member of a class, and that the Function procedure is accessible only to other
procedures in that class.
The Right Function:- The Right function extracts the right portion of a phrase. The format is
Visual Basic Notes by SATYAJIT, ANAND 21
Right (“Phrase”, n)
Where n is the starting position from the right of the phase where the portion of the phrase is going to be
extracted. For example,
Right(“Visual Basic”, 4) = asic
The Left Function:- The Left$ function extract the left portion of a phrase. The format is
Left(“Phrase”, n)
Where n is the starting position from the left of the phase where the portion of the phrase is going to be
extracted. For example,
Left (“Visual Basic”, 4) = Visu
The Ltrim Function:- The Ltrim function trims the empty spaces of the left portion of the phrase. The format
is
Ltrim(“Phrase”)
.For example,
Ltrim (“ Visual Basic”, 4)= Visual basic
The Rtrim Function:- The Rtrim function trims the empty spaces of the right portion of the phrase. The
format is
Rtrim(“Phrase”)
.For example,
Rtrim (“Visual Basic ”, 4) = Visual basic
The Trim function:- The Ttrim function trims the empty spaces on both side of the phrase. The format is
Trim(“Phrase”)
.For example,
Trim (“ Visual Basic ”) = Visual basic
The Mid Function:- The Mid function extracts a substring from the original phrase or string. It takes the
following format:
Mid(phrase, position, n)
Where position is the starting position of the phrase from which the extraction process will start and n is the
number of characters to be extracted. For example,
Mid(“Visual Basic”, 3, 6) = ual Bas
The InStr function;- The InStr function looks for a phrase that is embedded within the original phrase and
returns the starting position of the embedded phrase. The format is
Instr (n, original phase, embedded phrase)
Where n is the position where the Instr function will begin to look for the embedded phrase. For example
Instr(1, “Visual Basic”,” Basic”)=8
The Ucase and the Lcase functions :- The Ucase function converts all the characters of a string to capital
letters. On the other hand, the Lcase function converts all the characters of a string to small letters. For
example,
Ucase(“Visual Basic”) =VISUAL BASiC
Lcase(“Visual Basic”) =visual basic
Visual Basic Notes by SATYAJIT, ANAND 22
The Str and Val functions:- The Str is the function that converts a number to a string while the Val function
converts a string to a number. The two functions are important when we need to perform mathematical
operations.
The Chr and the Asc functions:- The Chr function returns the string that corresponds to an ASCII code while
the Ascfunction converts an ASCII character or symbol to the corresponding ASCII code. ASCII stands for
“American Standard Code for Information Interchange”. Altogether there are 255 ASCII codes and as many
ASCII characters. Some of the characters may not be displayed as they may represent some actions such as
the pressing of a key or produce a beep sound. The format of the Chr function is
Chr(charcode)
and the format of the Asc function is
Asc(Character)
The following are some examples:
Chr(65)=A, Chr(122)=z, Chr(37)=% , Asc(“B”)=66, Asc(“&”)=38
Hex Function:- returns the hexadecimal value of any number. Generally hex returns a string representing the
hexadecimal nature of a numeric expression or string expression. But hex$ can only take a string expression
as its argument.
The format is:
Temp$ = hex (tempvariable)
Example:=
Dim texthex = as string
Test hex = hex$(a)
Val Function ->This function is used as a conversion function. The Val function does the opposite of Str
Function i.e., this function converts the numbers contained in a string into equivalent number.
The syntax for using this number is:-
Val (String)
Where string can be any valid string expression. For ex, the code Val (“12 and 13”).
Will return a number 12.
Rnd Function -> The Rnd () Function is used to generate a random number. The Rnd () function returns a
single value that contains a randomly generated number less than 1 but greater than or equal to zero.
SYNTAX:-
Rnd [(number)]
Before calling Rnd, you should use the Randomize statement without an argument to initialize the random-
number generator with a seed based on the system timer.
To produce random integers in a given range, use this formula:
Int ((upperbound – lowerbound + 1) * Rnd + lowerbound)
Here, upperbound is the highest no. in the range
Lowerbound is the lowest no. in the range
For ex.- between 1 to 6
Dim Number
Randomize
Number = int ((6-1+1)* Rnd) + 1)
= int ((6*Rnd) + 1)
Cint Function -> (Convert into integer) Cint is a related function that returns truly rounded numbers as
n= Cint (14.1) stores 14, truly rounded number
Visual Basic Notes by SATYAJIT, ANAND 23
Write the steps to create and call a procedure in VB application. (6) [08]
Sub compute Area(Byval length As Double,Byval width As Double )
Dim area as double
If length=0 as width=0 then
Exit sub
End if
Area=length *width
Print area
End sub
Calling a procedure
Private sub command1_click()
Dim l as Integer
Dim w as Integer
l=Input Box(“Enter the length”)
w=Input Box(“Enter with width”)
Visual Basic Notes by SATYAJIT, ANAND 24
compute area(l,w)
end sub
Write a program in VB to check a number given from keyboard is prime or not. (5) [07]
Dim x as integer, re as integer
Private sub form_load()
Form1.show
x=input box(Enter a number)
re=prime(x)
if re = 0 then
print “the no is not prime”
end if
Visual Basic Notes by SATYAJIT, ANAND 25
end sub
function prime(byval re as integer)as integer
Dim j as integer
J=1
For i=2 to n/2
If re mod I = 0 then
J=0
End if
Next i
Prime =j
End function
Write a program that inputs three different integers using Input Box function & prints the Sum, the
Average, the product, the smallest & largest of these numbers on the form. (6) [08]
Using Timer control in a form show the current time and date after it is being loaded. (6) [08]
Write syntax for creating sequential files in VB. Also explain how to read the content From an existing file.
(7) [08]
Opening & Closing the file:
A Sequential file can be opened in 3 ways:
• for Input – when you want to read data from a disk file
• for Output – when you want to create a brand new file & write data to it
• for Append – when you want to add data to an existing file
General syntax for Open statement:
OPEN filename FOR mode As #filenumber
Examples of Open statements:
OPEN “C:\CSC 135\Myfile.dat” For Input As #1
OPEN “A:\Newfile.dat” For Output As #2
OPEN “C:\MyData\OldFile.dat” For Append As #3
If you wish to allow someone to type the file name in a textbox at execution time:
PrgFile = txtFilename.text ’assuming PrgFile is a declared string variable
OPEN PrgFile For Input As #4
OR
If you wish to allow someone to type the file name in an inputbox at execution time:
PrgFile = InputBox(“Enter name of file”, “Filename”)
OPEN PrgFile for Input as #1
When you are finished with the file, be sure to close it. Examples:
Close #1 (closes only filenumber #1)
Close #1, #2 (closes filenumbers 1 & 2)
Close (closes all open files)
Reading the Data from a file:
Must read a record at a time, but each field needs its own variable
Make sure the order of variables follows the order of the data fields on the disk file.
General Syntax:
INPUT #filenum, field1, field2, etc.
Visual Basic Notes by SATYAJIT, ANAND 26
Write a program in VB to test whether a number is a perfect number or not. (8) [08]
Private Sub Form_Load()
Dim x As Integer
Dim y As Boolean
x = InputBox("Enter integer")
y = isPerfect(x)
If y = True Then
MsgBox ("perfect")
Else
MsgBox ("Not perfect")
End If
End Sub
Public Function isPerfect(number As Integer) As Boolean
Dim i As Integer
i = sumFactors(number)
If i = number Then
isPerfect = True
Else
isPerfect = False
End If
End Function
Public Function sumFactors(number As Integer) As Integer
Dim factor, sum, k, i As Integer
factor = 0
sum = 0
For i = 1 To number step 1
k = number Mod i
If k != 0 Then
factor = i
sum = sum + factor
End If
Next i
sumFactor = sum
End Function
Visual Basic Notes by SATYAJIT, ANAND 27
Write a windows API application to determine the position of the mouse when it is moved in the text
boxes. (10) [08]
Page: 652 Mastering VB
In Windows based programming what do you mean by controls, properties of control, events and
methods? (3)
What do you mean by recursive programming? Write a program in VB to find out the Nth Fibonacci series
using recursion. (2 +3)
Currency 8 bytes
Decimal 12 bytes
Non-Numeric data types: - Non-numeric data values are those that cannot participate in calculations e.g. a
string or a true value or a stored form etc.
There are basically 5 non-numeric data types in V.B but two of these data types can further be divided into
two subtypes.
Non-numeric data types are:-
Data Type Storage
a) String
String (fixed length) Length of string
String (variable length) Length + 10 bytes
b) Date 8 bytes
c) Boolean 2 bytes
d) Object 4 bytes
e) Variant
Variant (numeric) 16 bytes
Variant (text) Length + 22 bytes
=> The Tool Box contains various tools, which are to be placed on the form. After these tools have placed on
the form, the user can use the property window and the code window to customize the behavior of these
controls. The various controls on the Tool Box along with their functions are:-
* Pointer: - The pointer is used for selecting objects that have been placed on the form. It is marked by an
arrow cursor and is used to position other objects after they have been selected.
* Picture Box: - It holds pictures created with paint brush and other similar art packages provided they are
in an acceptable format e.g. .BMP, .ICO, .JPG or .DIB. The .PCX format is not acceptable.
* Label: - This control is used to hold text. The user should use labels for messages and prompts.
* Text Box: - This control is also used to hold text, but it is capable of receiving input from the user. So the
user should use textbox for replies.
* Frame: - A frame is like a group box, it holds a set of controls together in such a way that they are all
moved together when the frame is moved.
* Command Button:- A command button represents an action that is carried out when the user clicks the
button e.g. ok, cancel, start, or quit.
* Check Box: - It acts as a toggle switch, checking or un-checking options. Its main property is value, and it
is 0 if the check box is cleared and 1 if the check box is checked.
* Option Button: - They are normally used to select 0ne option from a set of mutually exclusive options.
Its main property is checked, and it is true if the control is checked and false otherwise.
* List Box: - It displays the list of items so that the user can see what is available and select one item. User
can also scroll the list to locate an item. Items can be added only at runtime.
* Combo Box: - This control combines a drop-down list with the data control and the drop-down list is
populated at runtime depending upon data supplied at design time. A combo box control combines the
features of a text box control and a list box control.
* Horizontal and Vertical Scroll Bars: - These bars are used to give a very flexible means of setting values.
Scroll bars provide easy navigation through a long list of items of a large amount of information.
* Timer Control: - It controls actions that take place after set intervals, e.g., if a user wants to
automatically see some object in his application run from left to right or vice-versa, he can use the timer
control.
* Shape: - This control is used to draw graphical elements, such as boxes and circles, on the surface of a
form.
* Line: - Similar to the shape control, the line control is used to draw lines on a form.
* Image: - The image control is used to display a graphic. An image control can display a graphic of
different available formats such as bitmaps, GIF, or JPEG files etc.
* OLE: - OLE stands for object linking and embedding. Using OLE, the user can access and use objects
provided by other applications in his application.
* The Drive List Box, Directory List Box and File List Box can be together used to give standard Windows
File Management facilities. A Drive list box control enables a user to select a valid disk Drive at run time. Use
this control to display a list of all the valid drives in user’s system. We can create dialog boxes that enable the
user to open a file from a list of files on a disk in any available drive.
(*) What are V.B forms? How do you build dynamic forms at run time?
Form: The V.B Form is the container for all the controls that make up the user interface. When a V.B
application is executing each window it displays on the Desktop is a Form. (In Short--These are the windows
that can be customized to serve as the interface for an application.)
=> (i) The simplest approach is to create more controls than you’ll ever need and set their visible property to
false at design time. At runtime, we can display the controls by switching their visible property to True.
(ii) Dynamic form can be created by creating the object of the Form1 control and load the object at runtime
which are actually the members of an array of controls or objects.
For example:-
Visual Basic Notes by SATYAJIT, ANAND 30
* These are used to create reusable process * Standard Exe are used to create executable
component. applications.
* These can run on their own address space. * Standard exe can run on variety of programming
environment.
* They need a container because they cannot run * No need of container for a Standard exe because
stand alone. they can run Stand alone.
* ActiveX Control cannot use an application because * A standard exe or application can use the reusable
they have their own address space. process component Simultaneously.
* ActiveX Control has a file extension i.e. “.ocx”. * Standard exe have a file extension i.e. “.exe”.
(*) How do you create ActiveX Control? How do you register your own ActiveX Control into the windows
Registry?
To create an ActiveX control, as follows:-
Step-1:- Choose ActiveX control form the new project menu.
Step-2:- Place a text box into the form window and write a code for text box.
Private Sub Text1_keyPress (Key ASCII as integer)
If KeyAscii >= 48 AND KeyAscii<= 51 Then
Else
MsgBox “Numeric digits are allowed only”
KeyAscii = 0
Endif
If len (Text1. Text)>50 Then
MsgBox “Max allowed 50 digits”
Endif
EndSub
Step-3:- Now save the program and make an ‘ocx’ form file menu.
Registering the ActiveX Control:- A system call named “RegSvr32” is used for registering ActiveX Control.
For this purpose, from the Run menu, We have to write regsvr32 “xyz.ocx”. After registering, the component
Dialog Box and can be used in one or more application simultaneously.
DLL files: - DLL (Dynamic link library) or execution engine that translates p-code in to machine-executable
code, or native code. That is low projects are executed in the Visual Basic IDE.
A DLL is located at run not at design time, and it does not need to be linked to your application.
Because the API (Application Programining Interface) functions are required for the proper operation of
Windows itself, the DLLs are always available to your application.
(*) Write down the full form of: - DAO, RDO, ADODC, DSN.
DAO Data Access Object-
The DAO is a structure of objects for accessing databases through your code. All the functionality of the Data
control is also available to your code; through the DAO.
ADODC (ActiveX Data Objects Data Control) is equivalent to the Data control.
DSN Data Source Name
DSN is a logical name used by open Database connectivity (ODBC) to refer to the drive and other information
required to access data.
RDO Remote Data Object.
Procedures in VB
A procedure may or may not receive arguments,
SYNTAX:
Sub procedurename (argument list)
‘Statements’
End sub
Ex: - Sub Exit The Program () Empty brackets means no arguments