📄 clsadmins.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 = "clsAdmins"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Collection" ,"clsAdmin"
Attribute VB_Ext_KEY = "Member0" ,"clsAdmin"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit
'局部变量,保存集合
Private mCol As Collection
Public Function Find(Optional strAccount As String = "") As clsAdmins
Dim rs As Recordset
Dim index As Long
Dim obj As clsAdmin
'按输入的参数查询,并返回一个集合类
Dim strSQL As String
'构造SQL语句
strSQL = "Select * from Admins WHERE "
If strAccount <> "" Then
strSQL = strSQL & "A_Account_S='" & strAccount & "' AND "
End If
strSQL = strSQL & "A_Account_S <>'' "
'清空当前集合
Me.Clear
Set rs = g_Conn.Execute(strSQL)
'往集合中添加查询结果
For index = 1 To rs.RecordCount
Set obj = New clsAdmin
With obj
.Account = rs("A_Account_S").Value
.Pwd = Trim(rs("A_Pwd_S").Value)
.IsDefault = rs("A_IsDefault_B").Value
End With
Me.AddEx obj
Set obj = Nothing
rs.MoveNext
Next index
Set rs = Nothing
Set Find = Me
End Function
Public Sub AddEx(obj As clsAdmin)
mCol.Add obj, "A" & obj.Account
'在加入对象是,最好同时加入其“KEY”属性
'“KEY”属性不可以是数字型,因此在前面随便加
'一个字母,此处加了一个“A”
End Sub
'清除集合中的全部元素
Public Sub Clear()
'注意!在清除时必须倒序清除,否则要出错!
Dim i As Long
For i = mCol.Count To 1 Step -1
mCol.Remove i
Next i
End Sub
Public Function Add(strAccount As String, Optional sKey As String) As clsAdmin
'创建新对象
Dim objNewMember As clsAdmin
Set objNewMember = New clsAdmin
'设置传入方法的属性
objNewMember.Account = strAccount
If Len(sKey) = 0 Then
mCol.Add objNewMember
Else
mCol.Add objNewMember, sKey
End If
'返回已创建的对象
Set Add = objNewMember
Set objNewMember = Nothing
End Function
Public Property Get Item(vntIndexKey As Variant) As clsAdmin
'引用集合中的一个元素时使用。
'vntIndexKey 包含集合的索引或关键字,
'这是为什么要声明为 Variant 的原因
'语法:Set foo = x.Item(xyz) or Set foo = x.Item(5)
Set Item = mCol(vntIndexKey)
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 Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
'本属性允许用 For...Each 语法枚举该集合。
Set NewEnum = mCol.[_NewEnum]
End Property
Private Sub Class_Initialize()
'创建类后创建集合
Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
'类终止后破坏集合
Set mCol = Nothing
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -