Download presentation
Presentation is loading. Please wait.
Published byWilliam Ford Modified over 9 years ago
1
Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1
2
Form View 2
3
Combo Box in Form View 3
4
Create a blank Form 4 Form Design
5
Combo Box 5
6
Name the Combo Box 6
7
7
8
8
9
Private Sub ClientComboBox_Click() End Sub 9
10
Form in Design View 10
11
11
12
Add Button 12
13
13
14
14
15
15
16
Option Compare Database Option 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 16
17
Private Sub LoadClient() End Sub 17
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 18
19
On Error GoTo LoadError LoadError: MsgBox (Err.Number) MsgBox (Err.Description) End Sub 19
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").Value rs.Close db.Close Exit Sub 20
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 21
22
Private Sub ChangeCommand_Click() ChangeClient End Sub Private Sub ClearCommand_Click() ClearClient End Sub Private Sub ClientComboBox_Click() LoadClient End Sub Private Sub DeleteCommand_Click() DeleteClient End Sub Private Sub InsertRecord_Click() AddClient End Sub 22 Best Practice
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 String Set db = CurrentDb SQL statement and any other program code rs.Close db.Close 23
24
On Error GoTo AddError AddError: 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 24
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 25
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 & "';") 26
27
SQL: Deleting Records strDelete = "DELETE * FROM Client WHERE ClientID = '" _ & Me.ClientID.Value & "';" 27
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 28
29
Test Plan! GIGO 29
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.