Download presentation
Presentation is loading. Please wait.
Published byShanon Sutton Modified over 9 years ago
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 Structured Query Language n Also known as SQL i Industry standard for accessing relational databases n General format of SQL Select Select [Distinct] fieldlist From tablenames [Where search conditions] [Group By fieldlist] [Having group criterion] [Order By fieldlist [ASC|DESC]] n Examples Select * From Contacts Select * From Contacts, ContactTypes Where Contacts.ContactTypeID = ContactTypes.ContactTypeID Order By LastName
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 Creating a new Dynaset n 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 n Example stSQL = “Select * From Contacts, ContactTypes Where “ _ & “Contacts.ContactTypeID = ContactTypes.ContactTypeID ” datBooks.RecordSource = stSQL datBooks.Refresh
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 Database Objects n Data Access Objects (DAO) i Uses Jet DB engine to access databases n Remote Data Objects (RDO) i Used when developing client/server application that access remote ODBC database system i Requires Enterprise edition of VB & 32-bit OS n ActiveX Data Objects (ADO) i New model with VB 6.0 i Combines best features of DAO and ADO i Not limited to relational DBs i Requires OLE database driver to use with Jet DB
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 DAO Model
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 Data Control vs. DAO Notation
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 Options with SQL Select n Like operator i Relational operator used to find similar matches i Expression being compared is surrounded by single quotes and normally contains * wildcard u FirstName Like ‘A*’ –returns all records whose FirstName values start with A n Aggregate Queries i Used to get summary-type information i Define new field to hold the result from an aggregate function n Inner Joins
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 Aggregate Queries SELECT AggFunction(fieldname) As newdbfield FROM tablename [WHERE searchcriteria]
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 Inner Joins for Multiple Tables SELECT [DISTINCT] fieldlist FROM table1 INNER JOIN table2 ON table1.field1 = table2.field2 [WHERE searchcriteria] [ORDER BY fieldname] n Example Select * From Contacts INNER JOIN ContactTypes On Contacts.ContactTypeID = ContactTypes.ContactTypeID Order By LastName
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 Modifying DB Records n Define action query i INSERT INTO i UPDATE i DELETE n Use Execute method i Call DBObject.Execute(stSQL)
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 Example: Add a new record Private 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
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 Example: Delete a record Private 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
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 Example: Create New Database Option 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 Set fNewDB = fUserWS.CreateDatabase(FileName, dbLangGeneral) End Sub
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 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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.