home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Software Sampler / Visual_Basic_Software_Sampler_Visual_Basic_Programmers_Journal_June_1996.iso / issues / 03mar96 / code / p81lst1.txt < prev    next >
Text File  |  1996-02-06  |  6KB  |  280 lines

  1. Listing 1 [[VB4]]
  2.  
  3. ' Removes inventory from the system
  4. ' This is used when inventory is sold, 
  5. ' damaged, or adjusted
  6. ' Parameters:
  7. '    sInvItem        Product Number of the 
  8. '                    item
  9. '    iNumRemoved        Number of the 
  10. '                        product removed from 
  11. '                        inventory
  12. ' Returns:
  13. '    iTotal            Total number in 
  14. '                    inventory
  15. Public Function _
  16.     RemoveInventory(sInvItem As _
  17.     String, iNumRemoved As Integer) As _
  18.     Integer
  19. Dim iCurrentTotal
  20.  
  21.     ' Retrieve the current inventory 
  22.     ' count from the collection
  23.     iCurrentTotal = _
  24.         Me.Item(sInvItem).Quantity
  25.     
  26.     ' Perform the subtraction
  27.     iCurrentTotal = iCurrentTotal - _
  28.         iNumRemoved
  29.     
  30.     ' Code here should ensure the total 
  31.     ' number in inventory is never < 0
  32.     
  33.     ' Code here could also check the 
  34.     ' reorder point and automatically 
  35.     ' reorder when an order point is 
  36.     ' reached
  37.     
  38.     ' Update the collection
  39.     Me.Item(sInvItem).Quantity = _
  40.         iCurrentTotal
  41.     
  42.     ' Return the total amount
  43.     RemoveInventory = iCurrentTotal
  44.  
  45. End Function
  46.  
  47.  
  48.  
  49. Listing 2 [[vb4]]
  50.  
  51.  
  52. Private Sub Class_Initialize()
  53.     ' Create the collection of inventory 
  54.     ' items
  55.     CollectProducts
  56. End Sub
  57.  
  58. ' Fills the collection with the products
  59. Private Sub CollectProducts()
  60.     ' This is the prototype code
  61.     ' to create the data in the collection
  62.     ' Normally this information would
  63.     ' exist in a database
  64.     Dim Item As New CItem
  65.     
  66.     Item.ProductID = "1"
  67.     Item.ProductNumber = "WD-123"
  68.     Item.ProductName = "Widgets"
  69.     Item.Quantity = 16
  70.         
  71.     ' Add this one to the collection
  72.     ' Using the product number 
  73.     ' as the key
  74.     m_colItems.Add Item, _
  75.         Item.ProductNumber
  76.  
  77.     ' Clear the reference
  78.     Set Item = Nothing
  79.         
  80.     ' Set another one
  81.     Item.ProductID = "2"
  82.     Item.ProductNumber = "GG-123"
  83.     Item.ProductName = "Gagets"
  84.     Item.Quantity = 120
  85.         
  86.     ' Add this one to the collection
  87.     ' Using the product number 
  88.     ' as the key
  89.     m_colItems.Add Item, _
  90.         Item.ProductNumber
  91.         
  92. End Sub
  93.  
  94.  
  95.  
  96. Listing 3  [[VB4]]
  97.  
  98. ' Form Name:    frmInventory
  99. ' Author:        Deborah Kurata, InStep 
  100. '                Technologies
  101. ' Date:        December 10, 1995
  102. ' Description:     Inventory form. 
  103. ' This is a little test form for the OLE 
  104. ' server and NOT a good design for an 
  105. ' inventory form!
  106. '
  107. ' Revisions:
  108. '
  109. Option Explicit
  110.  
  111. ' Private member variables
  112. Private m_Inventory As CInventory
  113.  
  114. ' Constants for the command butons
  115. Const iCLOSE = 0
  116. Const iRECEIVE = 1
  117. Const iSELL = 2
  118.  
  119. Private Sub cboItems_Click()
  120.     ' If the user selects an item
  121.     ' clear the number received
  122.     txtReceived.Text = ""
  123.     
  124.     ' Set the total in inventory as 
  125.     ' appropriate
  126.     If cboItems.ListIndex <> -1 Then
  127.         txtTotal.Text = m_Inventory.Item_
  128.             (cboItems.ListIndex + _
  129.             1).Quantity
  130.     End If
  131.     
  132.     ' Ensure the button is disabled 
  133.     ' until a quantity is entered
  134.     cmdInventory(iRECEIVE).Enabled = _
  135.         False
  136.     cmdInventory(iSELL).Enabled = False
  137. End Sub
  138.  
  139. Private Sub cmdInventory_Click(Index _
  140.     As Integer)
  141. Dim iTotal As Integer
  142. Dim sProductNumber As String
  143.  
  144.     Select Case Index
  145.     
  146.         Case iCLOSE
  147.             Unload Me
  148.         
  149.         Case iRECEIVE
  150.             ' Product number for the 
  151.             ' inventory item
  152.             ' NOTE: combo's are 0 based, 
  153.             ' collections are 1 based
  154.             sProductNumber = _
  155.                 m_Inventory.Item_
  156.                 (cboItems.ListIndex + _
  157.                 1).ProductNumber
  158.             
  159.             ' Add the defined amount to 
  160.             ' the inventory
  161.             iTotal = m_Inventory._
  162.                 AddInventory_
  163.                 (sProductNumber, _
  164.                 Val(txtReceived.Text))
  165.             If Err.Number <> 0 Then
  166.                 ' A more friendly error 
  167.                 ' message would be 
  168.                 ' displayed to the user 
  169.                 ' here
  170.                 ' With a more detailed 
  171.                 ' message printed to a 
  172.                 ' log file
  173.                 MsgBox Err.Description
  174.             End If
  175.             
  176.             ' Display the revised 
  177.             ' inventory amount on the 
  178.             ' screen
  179.             txtTotal.Text = iTotal
  180.             
  181.         Case iSELL
  182.             ' Product number for the 
  183.             ' inventory item
  184.             ' NOTE: combo's are 0 based, 
  185.             ' collections are 1 based
  186.             sProductNumber = _
  187.                 m_Inventory.Item_
  188.                 (cboItems.ListIndex + _
  189.                 1).ProductNumber
  190.             
  191.             ' Remove the defined amount 
  192.             ' from the inventory
  193.             iTotal = _
  194.                 m_Inventory._
  195.                 RemoveInventory_
  196.                 (sProductNumber, _
  197.                 Val(txtReceived.Text))
  198.             If Err.Number <> 0 Then
  199.                 ' A more friendly error 
  200.                 ' message would be 
  201.                 ' displayed to the user 
  202.                 ' here
  203.                 ' With a more detailed 
  204.                 ' message printed to a 
  205.                 ' log file
  206.                 MsgBox Err.Description
  207.             End If
  208.             
  209.             ' Display the revised 
  210.             ' inventory amount on the 
  211.             ' screen
  212.             txtTotal.Text = iTotal
  213.     End Select
  214. End Sub
  215.  
  216. Private Sub Form_Load()
  217. Dim colItems As New Collection
  218. Dim lCount As Long
  219.  
  220.     ' Create only one instance of the 
  221.     ' inventory class
  222.     Set m_Inventory = New CInventory
  223.     If Err.Number <> 0 Then
  224.         ' A more friendly error message 
  225.         ' would be displayed to the user 
  226.         ' here
  227.         ' With a more detailed message 
  228.         ' printed to a log file
  229.         MsgBox Err.Description
  230.         Unload Me
  231.         GoTo EXIT_Form_Load
  232.     End If
  233.     
  234.     ' Fill the combo box with values
  235.     For lCount = 1 To m_Inventory.Count
  236.         With m_Inventory.Item(lCount)
  237.             cboItems.AddItem _
  238.                 .ProductNumber & _
  239.                 Space(5) & .ProductName
  240.         End With
  241.     Next lCount
  242.     
  243. EXIT_Form_Load:
  244. End Sub
  245.  
  246. Private Sub Form_Unload(Cancel As _
  247.     Integer)
  248.     ' Clear the instance
  249.     Set m_Inventory = Nothing
  250. End Sub
  251.  
  252. Private Sub txtReceived_Change()
  253.     ' If there is an amount entered and 
  254.     ' the command button is disabled,
  255.     ' enable it
  256.     If txtReceived.Text <> "" And _
  257.         cmdInventory(iRECEIVE).Enabled _
  258.         = False Then
  259.         cmdInventory(iRECEIVE).Enabled _
  260.             = True
  261.         cmdInventory(iSELL).Enabled _
  262.             = True
  263.     End If
  264. End Sub
  265.  
  266. Private Sub _
  267.     txtReceived_KeyPress(KeyAscii _
  268.     As Integer)
  269.     If (Chr$(KeyAscii) <> vbBack) And _
  270.         (Not IsNumeric(Chr$(KeyAscii))) _
  271.         Then
  272.         Beep
  273.         KeyAscii = 0
  274.     End If
  275. End Sub
  276.  
  277. Prog w/Class Listings    March 1996    page 5
  278.  
  279.  
  280.