unbound form form not tied directly to any fields in the database must use sql to “bind” the...
TRANSCRIPT
![Page 1: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/1.jpg)
1
Unbound Form
• Form not tied directly to any fields in the database
• Must use SQL to “bind” the fields
![Page 2: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/2.jpg)
2
Form View
![Page 3: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/3.jpg)
3
Combo Box in Form View
![Page 4: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/4.jpg)
4
Create a blank Form
Form Design
![Page 5: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/5.jpg)
5
Combo Box
![Page 6: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/6.jpg)
6
Name the Combo Box
![Page 7: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/7.jpg)
7
![Page 8: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/8.jpg)
8
![Page 9: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/9.jpg)
9
Private Sub ClientComboBox_Click()
End Sub
![Page 10: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/10.jpg)
10
Form in Design View
![Page 11: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/11.jpg)
11
![Page 12: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/12.jpg)
12
Add Button
![Page 13: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/13.jpg)
13
![Page 14: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/14.jpg)
14
![Page 15: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/15.jpg)
15
![Page 16: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/16.jpg)
16
Option Compare DatabaseOption Explicit
• Option Compare Database– Declares that string comparisons are not case
sensitive• Option Explicit– Used to require that all variables be declared
before they are used
Best practice
![Page 17: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/17.jpg)
17
Private Sub LoadClient()
End Sub
![Page 18: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/18.jpg)
18
Dim db As DAO.Database• An object library used to work with Access databases
Dim rs As DAO.Recordset• Declaring a variable named rs that stores the record set
in memory• The record set contains the records you extracted by
running a query or using a Access table
Dim strQuery As String• Creates an area in memory to store information
– This variable will be used to store the query text you create
Set db = CurrentDb• Specifies what database to use in the queries
– In this case it is the current database
![Page 19: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/19.jpg)
19
On Error GoTo LoadError
LoadError: MsgBox (Err.Number) MsgBox (Err.Description)End Sub
![Page 20: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/20.jpg)
20
strQuery = "SELECT * FROM Client WHERE ClientID = '" & Me.ClientComboBox.Value & "';"Set rs = db.OpenRecordset(strQuery, dbOpenSnapshot) Me.ClientID = rs.Fields("ClientID").Value Me.FirstName = rs.Fields("FirstName").Value Me.LastName = rs.Fields("LastName").Value Me.Address = rs.Fields("Address").Value Me.City = rs.Fields("City").Value Me.State = rs.Fields("State").Value Me.ZipCode = rs.Fields("Zipcode").Value Me.HomePhone = rs.Fields("HomePhone").Valuers.Closedb.CloseExit Sub
![Page 21: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/21.jpg)
21
Other Procedures
• Add records• Change records• Delete records• Clear all the controls on the form so you can
add a record
• These will all be attached to the appropriate command button and will have an [Event Procedure] in the On click property
![Page 22: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/22.jpg)
22
Private Sub ChangeCommand_Click() ChangeClientEnd Sub
Private Sub ClearCommand_Click() ClearClientEnd Sub
Private Sub ClientComboBox_Click() LoadClientEnd Sub
Private Sub DeleteCommand_Click() DeleteClientEnd Sub
Private Sub InsertRecord_Click() AddClientEnd Sub
Best Practice
![Page 23: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/23.jpg)
23
All Procedures accessing the Database will have the following Code
Dim db As DAO.Database Set db = CurrentDb Dim rs As DAO.Recordset Dim strQuery As StringSet db = CurrentDb
SQL statement and any other program code rs.Close db.Close
![Page 24: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/24.jpg)
24
On Error GoTo AddErrorAddError: Select Case Err.Number Case 3464 MsgBox ("Client ID Cannot be Blank") Case 3315 MsgBox ("Client ID Cannot be Blank") Case 3022 MsgBox ("Client ID Must be Unique") Case Else MsgBox (Err.Number) MsgBox (Err.Description) End Select
![Page 25: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/25.jpg)
25
Private Sub AddClient() Dim db As DAO.Database Set db = CurrentDb Dim strInsert As String On Error GoTo AddError strInsert = "INSERT INTO Client(ClientID, FirstName, LastName,Address, State, City, ZipCode, HomePhone) " & _ " VALUES('" & Me.ClientID & "','" & Me.FirstName & "','" & Me.LastName & _ "','" & Me.Address & "','" & Me.City & "','" & Me.State & _ "','" & Me.ZipCode & "','" & Me.HomePhone & "');"
CurrentDb.Execute strInsert, dbFailOnError Me.ClientComboBox.Value = Me.ClientID.Value Me.ClientComboBox.Requery
![Page 26: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/26.jpg)
26
SQL: Changing (Updating) Records
strUpdate = ("UPDATE Client SET " & _ "ClientID = '" & Me.ClientID & _ "', FirstName ='" & Me.FirstName & _ "', LastName ='" & Me.LastName & _ "', Address ='" & Me.Address & _ "', City ='" & Me.City & _ "', State ='" & Me.State & _ "', ZipCode ='" & Me.ZipCode & _ "', HomePhone ='" & Me.HomePhone & _ "' WHERE ClientID = '" & Me.ClientComboBox.Value & "';")
![Page 27: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/27.jpg)
27
SQL: Deleting Records
strDelete = "DELETE * FROM Client WHERE ClientID = '" _ & Me.ClientID.Value & "';"
![Page 28: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/28.jpg)
28
Execute this Procedure before Adding a Record
Private Sub ClearClient() Me.ClientComboBox = "" Me.ClientID = "" Me.FirstName = "" Me.LastName = "" Me.Address = "" Me.City = "" Me.State = "" Me.ZipCode = "" Me.HomePhone = ""End Sub
![Page 29: Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bff61a28abf838cbe58c/html5/thumbnails/29.jpg)
29
Test Plan! GIGO