clsSells.cls
Upload User: czxfzx
Upload Date: 2015-02-25
Package Size: 749k
Code Size: 5k
Development Platform:

Visual Basic

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsSells"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  15. Attribute VB_Ext_KEY = "Collection" ,"clsSell"
  16. Attribute VB_Ext_KEY = "Member0" ,"clsSell"
  17. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  18. Option Explicit
  19. '局部变量,保存集合
  20. Private mCol As Collection
  21. Public Function Find(Optional lgnID As Long = -1, Optional lngTypeId As Long = 0) As clsSells
  22.   Dim rs As Recordset
  23.   Dim index As Long
  24.   Dim obj As clsSell
  25.   
  26.   '按输入的参数查询,并返回一个集合类
  27.   Dim strSQL As String
  28.   
  29.   '构造SQL语句
  30.   strSQL = "Select * from Sell, Merchandise, MerchandiseType WHERE "
  31.   strSQL = strSQL & " M_TypeId_N = MT_ID_N AND S_MerchandiseId_N=M_ID_N "
  32.   If lgnID <> -1 Then
  33.     strSQL = strSQL & " AND S_ID_N=" & lgnID
  34.   End If
  35.   If lngTypeId <> 0 Then
  36.     strSQL = strSQL & " AND M_TypeId_N=" & lngTypeId
  37.   End If
  38.     
  39.   strSQL = strSQL & " AND S_ID_N>0"
  40.   
  41.   '清空当前集合
  42.   Me.Clear
  43.   Set rs = g_Conn.Execute(strSQL)
  44.   '往集合中添加查询结果
  45.   For index = 1 To rs.RecordCount
  46.     Set obj = New clsSell
  47.     With obj
  48.       .ID = rs("S_ID_N").Value
  49.       .MerchandiseID = rs("S_MerchandiseID_N").Value
  50.       .RegDate = rs("S_RegDate_D").Value
  51.       .Count = rs("S_Count_N").Value
  52.       .SellPrice = rs("S_SellPrice_N").Value
  53.       .OperatorId = rs("S_OperatorId_S").Value
  54.       .Remark = Trim(rs("S_Remark_R").Value)
  55.       .MerchName = GetValueByID("Merchandise", "M_ID_N", .MerchandiseID, "M_Name_S")
  56.     End With
  57.     Me.AddEx obj
  58.     Set obj = Nothing
  59.     rs.MoveNext
  60.   Next index
  61.   
  62.   Set rs = Nothing
  63.   Set Find = Me
  64. End Function
  65. Public Function FindStorage(Optional IsDesc As Boolean = True, _
  66.                       Optional nCount As Integer = 10) As clsSells
  67.   Dim rs As Recordset
  68.   Dim index As Long
  69.   Dim obj As clsSell
  70.   
  71.   '按输入的参数查询,并返回一个集合类
  72.   Dim strSQL As String
  73.   
  74.   Dim strOrder As String
  75.   If nCount <= 0 Then nCount = 10
  76.   strOrder = IIf(IsDesc, "DESC", "ASC")
  77.   
  78.   '构造SQL语句
  79.   strSQL = "Select M_ID_N, M_Name_S, MT_Name_S, "
  80.   strSQL = strSQL & " COUNT(S_Count_N) AS RegTimes, "
  81.   strSQL = strSQL & " SUM(S_SellPrice_N * S_Count_N) AS TotalPrice "
  82.   strSQL = strSQL & " FROM Sell, Merchandise, MerchandiseType "
  83.   strSQL = strSQL & " WHERE "
  84.   strSQL = strSQL & " M_TypeId_N = MT_ID_N AND S_MerchandiseId_N=M_ID_N "
  85.   strSQL = strSQL & " GROUP BY M_ID_N, M_Name_S, MT_Name_S "
  86.   
  87.   '清空当前集合
  88.   Me.Clear
  89.   '排序
  90.   Set rs = g_Conn.Execute(strSQL)
  91.   rs.Sort = "TotalPrice " & strOrder
  92.   Dim aa, bb As Double
  93.   '往集合中添加前nCount条查询结果
  94.   nCount = IIf(nCount < rs.RecordCount, nCount, rs.RecordCount)
  95.   For index = 1 To nCount
  96.     Set obj = New clsSell
  97.     With obj
  98.       '此时ID属性中仅存用于区分的商品ID,而非Sell表中的ID
  99.       .ID = rs("M_ID_N").Value
  100.       .MerchName = Trim(rs("M_Name_S").Value)
  101.       .TypeName = Trim(rs("MT_Name_S").Value)
  102.       .RegTimes = rs("RegTimes").Value
  103.       .TotalPrice = rs("TotalPrice").Value
  104.     End With
  105.     Me.AddEx obj
  106.     Set obj = Nothing
  107.     rs.MoveNext
  108.   Next index
  109.   
  110.   Set rs = Nothing
  111.   Set FindStorage = Me
  112.   
  113. End Function
  114. Public Sub AddEx(obj As clsSell)
  115.   mCol.Add obj, "A" & obj.ID
  116.   '在加入对象是,最好同时加入其“KEY”属性
  117.   '“KEY”属性不可以是数字型,因此在前面随便加
  118.   '一个字母,此处加了一个“A”
  119. End Sub
  120. '清除集合中的全部元素
  121. Public Sub Clear()
  122.   '注意!在清除时必须倒序清除,否则要出错!
  123.   Dim i As Long
  124.   For i = mCol.Count To 1 Step -1
  125.     mCol.Remove i
  126.   Next i
  127. End Sub
  128. Public Function Add(ID As Long, Optional sKey As String) As clsSell
  129.     '创建新对象
  130.     Dim objNewMember As clsSell
  131.     Set objNewMember = New clsSell
  132.     '设置传入方法的属性
  133.     objNewMember.ID = ID
  134.     objNewMember.ID = ID
  135.     If Len(sKey) = 0 Then
  136.         mCol.Add objNewMember
  137.     Else
  138.         mCol.Add objNewMember, sKey
  139.     End If
  140.     '返回已创建的对象
  141.     Set Add = objNewMember
  142.     Set objNewMember = Nothing
  143. End Function
  144. Public Property Get Item(vntIndexKey As Variant) As clsSell
  145.     '引用集合中的一个元素时使用。
  146.     'vntIndexKey 包含集合的索引或关键字,
  147.     '这是为什么要声明为 Variant 的原因
  148.     '语法:Set foo = x.Item(xyz) or Set foo = x.Item(5)
  149.   Set Item = mCol(vntIndexKey)
  150. End Property
  151. Public Property Get Count() As Long
  152.     '检索集合中的元素数时使用。语法:Debug.Print x.Count
  153.     Count = mCol.Count
  154. End Property
  155. Public Sub Remove(vntIndexKey As Variant)
  156.     '删除集合中的元素时使用。
  157.     'vntIndexKey 包含索引或关键字,这是为什么要声明为 Variant 的原因
  158.     '语法:x.Remove(xyz)
  159.     mCol.Remove vntIndexKey
  160. End Sub
  161. Public Property Get NewEnum() As IUnknown
  162. Attribute NewEnum.VB_UserMemId = -4
  163. Attribute NewEnum.VB_MemberFlags = "40"
  164.     '本属性允许用 For...Each 语法枚举该集合。
  165.     Set NewEnum = mCol.[_NewEnum]
  166. End Property
  167. Private Sub Class_Initialize()
  168.     '创建类后创建集合
  169.     Set mCol = New Collection
  170. End Sub
  171. Private Sub Class_Terminate()
  172.     '类终止后破坏集合
  173.     Set mCol = Nothing
  174. End Sub