Upload
emil-griffin
View
214
Download
0
Tags:
Embed Size (px)
Citation preview
Database ManagementReview for Final (Part 1)
School of BusinessEastern Illinois University
© Abdou Illia, Spring 2003
(Week 15, Wednesday 4/30/2003)
2What is a Database ?
Database: Collection of information stored in tables
Example: BIBLIO.MDB
Au_ID Author Year Born
Authors table (6246 Records, 3 Fields)
PubID Name Company Fax Comments
Publishers table (727 Records, 10 Fields)
Au_IDISBN
Title Author table (16056 Records, 2 Fields)
3Table, Field, Record
Authors Table
Au_ID Author Year_Born
A001 Jacobs, Russell 1945
A002 Metzger, Philip W. 1961
A003 Sydow, Dan Parks 1948
: : :
: : :
Field Record
4VB and Database Management
Two tools in VB 6.0 to manage databases – Activex Data Object (ADO)
– Data Access Object (DAO)
ADO control should be added to the toolbox (Project/Components… and check Microsoft ADO Data Control)
ADO and DAO controls can be used to:– Connect to a database.– Open a specified database table.– Create a virtual table based on a database query.– Pass database fields to other Visual Basic tools, for display or editing.
Such tools are bound tools (controls), or data-aware.– Add new records or update a database.
5DAO control
Common properties:– DatabaseName Used to set the name and location of database– RecordSource Used to set specific table in the database– EOF End of File– BOF Beginning of File– RecordCount Number of records
Examples:
Name: datBiblioDatabaseSource: C:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDBRecordSource: Titles
6Bounding Text boxes to DAO control
Text boxes are said data-aware because they can be bound to DAO controls and access their data
Two Text boxes properties are used to bound them to DAO controls: – DataSource Used to set the DAO control from which data is read– DataField Used to set the field of the table from which data
is read
Examples:
Name: txtTitleDataSource: datBiblioDataField: Title
Name: TxtYearDataSource: datBiblioDataField: Year_Published
7Exercise
Create the following user-interface with one DAO control and two text boxes so that data in the table Titles from the database BIBLIO.MDB (found in the main VB directory).
Name: txtTitleDataSource: datBiblioDataField: Title
Name: TxtYearDataSource: datBiblioDataField: Year_Published
Name: datBiblioDatabaseSource: C:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDBRecordSource: Titles
8ADO control: Methods
Common methods used:– MoveFirst Move to the 1st record and make it the current record– MoveLast Move to the last record and make it the current record– MoveNext Move to the next record and make it the current record– MovePrevious Move to the previous record and make it the current record– Recordset Used to refer to the content of the table in RecordSource
Recordset.Fields(“FieldName") Recordset.Fields(" FieldName").Value
Example:
datBiblio.Recordset.MoveLast ' Needed to set the value of RecordCount
datBiblio.Recordset.MoveFirst
For i = 0 To datBiblio.Recordset.RecordCount
datBiblio.Recordset.MoveNext
Next i
9ADO control: Examples
Example 1 : If EOF is reached, move to the previous record
If datBiblio.Recordset.EOF
datBiblio.Recordset.MovePrevious
End If
Example 2 : Delete the current record
datBiblio.Recordset.Delete
Example 3 : Use datBiblio to add items do a list box
To be done in class
12Statements for setting properties
General rule:
ObjectNane.Property = Value
Where ObjectName is the name of the Object
Property is one of the properties of the object
Value is a valid setting for Property
Such statements are called Assignment statements
13Setting colors
At design time– Colors are selected from the palette (in Properties Win.)
When writing code– 8 most common colors can be assigned with the following
color constants:
vbBlack vbRed vbGreenvbYellow
vbBlue vbMagneta vbCyan vbWhite
Example: txtBox.ForeColor = vbGreen
14Assigning Font settings
At design time– Font settings are assigned from the palette (in Properties
Win.)
When writing code– Font settings can be assigned using statements like:
txtBox.Font.Name = “Courier”
txtBox.Font.Name = “Arial”
txtBox.Font.Size = 18
lblOne.Font.Italic = True
15Exercises
Exercises 19-32, page 68Write a line (or lines) of code to carry out the following:
Display “The stuff that dreams are made of.” in red letters in txtBox
txtBox.ForeColor = vbRed
txtBox.Text = “The stuff that dreams are made of.”
Display “Life is like a box of chocolates.” in Courier font in txtBox
txtBox.Text = “Life is like a box of chocolates.”
txtBox.Font.Name = “Courier”
16Exercises
Delete the content of txtBox
txtBox.Text = “”
Delete the content of lblTwo
lblTwo.Caption = “”
17Exercises
Make lblTwo disappear
lblTwo.Visible = False
Remove the border from lblTwo
lblTwo.BorderStyle = 0
Give picBox a blue background
picBox.BackColor = vbBlue
18Exercises
Place a bold red “Hello” in lblTwo
lblTwo.Font.Bold = True
lblTwo.ForeColor = vbRed
lblTwo.Text = “Hello”
19Exercises
Place a bold italic “Hello” in txtBox
txtBox.Font.Bold = True
txtBox.Font.Italic = True
txtBox.Caption = “Hello”
Make picBox disappear
picBox.Visible = False
20Exercises
Give the focus to cmdButtom
cmdButton.SetFocus
Remove the border from picBox
picBox.BorderStyle = 0
21Exercises
Place a border arround lblTwo and center its content
lblTwo.BorderStyle = 1
lblTwo.Alignment = 2
Give the focus to txtBoxTwo
txtBoxTwo.SetFocus
23Numeric Functions
Rnd Returns a number between 0 and 1.(excluding 1)
Sqr(n) Returns the square root of a number.
Round(n,r) The number n is rounded to r decimalplaces.
Int(n) Returns the largest integer less thanor equal to a number
24Numeric Functions
Examples
Sqr (9) is 3 Int(2.7) is 2 Round(2.7) is 3
Sqr (0) is 0 Int(3) is 3 Round(2.317,2) is 2.32
Sqr (2) is 1.4142 Int(-2.7) is -3 Round(2.317,1) is 2.3
Private Sub cmdEvaluate_Click() Dim n As Single, root As Single ' Evaluate functions at a variable picResults.Cls n = 6.76 root = Sqr(n) picResults.Print root; Int(n); Round(n, 1)
End Sub
25String Functions
Left (“String”, n) Returns a string containing a specified number of characters,
starting at the beginning of the “String”.
Right (“String”, n) Returns a string containing a specified number of
characters from the end of the “String”.
Mid (“String”, m, n) Returns a string containing a specified number of characters starting at the position indicated by the first number and continuing for the length specified by the second number
UCase (“String”) Converts any lowercase letters in string to uppercase
LCase (“String”) Converts any uppercase letters in string to lowercase
InStr (“Str1”, ”Str2“) Searches for the first occurrence of one string in another and gives the position at which the string is found
Len (“String”) Returns the number of characters in the string.
Trim (“ String ”) Returns “String” with leading and trailing spaces removed.
26String Functions
ExamplesLeft (“fanatic”, 3) is “fan” Right (“fanatic”, 3) is “tic” Mid (“fanatic”,5,1) is “t”
UCase(“Disk”) is “DISK” LCase(“Disk”) is “disk” Trim(“-12 “) is “-12”
27Format Function
The format functions provide detailed control of how numbers, dates, and strings are displayed.
Example:
FormatNumber (12345.678, 1) 12,345.6FormatCurrency (12345.678, 2) $12,345.68FormatPercent (.185, 2) 18.50%FormatNumber (1 + Sqr(2), 3) 2.414FormatDateTime(“9-15-99”, vbLongDate) Wednesday, September 15, 1999
Format(1234567890, “@@@@@@@@@@”) 1234567890Format(FormatNumber(1234.5), “@@@@@@@@@@”) 1,234.50
28Exercises
1. Given the data in the string variable y shown below, which of the following statements will assign the value ALL to the string variable x?
y = "WHEN ALL ELSE FAILS, READ THE DIRECTIONS“
(A) x = Mid(y, 6, 3)(B) x = Left(y, 3)(C) x = Middle(y, 6, 3)(D) x = Right(y, 8)
29Exercises
2. What will be displayed when the following program is executed?Dim a As String, b As String, c As Stringa = "THE WHOLE"b = "PART"c = Mid(a, Sqr(4), Len(b))picBox.Print c
(A) THE WHOLE PART(B) 6(C) HE W(D) HOLE(E) None of the above
30Exercises
3. What will be the output of the following statement?picBox.Print FormatCurrency(1234.567,2)
(A) $1234.567(B) 1,234.57(C) $1234.57(D) $1,234.57
31Exercises
4. What will be the output of the following lines?Dim alphabet As String, soup As Stringalphabet = "abcdefghijklmnopqrstuvwxyz"soup = UCase(alphabet)picBox.Print Left(alphabet, 5); Left(soup, 5)
(A) abcdeABCDE(B) ABCDEABCDE(C) eE(D) EE