13
© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem- Solving Approach Structured Query Language Also known as SQL Industry standard for accessing relational databases General format of SQL Select Select [Distinct] fieldlist From tablenames [Where search conditions] [Group By fieldlist] [Having group criterion] [Order By fieldlist [ASC|DESC]] Examples Select * From Contacts Select * From Contacts, ContactTypes Where Contacts.ContactTypeID = ContactTypes.ContactTypeID Order By LastName

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Embed Size (px)

Citation preview

Page 1: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Structured Query Language Also known as SQL

Industry standard for accessing relational databases

General format of SQL SelectSelect [Distinct] fieldlist From tablenames

[Where search conditions][Group By fieldlist][Having group criterion][Order By fieldlist [ASC|DESC]]

ExamplesSelect * From ContactsSelect * From Contacts, ContactTypes Where Contacts.ContactTypeID = ContactTypes.ContactTypeID Order By LastName

Page 2: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Creating a new Dynaset Steps to define new dynaset via data control

1) Set data control’s RecordSource property to SQL query

2) Re-open recordset using Refresh method

ExamplestSQL = “Select * From Contacts, ContactTypes Where “ _

& “Contacts.ContactTypeID = ContactTypes.ContactTypeID ”

datBooks.RecordSource = stSQL

datBooks.Refresh

Page 3: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Database Objects Data Access Objects (DAO)

Uses Jet DB engine to access databases Remote Data Objects (RDO)

Used when developing client/server application that access remote ODBC database system

Requires Enterprise edition of VB & 32-bit OS ActiveX Data Objects (ADO)

New model with VB 6.0 Combines best features of DAO and ADO Not limited to relational DBs Requires OLE database driver to use with Jet DB

Page 4: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

DAO Model

E rro rs

G ro u p s U sers

R eco rd sets Q u ery D efs T a b leD efs R ela tio n s C o n ta in ers

D a ta b a ses

W o rk sp a ces

D B E n g in e

Page 5: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Data Control vs. DAO NotationPurpose of notation Notation with data control Notation with DAO

Move to first record CalldatEmployee.Recordset.MoveFirst

CallfEmpRS.MoveFirst

Move to last record CalldatEmployee.Recordset.MoveLast

CallfEmpRS.MoveLast

Move to nextrecord

CalldatEmployee.Recordset.MoveNext

CallfEmpRS.MoveNext

Move to previousrecord

CalldatEmployee.Recordset.MovePrevious

CallfEmpRS.MovePrevious

Condition to checkif before the start ofthe Recordset

datEmployee.Recordset.BOF FEmpRS.BOF

Condition to checkif after the end ofthe Recordset

datEmployee.Recordset.EOF fEmpRS.EOF

Retrieve contents ofa specific field

datName.Recordset!FieldName ordatName.Recordset("FieldName") ordatName.Recordset![FieldName]

fEmpRS!FieldName orfEmpRS("FieldName") orfEmpRS![FieldName]

Page 6: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Options with SQL Select Like operator

Relational operator used to find similar matches Expression being compared is surrounded by

single quotes and normally contains * wildcard FirstName Like ‘A*’

– returns all records whose FirstName values start with A

Aggregate Queries Used to get summary-type information Define new field to hold the result from an

aggregate function Inner Joins

Page 7: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Aggregate QueriesSELECT AggFunction(fieldname) As newdbfield

FROM tablename [WHERE searchcriteria]

Function PurposeAVG Returns the average of the values in the selected fieldCOUNT Returns the number of records in the selected fields.SUM Returns the sum of all the values in the selected field.MAX Returns the highest value in the selected field.MIN Returns the smallest value in the selected field.

Page 8: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Inner Joins for Multiple TablesSELECT [DISTINCT] fieldlist

FROM table1 INNER JOIN table2 ON table1.field1 = table2.field2

[WHERE searchcriteria][ORDER BY fieldname]

ExampleSelect * From Contacts INNER JOIN ContactTypes On

Contacts.ContactTypeID = ContactTypes.ContactTypeID Order By LastName

Page 9: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Modifying DB Records Define action query

INSERT INTO UPDATE DELETE

Use Execute method Call DBObject.Execute(stSQL)

Page 10: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Example: Add a new recordPrivate Sub WriteToDB()

Dim SQL As String

SQL = "INSERT INTO Employee "

SQL = SQL & "(EmpName, BirthDate, PayRate, Manager)"

SQL = SQL & " VALUES ("

SQL = SQL & DBTextFmt(txtEmpName.Text) & ", "

SQL = SQL & CStr(DBDateFmt(txtBirthdate)) & ", "

SQL = SQL & txtPayRate.Text & ", "

SQL = SQL & txtMgr.Text & ")"

Call fEmpDB.Execute(SQL)

' Enable navigation buttons

cmdFirst.Enabled = True

cmdLast.Enabled = True

cmdNext.Enabled = True

cmdPrevious.Enabled = True

End Sub

Page 11: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Example: Delete a recordPrivate Sub cmdDelete_Click()

Dim SQL As String

SQL = "DELETE FROM Employee Where EmpID = " & _

fEmpRS("EmpID")

Call fEmpDB.Execute(SQL)

Call fEmpRS.Close

Call fEmpDB.Close

Call ReOpenDB

End Sub

Page 12: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Example: Create New DatabaseOption Explicit

Private fUserWS As Workspace

Private fNewDB As Database

Private Sub CreateDB()

Dim FileName As String

Set fUserWS = DBEngine.Workspace(0)

FileName = AppendFileToPath(App.Path, “NewTestDB.mdb”)

If FileExists(FileName) Then

If MsgBox(“The database exists. Do you want to replace it?”, _

vbYesNo + vbQuestion) = vbYes Then

Kill FileName

Else

Exit Sub ' Do not create the DB-already exists

End If

End If

Set fNewDB = fUserWS.CreateDatabase(FileName, dbLangGeneral)

End Sub

Page 13: © 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

© 1999, by Que Education and Training, Chapter 11, pages 593-625 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Example: Create Database Table‘ Create table

SQL = "CREATE TABLE Employee (" & _

"EmpID COUNTER, " & _

"EmpName TEXT(30), " & _

"BirthDate DATETIME, " & _

"PayRate CURRENCY, " & _

"Manager BIT)"

Call fNewDB.Execute(SQL)

‘ Create index to improve performance

SQL = "CREATE UNIQUE INDEX indEmpPrimary"

SQL = SQL & " ON Employee (EmpID)"

SQL = SQL & " WITH PRIMARY"

Call fDBPaychecks.Execute(SQL)