1 All Powder Board and Ski Microsoft Access Workbook Chapter 7: Integrity and Transactions Jerry Post Copyright © 2003.

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.
All Powder Board and Ski Microsoft Access Workbook Chapter 3: Database Tables Jerry Post Copyright © 2007.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 7: Integrity and Transactions Jerry Post Copyright © 2007.
MS-Access XP Lesson 1. Introduction to MS-Access Database Management System Software (DBMS) Store data in databases Database is a collection of table.
Chapter 6: The Repetition Structure
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Systems Development Group Project Programming Access Forms.
Systems Development Group Project Programming Access Forms.
Event-Driven Programming and Access Events
Chapter Extension 6 Using Microsoft Access © 2008 Pearson Prentice Hall, Experiencing MIS, David Kroenke.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Muffin Shop - if, calculations etc. (muffins, muffins2) Please use speaker notes for additional information!
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
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.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
DATABASE 1 Base Data Types  Numbers  Integers  Reals  Text  Length  International  Date/Time  Images  Bitmap  Vector  Sound  Samples  MIDI.
Jerry Post Copyright © Database Management Systems Chapter 8 Applications.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 4: Queries Jerry Post Copyright © 2007.
All Powder Board and Ski SQL Server 2000 Workbook Chapter 1 Jerry Post Copyright © 2003.
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 / 23 © 2006, Universal Tax Systems, Inc. All Rights Reserved. Working with Tax Returns Objectives –In this chapter you will learn how to: Get Started.
Visual Basic ADO Programming 56:150 Information System Design.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 10: Distributed Databases Jerry Post Copyright © 2003.
1 Chapter 12 – Web Applications 12.1 Programming for the Web, Part I 12.2 Programming for the Web, Part II 12.3 Using Databases in Web Programs.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
All Powder Board and Ski Microsoft Access Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.
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.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
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.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
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.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2003.
MIS333k(Poynor) Chapter 2. Intro to VBA Events Where to plug in your programs?
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
Source = Table rsObject.Open tablename, Connection Object, CursorType, LockType, adCmdTable Source = Stored Procedure rsObject.Open stored procedure name,
Form and Graphical User Interfaces. Lesson plan More about queries More about entering data into a table Form.
1 All Powder Board and Ski SQL Server Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2003.
 2 Data Object Library approaches ◦ DAO (Data Access Objects)  Original access strategy (up to VB6)  Closely linked to MS Access ◦ ADO (ActiveX Data.
Copyright © 2014 Pearson Canada Inc. Ext. 5b-1 Copyright © 2014 Pearson Canada Inc. Application Extension 5b Using Microsoft Access Part 2: Using Information.
Chapter 1: Introduction All Powder Board and Ski
Database Management Systems
ADO VBA Programming in Access
Chapter 6: Forms, Reports and Applications All Powder Board and Ski
Find, filter etc with connection to Access code internally
Database Management Systems
Chapter 7: Database Integrity and Transactions
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
Click on Order Tab..
Presentation transcript:

1 All Powder Board and Ski Microsoft Access Workbook Chapter 7: Integrity and Transactions Jerry Post Copyright © 2003

2 Compute Sales Tax Sales Tax From Figure 6.17

3 Create Access Code Module New Module Visual Basic Editor Application code

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

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

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

7 Form Events 1) Open2) Load3) Resize4) Activate5) Current b a d 6) Enter b a d 7) GotFocus b a d 5) Close4) Deactivate3) Unload2) LostFocus b a d 1) Exit b a d ControlsForms Change rows Close a Form Open a Form

8 Main Control Events Enter GotFocus Change: keystrokes Exit BeforeUpdate AfterUpdate time controlevent 131 LostFocus

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

10 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

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

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

13 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 End If End Sub

14 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 End Sub

15 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

16 Transactions for Discounts New table

17 Rental Form Button to open discount form

18 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

19 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

20 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 & "')"

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

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

23 Form to Compute Average Increase

24 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

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

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

27 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

28 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

29 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

30 Lock Test Form Combo box to select customer

31 CustomerLock Test Form

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

33 Pessimistic Lock on Forms Change the ZIP but do not leave the cell 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

34 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