home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / source / chap35 / main.bas < prev    next >
Encoding:
BASIC Source File  |  1995-09-10  |  5.0 KB  |  155 lines

  1. Attribute VB_Name = "MAIN"
  2. ' Allow the user to set the database location
  3. Global Database_name
  4. Global CustDB As Database
  5.  
  6. ' Placeholder for the home directory of this application
  7. Global App_location As String
  8.  
  9. ' Snapshot for Custmain table
  10. Global CustmainSnapshot As Snapshot
  11. Global CustmainDynaset As Dynaset
  12.  
  13. ' Customer number is the key to the database
  14. Global Customer_number
  15.  
  16. ' Backup customer_number to restore in case user
  17. ' cancels an event which might change it.
  18. Global tmpCustNum
  19.  
  20. ' Customer balance
  21. Global CurrentBalance As Double
  22.  
  23. ' Indicator to prevent activity on a just deleted customer
  24. Global just_deleted
  25.  
  26.  
  27.  
  28.  
  29. Sub FillOrderTicketTable(ByVal Product As String)
  30.  
  31.     Dim OrderTicketTbl As Table
  32.     Dim CustBillAddrTbl As Table
  33.     Dim CustomerTbl As Table
  34.     Dim CustProdTbl As Table
  35.  
  36.     Set OrderTicketTbl = CustDB.OpenTable("OrderTicket")
  37.     Set CustomerTbl = CustDB.OpenTable("CustMain")
  38.  
  39.     ' Find the customer record
  40.     CustomerTbl.Index = "PrimaryKey"
  41.     CustomerTbl.Seek "=", Customer_number
  42.     If (CustomerTbl.NoMatch = True) Then
  43.         MsgBox ("The customer you have selected is not in the database.")
  44.         Exit Sub
  45.     End If
  46.     
  47.     OrderTicketTbl.AddNew
  48.  
  49.     OrderTicketTbl.Fields("CustomerNum") = Customer_number
  50.     OrderTicketTbl.Fields("InvoiceDate") = Date$
  51.     OrderTicketTbl.Fields("First_Name") = CustomerTbl.Fields("First_Name")
  52.     OrderTicketTbl.Fields("Last_Name") = CustomerTbl.Fields("Last_Name")
  53.     OrderTicketTbl.Fields("Company") = CustomerTbl.Fields("Company")
  54.     OrderTicketTbl.Fields("Address") = CustomerTbl.Fields("Address")
  55.     OrderTicketTbl.Fields("Suite_Apt") = CustomerTbl.Fields("Suite_Apt")
  56.     OrderTicketTbl.Fields("PO_Box") = CustomerTbl.Fields("PO_Box")
  57.     OrderTicketTbl.Fields("City") = CustomerTbl.Fields("City")
  58.     OrderTicketTbl.Fields("State") = CustomerTbl.Fields("State")
  59.     OrderTicketTbl.Fields("Zip") = CustomerTbl.Fields("Zip")
  60.  
  61.     ' Find the billing record
  62.     Set CustBillAddrTbl = CustDB.OpenTable("CUSTBILLADDR")
  63.     CustBillAddrTbl.Index = "PrimaryKey"
  64.     CustBillAddrTbl.Seek "=", Customer_number
  65.     If (CustBillAddrTbl.NoMatch = False) Then
  66.         OrderTicketTbl.Fields("Billing_Name") = CustBillAddrTbl.Fields("Billing_Name")
  67.         OrderTicketTbl.Fields("Billing_Address") = CustBillAddrTbl.Fields("Billing_Address")
  68.         OrderTicketTbl.Fields("Billing_Apt") = CustBillAddrTbl.Fields("Billing_Apt")
  69.         OrderTicketTbl.Fields("Billing_PO_Box") = CustBillAddrTbl.Fields("Billing_PO_Box")
  70.         OrderTicketTbl.Fields("Billing_City") = CustBillAddrTbl.Fields("Billing_City")
  71.         OrderTicketTbl.Fields("Billing_State") = CustBillAddrTbl.Fields("Billing_State")
  72.         OrderTicketTbl.Fields("Billing_Zip") = CustBillAddrTbl.Fields("Billing_Zip")
  73.     End If
  74.  
  75.     ' Find the customer product record
  76.     Set CustProdTbl = CustDB.OpenTable("CustProd")
  77.     CustProdTbl.Index = "Key1"
  78.     CustProdTbl.Seek "=", Customer_number, Product
  79.     If (CustProdTbl.NoMatch = True) Then
  80.         MsgBox ("No product record found for this customer.")
  81.         Exit Sub
  82.     End If
  83.  
  84.     OrderTicketTbl.Fields("product") = CustProdTbl.Fields("product")
  85.     OrderTicketTbl.Fields("Qty") = CustProdTbl.Fields("Qty")
  86.       OrderTicketTbl.Fields("Price") = CustProdTbl.Fields("Price")
  87.     OrderTicketTbl.Fields("Subtotal") = CustProdTbl.Fields("Subtotal")
  88.     If CustProdTbl.Fields("Tax") > 0 Then
  89.         OrderTicketTbl.Fields("Tax") = CustProdTbl.Fields("Subtotal") * 0.06
  90.         OrderTicketTbl.Fields("Total") = CustProdTbl.Fields("Subtotal") & (CustProdTbl.Fields("Subtotal") * 0.06)
  91.     Else
  92.         OrderTicketTbl.Fields("Total") = CustProdTbl.Fields("Subtotal")
  93.     End If
  94.  
  95.  
  96.     OrderTicketTbl.Fields("Deposit") = CustProdTbl.Fields("Deposit")
  97.     OrderTicketTbl.Update
  98.  
  99. End Sub
  100.  
  101. Sub MAIN()
  102.  
  103.     Cover_scr.Show
  104.  
  105. End Sub
  106.  
  107. Public Sub CalculateCurrentBalance()
  108.  
  109.     Dim RecSet As Snapshot
  110.     Dim buffer As String
  111.     Dim CustOrdTbl As Table
  112.     Dim charge
  113.     Dim credit
  114.     Dim Tbl As Table
  115.  
  116.     ' Get all of the customers orders and subtract
  117.     ' the credits from the charges.
  118.     Set CustOrdTbl = CustDB.OpenTable("Orders")
  119.     buffer = "Select Sum(Charge),Sum(Credit) From Orders Where CustomerNum = " & Str(Customer_number) & ";"
  120.  
  121.     Set RecSet = CustDB.CreateSnapshot(buffer)
  122.  
  123.     ' If there is no value, then the value is considered zero.
  124.     If (IsNull(RecSet.Fields(0))) = True Then
  125.         charge = 0
  126.     Else
  127.         charge = RecSet.Fields(0)
  128.     End If
  129.  
  130.     If (IsNull(RecSet.Fields(1))) = True Then
  131.         credit = 0
  132.     Else
  133.         credit = RecSet.Fields(1)
  134.     End If
  135.  
  136.     ' Set the gloabal variable
  137.     CurrentBalance = charge - credit
  138.     
  139.     ' close the order table
  140.     CustOrdTbl.Close
  141.  
  142.     ' Now update the main customer table.
  143.     Set Tbl = CustDB.OpenTable("CustMain")
  144.     Tbl.Index = "PrimaryKey"
  145.     Tbl.Seek "=", Customer_number
  146.     If (Tbl.NoMatch = False) Then
  147.         Tbl.Edit
  148.         Tbl.Fields("Current_Balance") = CurrentBalance
  149.         Tbl.Update
  150.     End If
  151.     Tbl.Close
  152.     
  153.  
  154. End Sub
  155.