⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clsspoilages.cls

📁 <Visual Basic 数据库开发实例精粹(第二版)>一书首先介绍了Visual Basic(简称VB)开发的技巧和重点技术
💻 CLS
字号:
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsSpoilages"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Collection" ,"clsSpoilage"
Attribute VB_Ext_KEY = "Member0" ,"clsSpoilage"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit

'局部变量,保存集合
Private mCol As Collection

'查询报损信息,返回一个集合类
Public Function Query(Optional lngID As Long = -1) As clsSpoilages
  On Error Resume Next
  Dim rs As Recordset       '查询返回的记录集
  Dim obj As clsSpoilage    '进货对象
  Dim index As Long         '要生成的集合索引
  Dim strSQL As String      'SQL字符串
    
  '构造SQL语句
  strSQL = "SELECT * FROM fn_SpoilageQuery"
  If lngID = -1 Then
    strSQL = strSQL & "(NULL)"
  Else
    strSQL = strSQL & "(" & lngID & ")"
  End If
  
  '清空当前集合
  Me.Clear
  '执行查询并返回查询结果
  Set rs = g_Conn.Execute(strSQL)
  
  '往集合中添加查询结果
  For index = 1 To rs.RecordCount
    Set obj = New clsSpoilage
    '为单个对象的属性赋值
    With obj
      .ID = rs("SpoilageID").Value
      .BuyID = rs("BuyID").Value
      .GoodsID = rs("GoodsID").Value
      .GoodsName = Trim(rs("GoodsName").Value)
      .UnitName = Trim(rs("UnitName").Value)
      .Amount = rs("Amount").Value
      .Reportor = Trim(rs("Reportor").Value)
      .Reason = Trim(rs("Reason").Value)
      .RegistrarID = rs("RegistrarID").Value
      .RegistrarName = GetValueByID("UserInfo", "UserID", .RegistrarID, "TrueName")
      .RegDate = rs("RegDate").Value
      .TypeID = Trim(rs("TypeID").Value)
      .TypeName = GetValueByID("GoodsType", "TypeID", .TypeID, "TypeName")
      .SupplierID = Trim(rs("SupplierID").Value)
      .SupplierName = GetValueByID("Supplier", "SupplierID", .SupplierID, "SupplierName")
    End With
    '添加单个对象到集合中
    Me.Add obj
    '释放对象
    Set obj = Nothing
    rs.MoveNext
  Next index
  
  '释放查询结果集
  Set rs = Nothing
  '函数返回值
  Set Query = Me
End Function

Public Function Statistic() As clsSpoilages
  On Error Resume Next
  Dim rs As Recordset       '查询返回的记录集
  Dim obj As clsSpoilage    '进货对象
  Dim index As Long         '要生成的集合索引
  Dim strSQL As String      'SQL字符串
  
  '构造SQL语句
  strSQL = "Select * FROM fn_SpoilageStatistic()"
  
  '清空当前集合
  Me.Clear
  '执行查询并返回查询结果
  Set rs = g_Conn.Execute(strSQL)
 
  For index = 1 To rs.RecordCount
    Set obj = New clsSpoilage
    '为单个对象的属性赋值
    With obj
      '此时ID属性是商品编号,而非sale表中的销售编号
      .ID = rs("GoodsID").Value
      .GoodsName = Trim(rs("GoodsName").Value)
      .TypeName = GetValueByID("GoodsType", "TypeID", rs("TypeID").Value, "TypeName")
      .UnitName = Trim(rs("UnitName").Value)
      .TotalRegTimes = rs("TotalRegTimes").Value
      .TotalRegAmount = rs("TotalRegAmount").Value
    End With
    '添加单个对象到集合中
    Me.Add obj
    '释放对象
    Set obj = Nothing
    rs.MoveNext
  Next index
  
  '释放查询结果集
  Set rs = Nothing
  '函数返回值
  Set Statistic = Me
End Function

Private Sub Class_Initialize()
  '创建类后创建集合
  Set mCol = New Collection
End Sub

Private Sub Class_Terminate()
  '类终止后破坏集合
  Set mCol = Nothing
End Sub

Public Sub Add(obj As clsSpoilage)
  mCol.Add obj, "K" & obj.ID
  '在加入对象是,最好同时加入其“KEY”属性
  '“KEY”属性不可以是数字,因此在前面加一
  '个辅助字母“K”
End Sub

Public Property Get Item(vntIndexKey As Variant) As clsSpoilage
  '引用集合中的一个元素时使用。
  'vntIndexKey 包含集合的索引或关键字,
  '这是为什么要声明为 Variant 的原因
  '语法:Set foo = x.Item(xyz) or Set foo = x.Item(5)
  Set Item = mCol(vntIndexKey)
End Property

Public Property Get NewEnum() As IUnknown
  '本属性允许用 For...Each 语法枚举该集合。
  Set NewEnum = mCol.[_NewEnum]
End Property

Public Property Get Count() As Long
  '检索集合中的元素数时使用。语法:Debug.Print x.Count
  Count = mCol.Count
End Property

Public Sub Remove(vntIndexKey As Variant)
  '删除集合中的元素时使用。
  'vntIndexKey 包含索引或关键字,这是为什么要声明为 Variant 的原因
  '语法:x.Remove(xyz)
  mCol.Remove vntIndexKey
End Sub

Public Sub Clear()
  '清除集合中的全部元素
  Dim i As Long
  For i = 1 To mCol.Count
    mCol.Remove 1
  Next i
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -