Chapter 7: Database Integrity and Transactions

Slides:



Advertisements
Similar presentations
All Powder Board and Ski
Advertisements

1 All Powder Board and Ski Microsoft Access Workbook Chapter 9: Data Warehouses and Data Mining Jerry Post Copyright © 2007.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2007.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 6: Forms and Reports Jerry Post Copyright © 2007.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 7: Integrity and Transactions Jerry Post Copyright © 2007.
Chapter 6: The Repetition Structure
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Creating Custom Forms. 2 Design and create a custom form You can create a custom form by modifying an existing form or creating a new form. Either way,
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
Systems Development Group Project Programming Access Forms.
Concepts of Database Management Sixth Edition
ADO Recordsets. Recordset Objects Similar to Tables and Queries: data Using VBA/VBScript you… –Open a recordset, –Locate a record –Update or add a record.
Private Sub Close_Click() On Error GoTo Err_Close_Click DoCmd.Close Exit_Close_Click: Exit Sub Err_Close_Click: MsgBox Err.Description Resume Exit_Close_Click.
Advanced Forms Lesson 10.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
Concepts of Database Management Seventh Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
1 All Powder Board and Ski Oracle 9i Workbook Chapter 7: Integrity and Transactions Jerry Post Copyright © 2003.
1 All Powder Board and Ski SQL Server Workbook Chapter 2: Database Design Jerry Post Copyright © 2004.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 7: Integrity and Transactions Jerry Post Copyright © 2003.
Visual Basic ADO Programming 56:150 Information System Design.
Concepts of Database Management Seventh Edition
Copyright 2008 McGraw-Hill Ryerson 1 TECHNOLOGY PLUG-IN T7 PROBLEM SOLVING USING ACCESS.
XP Chapter 4 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Collecting Data for Well-Designed Forms Chapter 4 “Making.
Course ILT Forms and queries Unit objectives Create forms by using AutoForm and the Form Wizard, and add or modify form headers and footers Open and enter.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 6: Forms, Reports, and Applications Jerry Post Copyright © 2003.
1 MIS309 Database Systems Introduction to Microsoft Access.
1 All Powder Board and Ski Oracle 9i Workbook Chapter 8: Data Warehouses and Data Mining Jerry Post Copyright © 2003.
All Powder Board and Ski Microsoft Access Workbook Chapter 2: Database Design Jerry Post Copyright © 2003.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 8: Data Warehouses and Data Mining Jerry Post Copyright © 2003.
VB for applications. Lesson Plan Fundamentals of VB VB for handling events in Access.
Microsoft Office 2013: In Practice Chapter 2 Using Design View, Data Validation, and Relationships Copyright © 2014 by The McGraw-Hill Companies, Inc.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
T7-1 LEARNING OUTCOMES – ACCESS PROBLEM SOLVING 1.Describe the process of using the Simple Query Wizard using Access 2.Describe the process of using the.
VAT Calculator program Controls Properties Code Results.
Unbound Form Form not tied directly to any fields in the database Must use SQL to “bind” the fields 1.
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith Mast, and Mary Anne.
DAY 18: ACCESS CHAPTER 3 Tazin Afrin October 22,
Source = Table rsObject.Open tablename, Connection Object, CursorType, LockType, adCmdTable Source = Stored Procedure rsObject.Open stored procedure name,
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 6 1 Microsoft Office Access 2003 Tutorial 6 – Creating Custom Forms.
1 All Powder Board and Ski SQL Server Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2003.
Visual Basic Fundamental Concepts
Access Tutorial 2 Building a Database and Defining Table Relationships
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Using Macros, Switchboards, PivotTables, and PivotCharts
ADO VBA Programming in Access
Chapter 6: Forms, Reports and Applications All Powder Board and Ski
ActiveX Data Objects (ADO)
Plug-In T7: Problem Solving Using Access 2007
Chapter 2: Database Design All Powder Board and Ski
All Powder Board and Ski
All Powder Board and Ski
Access Project 8 Using Visual Basic for Applications (VBA) and Creating Multi-Page Forms.
DB Integrity & Transactions Part 2
Chapter 7 Advanced Form Techniques
Microsoft Office Access 2003
Visual Basic..
MODULE 7 Microsoft Access 2010
Microsoft Office Access 2003
Exploring Microsoft Access 2003
Microsoft Official Academic Course, Access 2016
Access Tutorial 2 Building a Database and Defining Table Relationships
Access Tutorial 2 Building a Database and Defining Table Relationships
Guidelines for Microsoft® Office 2013
Presentation transcript:

Designing & Building Business Applications Microsoft® Access Jerry Post Chapter 7: Database Integrity and Transactions All Powder Board and Ski

Compute Sales Tax Sales Tax From Figure 6.17

Action Action Create a new module At the top add: Option Explicit Add the function ComputeSalesTax Use Debug/Compile to find errors Close the module

Create Access Code Module Visual Basic Editor New Module Application code

Action Action Edit the Sale form in Design view Right click the SalesTax box and select the Build Code option Select the Enter event Add the line: SalesTax = ComputeSalesTax(Subtotal) Run the form Click on the SalesTax box to test the calculation

Add Event Code to the Sales Form Choose the Enter event Call the new function Right click and Build Code

Debugging Use F8 to step through the code Set a break point Roll the cursor over a variable to see its current value

Action Action Open the SaleItem subform in Design view Open the form properties and modify the Record Source query to add the QuantityOnHand column View the Field List box Drag QuantityOnHand onto the form Run the form and improve the layout

Adding QuantityOnHand to Subform Build query to add QuantityOnHand Right click to set properties Drag QOH from Field List box onto form

Form Events Open a Form Forms Controls Close a Form 1) Open 2) Load 3) Resize 4) Activate 5) Current 6) Enter 7) GotFocus 12 a 12 a 12 a 33 33 33 d d d b b b Forms Controls Close a Form Change rows 5) Close 4) Deactivate 3) Unload 2) LostFocus 1) Exit 12 a 12 a 33 33 d d b b

Main Control Events control event 32 Enter 32 GotFocus Change: keystrokes 131 time Exit 131 LostFocus 131 BeforeUpdate 131 AfterUpdate 131

Action Action Edit the SaleItem subform in Design view Right click the QOH box and build code Select the AfterUpdate event Add code to refresh the row and subtract QuantitySold from QuantityOnHand Test the form

First Attempt: QOH Code Private Sub QuantitySold_AfterUpdate() Me.Refresh ‘ Save data for new rows QuantityOnHand = QuantityOnHand - QuantitySold End Sub

Problems What if the clerk entered the wrong value and should have entered 1 instead of 2 units? Test it, and the code subtracts 1 from the QOH, leaving 7. You need to add the original 2 units back. QuantityOnHand = QuantityOnHand – QuantitySold + OldQuantity

Clerk Changes SKU Subtract Inventory SKU QOH Enter SKU 950049, Quantity 2 950049 10 950050 10 8 10 8 Change SKU to 950050 Add back to 950049 Subtract for 950050

Clerk Changes SKU: Events Clerk enters original SKU (950049) and Quantity Code subtracts QuantitySold from QuantityOnHand (10 - 2 = 8) Clerk changes SKU to new value (950050) Active row becomes set for new value Need to subtract QuantitySold from QuantityOnHand for 950050 Need to add QuantitySold back into QuantityOnHand for 950049 Private Sub cboSKU_Enter() If IsNull(cboSKU.Value) Then OldSKU = "-1" Else OldSKU = cboSKU.Value End If End Sub

Action Action Open the SaleItem subform in Design view Choose View/Code Add and modify the code as specified to handle changes in quantity Test the form

SKU Changes: Code Private Sub cboSKU_Enter() If IsNull(cboSKU.Value) Then OldSKU = "-1" Else OldSKU = cboSKU.Value End If End Sub Private Sub cboSKU_AfterUpdate() If OldSKU <> "-1" And Not IsNull(QuantitySold) Then QuantityOnHand = QuantityOnHand - QuantitySold Dim sql As String sql = "UPDATE Inventory SET QuantityOnHand = QuantityOnHand + " _ & QuantitySold & " WHERE SKU='" & OldSKU & "'" Dim cmd As ADODB.Command Set cmd = CreateObject("ADODB.Command") cmd.ActiveConnection = CurrentProject.Connection cmd.CommandText = sql cmd.Execute

Delete Code Dim DelQuantity() As String Dim DelSKU() As String Dim nDel As Integer Private Sub Form_Delete(Cancel As Integer) If IsNull(cboSKU.Value) Or IsNull(QuantitySold) Then Exit Sub If (nDel = 0) Then ReDim DelQuantity(Me.CurrentView) ReDim DelSKU(Me.CurrentView) End If DelSKU(nDel) = cboSKU.Value DelQuantity(nDel) = QuantitySold nDel = nDel + 1 End Sub Private Sub Form_AfterDelConfirm(Status As Integer) Dim i As Integer For i = 0 To nDel - 1 DeleteOneRow DelSKU(i), DelQuantity(i) Next i nDel = 0

Delete Code 2 Private Sub DeleteOneRow(ByVal SKU As String, ByVal Qty As String) Dim sql As String sql = "UPDATE Inventory SET QuantityOnHand = QuantityOnHand + " _ & Qty & " WHERE SKU='" & SKU & "'" Dim cmd As ADODB.Command Set cmd = CreateObject("ADODB.Command") cmd.ActiveConnection = CurrentProject.Connection cmd.CommandText = sql cmd.Execute End Sub

Action Action Open the SaleItem subform in Design view Add the oldSKU variable Add the SKU event code as described Place a breakpoint in the code before the Execute line Test the form by changing an SKU number Examine the sql line before it runs Add the delete code as shown Test multiple changes to validate the code

Transactions for Discounts New table

Rental Form Button to open discount form

Action Action Create the Rental Discount form in Design view Add the text boxes and button Save the form Open the Rental form in Design view Add a button to open the Discount form Modify the open code as indicated Run the form and test the button

Rental Discount Form RentID and Amount are filled in by code on the Rental form Date default value is set to =Now() This is an unbound form built from design view with no Record Source

Action Action Open the Rental Discount form in Design view Add the specified code to the button click event Test the forms

Rental Form Code: Discount Button Private Sub cmdDiscount_Click() On Error GoTo Err_cmdDiscount_Click Dim stDocName As String Dim stLinkCriteria As String stDocName = "GiveRentDiscount" DoCmd.OpenForm stDocName, , , stLinkCriteria Forms!GiveRentDiscount!txtRentID = RentID Forms!GiveRentDiscount!txtDiscountAmount = SubTotalCharges Exit_cmdDiscount_Click: Exit Sub Err_cmdDiscount_Click: MsgBox Err.Description Resume Exit_cmdDiscount_Click End Sub

Discount Code Private Sub cmdDiscount_Click() Dim cmd As ADODB.Command Dim SQL1 As String, SQL2 As String Set cmd = CreateObject("ADODB.Command") cmd.ActiveConnection = CurrentProject.Connection SQL1 = "UPDATE RentItem SET RepairCharges=0 WHERE RentID=" & txtRentID SQL2 = "INSERT INTO RentalDiscount (RentID, DiscountDate, DiscountAmount, Reason)" & _ " VALUES (" & txtRentID & _ ", #" & txtDiscountDate & "#" & _ ", " & txtDiscountAmount & _ ", '" & txtReason & "')"

Discount Code Continued On Error GoTo Err_DiscountTrans cmd.ActiveConnection.BeginTrans cmd.CommandText = SQL1 cmd.Execute cmd.CommandText = SQL2 cmd.ActiveConnection.CommitTrans lblMessage.Caption = "Changes recorded." Exit1: Exit Sub Err_DiscountTrans: cmd.ActiveConnection.RollbackTrans lblMessage.Caption = Err.Description Resume Exit1 End Sub

Action Action Create a new query in Design view Tables: Sale and SaleItem Create column SaleWeek: Val(Format([SaleDate],”ww”)) Create column Value: [QuantitySold]*[SalePrice]) Sum the Value column

Query for Cursor: Weekly Sales Val(Format([SaleDate],”ww”))

Action Action Create a new form in Design view Add a button Add a text box named txtAverage Add the specified code to the button Run the form and test the code Place a breakpoint and step through the code as it runs

Form to Compute Average Increase

Code to Compute Average Increase Dim rst As ADODB.Recordset Set rst = CreateObject("ADODB.Recordset") Dim SQL As String SQL = "SELECT SaleWeek, [Value] FROM qryWeeklySales" rst.Open SQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly Dim avg1 As Double, n As Integer Dim prior As Currency prior = -1 Do Until rst.EOF If (prior > 0) Then avg1 = avg1 + (rst("Value") - prior) / prior n = n + 1 End If prior = rst("Value") rst.MoveNext Loop rst.Close Me.txtAverage = avg1 / n Open the SQL statement or table Skip the first week because there is no prior value Compute the percent change and keep a running total Save the current value and move to the next row

Keys: Create Sales and Items (barcode) Customer ID card is scanned Create new sale Get SaleID Scan an item Save sale item, update QOH and totals Save SaleID, SKU, Quantity Repeat until done (payment key)

Action Action Create a new form in Design view Add boxes for CustomerID, EmployeeID, SKU, and txtSaleID as the generated key Create a command button and add the indicated code Test the form Place a breakpoint at the top of the code and step through the code

Generate Sale Form IDs and SKU would be scanned, but to test code, set default values

Generate Sale Code-1 Dim sqlSale As String, sqlItem As String, sqlSaleItem As String Dim rstSale As ADODB.Recordset, rstModel As ADODB.Recordset Dim rstSaleItem As ADODB.Recordset Set rstSale = CreateObject("ADODB.Recordset") Set rstModel = CreateObject("ADODB.Recordset") Set rstSaleItem = CreateObject("ADODB.Recordset") sqlSale = "SELECT SaleID, CustomerID, EmployeeID, SaleDate FROM Sale" sqlItem = "SELECT ModelID, ListPrice FROM Inventory INNER JOIN “ & _ “ItemModel ON Inventory.ModelID = ItemModel.ModelID “ & _ “WHERE SKU='" & SKU & "'" sqlSaleItem = "SELECT SaleID, SKU, SalePrice, QuantitySold FROM SaleItem" Dim cnn As ADODB.Connection Set cnn = CurrentProject.Connection

Generate Sale Code-2 ' Get the List Price for the SKU rstModel.Open sqlmodel, cnn, adOpenStatic, adLockReadOnly Dim ListPrice As Currency ListPrice = rstModel("ListPrice") rstModel.Close ' Open the Sale table and create a new sale rstSale.Open sqlSale, cnn, adOpenDynamic, adLockOptimistic Dim SaleID As Long rstSale.AddNew rstSale("SaleDate") = Now rstSale("CustomerID") = CustomerID rstSale("EmployeeID") = EmployeeID SaleID = rstSale("SaleID") rstSale.Update rstSale.Close

Generate Sale Code-3 ' Add the SKU to the SaleItem table using the new SaleID rstSaleItem.Open sqlSaleItem, cnn, adOpenDynamic, adLockOptimistic rstSaleItem("SaleID") = SaleID rstSaleItem("SKU") = SKU rstSaleItem("SalePrice") = ListPrice rstSaleItem("QuantitySold") = 1 rstSaleItem.Update rstSaleItem.Close txtSaleID = SaleID

Action Action Create a new form in Design view Add a combo box to select customers Add a text box to enter a new ZIP Code Create a button and add the indicated code for it Test the form

Lock Test Form Combo box to select customer

Action Action Create a CustomerLocks form as a datasheet into the Customer table Run the form Edit a ZIP Code but do not leave the cell Switch to the new form and run it for the same CustomerID Return to the table and press the Tab key

CustomerLock Test Form

Optimistic Lock on the Form Select the first customer, enter a ZIP code and change it Change the ZIP but do not leave the cell Switch back here and Tab out of the cell Error message that value was changed

Action Action Switch to design view on the change form and add the specified error-handling code Open the CustomerLocks form in Design view and set the Record Locks property to Edited Record Run both forms Change a ZIP Code in CustomerLocks Enter a new ZIP Code in the change form for the same customer

Pessimistic Lock on Forms Select the first customer, enter a ZIP code and change it The change is not made and the error is trapped because the row is locked Change the ZIP but do not leave the cell

Handle Optimistic Locks in Code RetryUpdate: rst.Open SQL, cnn, adOpenDynamic, adLockOptimistic rst("ZIP") = NewZIPCode rst.Update rst.Close Exit_cmdNewZipCode_Click: Exit Sub Err_cmdNewZipCode_Click: If (MsgBox(Err.Description, vbRetryCancel) = vbCancel) Then Resume Exit_cmdNewZipCode_Click End If rst.Cancel rst.Requery Resume RetryUpdate

Designing & Building Business Applications Microsoft® Access Jerry Post End of Chapter 7