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

📄 clsgoodses.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 = "clsGoodses"
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" ,"clsGoods"
Attribute VB_Ext_KEY = "Member0" ,"clsGoods"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit

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

'查询商品信息(按输入的参数查询,并返回一个集合类)
Public Function Query(Optional lngID As Long = -1, Optional lngTypeId As Long = -1) As clsGoodses
  On Error Resume Next
  Dim rs As Recordset       '查询返回的记录集
  Dim obj As clsGoods       '商品对象
  Dim index As Long         '要生成的集合索引
  Dim strSQL As String      'SQL字符串
  
  '构造SQL语句
  strSQL = "SELECT * FROM fn_GoodsQuery"
  If lngID = -1 Then
     strSQL = strSQL & "(NULL"
  Else
     strSQL = strSQL & "(" & lngID
  End If
  If lngTypeId = -1 Then
     strSQL = strSQL & ",NULL"
  Else
     strSQL = strSQL & "," & lngTypeId
  End If
  strSQL = strSQL & ")"
  
  '清空当前集合
  Me.Clear
  '执行查询并返回查询结果
  Set rs = g_Conn.Execute(strSQL)
  
  '往集合中添加查询结果
  For index = 1 To rs.RecordCount
    Set obj = New clsGoods
    '为单个对象的属性赋值
    With obj
      .ID = rs("GoodsID").Value
      .GoodsName = Trim(rs("GoodsName").Value)
      .UnitName = Trim(rs("UnitName"))
      .Amount = rs("Amount").Value
      .TypeID = Trim(rs("TypeID").Value)
      .TypeName = Trim(rs("TypeName").Value)
      .SupplierID = Trim(rs("SupplierID").Value)
      .SupplierName = Trim(rs("SupplierName").Value)
      .Introduce = Trim(rs("Introduce").Value)
      .Remark = Trim(rs("Remark").Value)
    End With
    '添加单个对象到集合中
    Me.Add obj
    '释放对象
    Set obj = Nothing
    rs.MoveNext
  Next index
  
  '释放查询结果集
  Set rs = Nothing
  '函数返回值
  Set Query = 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 clsGoods)
  mCol.Add obj, "K" & obj.ID
  '在加入对象是,最好同时加入其“KEY”属性
  '“KEY”属性不可以是数字,因此在前面加一
  '个辅助字母“K”
End Sub

Public Property Get Item(vntIndexKey As Variant) As clsGoods
  '引用集合中的一个元素时使用。
  '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 + -