Return
to IFS 110 class Notes Menu!
Project 4
Menus, Data Controls Common Dialogs, and
General Procedures
Visual Basic Capabilities
- Visual Basic can be used
to build applications that display, edit and
update
information from databases created
by the following DBMS products:
Microsoft Access, dBase, FoxPro, and Paradox.
Project 4 Outline
- Project 4 provides exposure to accessing
a database by building an application that displays
information from a World Geography database created
with Access
- World Geography
database is included on Student
Diskette.
- Project describes only how to display
information. VB can be used to create applications that query, add,
change,
and delete records are covered in
later project.
Common Database Terms
- field - single item of data (characteristic
or attribute )
(graphic image -
country flag)
- record - group of fields (person
or item)
- table - group of related records (file)
- database -collection of related facts organized in a
systematic
manner
- DBMS - database management system -
collection of software that manipulates
a database
Geography Database Viewer
- Geography Database Viewer does not have any
visible
controls
- reason all interaction occurs
by using menus or the Data
Control
- Select menu - Next, Previous, First and Last
- File menu - Exit
- Options Menu
- View menu Name, Capital, Government
- Font menu Font common
dialog box
- Shortcut menu
can also be used (Options)
Getting Started
- The database file WORLD.MDB and the icon named
Earth.ico used in this project is found on the Student
Disk.
- These files should
be copied to another disk that
will be used later to save
the completed application
Creating the Interface
- Size and Location
of Form determined
- Viewer form contains -
one image control, 6 label controls, one data
control
and
one common dialog control
Note: common dialog control
is not visible at run-time
Adding Label Control Array
- Control array - is made
up of a group of controls of the same
type that share
a common control name and
a common set of event procedures
- Array index - a unique index
number is assigned by Visual Basic to
each element of the array starting at 0
Control Arrays Continued
- All items in control
array must have the same Name property setting
- All other property settings
apply only to the individual control
- Example: Label1(0) can have a caption and other property
settings different from Label1(1)
- Arrays make it easier
to change a property of a group
of controls to a common value during run-time.
Label Array Properties
- FontSize can be changed
through a simple code structure
- Six labels have AutoSize
property set to True and have the same BackStyle property (Transparent, Opaque)
- These two properties will
be set for the first
control during the interface
building step, rather than during the set
properties step, so that these properties can
be copied to the other controls
Adding Common Dialog Control
- Common dialog control is
used in applications to display one of five
common dialog boxes at run-time
- Open, Save As, Print, Color,
and Font
- Common dialog control invisible
(run-time)
- Common dialog control is
an ActiveX control and must be added
to the Toolbox before you can use it in your application
Adding Data Control
- data control used in
Visual Basic applications to provide the necessary links
to a database file
Note: A form can contain more than one
data control, and generally one data control
exists for each database table accessed by the
application
- data control has four
record selection buttons used during run-time
to move between records
Geography Database Viewer
Data Control
- Geography database Viewer
application contains a data control to link to the
World
database. (WORLD.MDB)
- Record selection is controlled
through the Data control or the Select
menu
- Code will be written that
causes the Select menu commands to trigger
data control events (data control needed by
the menu)
Creating Menus
- Menu Design Window -
Visual Basic's tool used to create menus at design
time
- Access keys - allow you
to open a menu at run-time by pressing the ALT
key and a letter. (Hot-keys) (& used at design
time)
Note: When a menu is open,
you press only the access
key of a command (not the ALT key.)
Example: Exit (press
only the x key)
Menu Concepts
- separator bar - horizontal line used to visually
group related commands in a menu
- added to menu at design time by
typing a hyphen (-) in the Caption
text box
- submenu
- branches off another menu to present an additional
set of commands in a separate
grouping
- Visual Basic can have up to four levels of
submenus
Setting Properties (steps)
- Properties of form
- Properties of image control
- Caption property of the label
controls
- Properties of the CommonDialog
control
- Properties of the data
control
- Properties of the data-aware
controls
Setting Properties of Form
- Name of the form is set
- Form is given a Caption
to appear in the title bar
- Form's BackColor
is set to match the BackColor of the label controls
- BorderStyle property (1-Fixed
Single)
- MinButton property set to True
- Icon specified to
represent the form when it is minimized
CommonDialog Control
Properties
- Flags property used to
set dialog box options
- Flags value = 1
(Font dialog box lists only screen fonts supported by system)
- In Visual Basic, default for FontName
property is determined by
the system.
- Fonts available with VB
vary depending on your system configuration.
For this reason, when Font dialog box displays, a preselected
Font does not exist.
- Must set this FontName
property at design time
or with code at run time
Data Control Properties
- Caption property - of Data control is text
that displays between
control's next and previous
record buttons
- Align property - determines whether control
can be located anywhere on form or
whether it is sized automatically to fit
the
form's width and displayed at the top, left, right, or
bottom of the form
Setting
Properties of Data Control
- DatabaseName property - used to supply the file
name of
the database to which the data
control is linked
- Record Source property - used to specify the table
name in
the database to which the data
control is linked
- Connect property - used to specify the type of
database (Access, FoxPro, dBase etc.) Note: not
necessary to set connect property when
Microsoft Access used
Setting Properties
Data-Aware controls
- data-aware - controls
within Visual Basic that can be linked to
information in a database
- data-aware controls are check
boxes, images, labels, picture boxes, and
text
boxes.
Setting Properties
Data-Aware controls
- to use these controls to access a
database, the controls must be bound to a data
control on the form
- In multiform
application, the bound control and data
control to which it is bound must be
on the same form
- Data-aware controls are bound to
a data control by setting their DataSource and DataField
properties
Setting Properties
Data-Aware controls
- DataSource property - of
a control specifies the name
of the data control to which it is bound
- DataField property of a bound
control specifies the name of a
field in the database to
which the control is linked
- Not necessary to change
caption property of bound labels to blank since captions will take on
field
values of 1st record of table
Writing Code (table 4-2 VB 4.38)
Geography Database Viewer code tasks
- frmGeoView_MouseUp subroutine
- mnuExit_Click subroutine
- Select menu subroutines
- mnuNext, mnuPrev, mnuFirst, mnuLast
- mnuFont_Click subroutine
- mnuView_Click submenu subroutines
- mnuName, mnuCapital, mnuGovt
QueryUnload Event
- Before a form is unloaded,
the Form_QueryUnload event procedure occurs, followed by
the Form_Unload event procedure
- Event typically used to make sure that
before an application closes, no unfinished tasks
remain
- (put code in this event
reminding users to save data)
Select Menu Subroutines
- One event is associated
with each of the four commands in
the Select menu: mnuNext_Click, mnuPrev_Click, mnuFirst_Click, and mnuLast_Click
- These actions are
the same as if the data
control was clicked on
the appropriate record selection
button
- Actions in code with
methods MoveNext, MovePrevious, MoveFirst, and MoveLast
Code statement form
- DataControlName.Recordset.MoveMethod
Example: Data1.Recordset.MoveNext
- EOF property - End
Of File occurs when on last record and MoveNext method is used (property
changes from false
to true)
- BOF property - Beginning
Of File occurs when on 1st record and MovePrevious method is used (property
changes from false
to true)
Coding EOF statement
Data1.Recordset.MoveNext
If
Data1.Recordset.EOF = True Then Data1.Recordset.MoveFirst
Note: If the MoveNext
command causes the current record to move past the
last record, then the first
record in the database becomes the
current record
MouseUp Event
- MouseDown or MouseUp
event used to specify actions that
will occur when a given mouse
button is pressed or released
- (enable to distinguish among left, right, and
middle
mouse buttons) (2=right)
If Button = 2 Then
PopupMenu mnuOptions
- Options pop-up menu to
display when user right-clicks the
form
- can specify position of pop-up
menu, use default
location (current location mouse
pointer)
Using Font Property
- Font properties - selected font properties becomes
the values of common
dialog control's font properties when the Font
dialog box is closed
- Set Font properties of the 6 labels to equal Font
properties of common dialog control
Code :
Label1.FontSize = CommonDialog1.FontSize
For...Next statement (loop)
For counter = start To
end (header
part)
[statements] (repeated)
Next (terminator
part)
- counter - numeric
variable (loop counter)
- start - initial
value of counter
- end - final
value for counter
- Next - Ends
a For...Next loop; causes 1
to be added to counter
With Statement
- With statement used to execute
a series of statements
on a single object or control
- Syntax:
Syntax:
With object
[statements]
End With
- Object - is the name
of the object or control
- [statements] - group of code
statements that operate on object
Potential Problem
- Possible to increase font
size where captions extend beyond
form's right border
- Solution: - write code statements
that adjust form's size (Height and Width) and controls'
locations (Top and Left)
- In GeoView application
code statements adjust the Width of
the form and center
the Image control right to left
For...Next and Arrays
- mnuFont_Click
subroutine uses For...Next
loop to set the Font properties of
the 6 labels using With statement
- Coding:
Coding:
For X
= 0 To 5
With Label1(X)
.FontName = CommonDialog1.FontName
(etc)
End With
Next
General Procedures
- General procedures include subroutines,
also called Sub procedures, and functions
- Function procedures and Sub
procedures are separate procedures
that can take arguments, perform a series
of statements, and change the value
of their arguments
- Sub procedure cannot be
used in an expression
General Procedures
Advantage: - when multiple events
execute similar code, it is more efficient
to put code in a general procedure and then activate
the general procedure from the
appropriate events
- Activate or call a Sub procedure using the
procedure name followed by the argument list
Example of call: - ToggleView mnuName, 1
General Procedures
- General procedures may have none or one
or more arguments (multiple
arguments are separated by commas)
- After the general procedure is
completed, program control returns
to the code statement
following the one that called
the general procedure
Check Marks in Menus
- Checked property of menu
can be set at design-time
by clicking the Checked
box in the Menu Design Window (causes
check marks to appear before menu item when program runs)
- Code can be used
to add or remove
checks during run-time by setting menu control's Checked
property to True or False
Code: mnuFlagshow.Checked = True
Project 4
Menus, Data Controls Common Dialogs, and
General Procedures
END
Program
Creating a Database Viewer Application Requirements
Return
to IFS 110 class Notes Menu!