注册 登录
Office中国论坛/Access中国论坛 返回首页

zhuyiwen的个人空间 http://www.office-cn.net/?625 [收藏] [复制] [分享] [RSS]

日志

关于 VBA 中 Public Function NewEnum() As IUnknown

热度 1已有 1901 次阅读2012-9-17 14:22 |个人分类:VBA| NewEnum

例如,类 cField 定义如下:
Option Explicit

Public Enum xlsDataType
    xdString = 0
    xdNumeric = 1
    xdDate = 2
End Enum

Public Index As Integer
Public ID As String
Public Value As String
Public DataType As xlsDataType
Private m_Name As String
Public Property Let Name(ByVal data As String)
    m_Name = data
    
    DataType = xdString
    
    If InStr(data, "日期") > 0 Then DataType = xdDate
    If InStr(data, "数量") > 0 Then DataType = xdNumeric
    If InStr(data, "面积") > 0 Then DataType = xdNumeric
    If data = "价值" Then DataType = xdNumeric
    
End Property
Public Property Get Name() As String
    Name = m_Name
End Property

集合类 cFields 定义如下:
Option Explicit

Private m_col As New Collection

Public Function Add(ByVal Name As String) As cField
    Dim fld As New cField
    Static intFldNum As Integer

    With fld
        intFldNum = intFldNum + 1
        
        .Index = intFldNum
        .ID = "F" & Format(intFldNum, "00000")
        .Name = Name
        
        m_col.Add fld, .ID
    End With
    
    Set Add = fld
End Function

Public Function Count() As Long
    Count = m_col.Count
End Function

Public Sub Delete(ByVal Index As Variant)
    m_col.Remove Index
End Sub

Public Function Item(ByVal Index As Variant) As cField
    Set Item = m_col.Item(Index)
End Function

Public Function NewEnum() As IUnknown
    Set NewEnum = m_col.[_NewEnum]
End Function

在例程中:
Function ImportTo()
    Dim c As Long
    Dim r As Long
    
    c = ActiveSheet.UsedRange.Columns.Count
    r = ActiveSheet.UsedRange.Rows.Count

    Dim flds As New cFields
    
    Dim i As Long
    
    For i = 1 To c
        flds.Add Cells(1, i).Value
    Next
    
    Dim f As cField
    For Each f In flds
        Debug.Print f.Index, f.ID, f.Name, f.DataType, f.Value
    Next
End Function

当 For Each 时会报错。

解决办法
Public Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
    Set NewEnum = m_col.[_NewEnum]
End Function

即手工加入
Attribute NewEnum.VB_UserMemId = -4

隐藏方法
Attribute NewEnum.VB_MemberFlags = "40"

缺省属性
Attribute Item.VB_UserMemId = 0

Public Property Get Item(ByVal Index As Variant) As cField
Attribute Item.VB_UserMemId = 0
    Set Item = m_col.Item(Index)
End Property

缺省变量
Attribute Value.VB_VarUserMemId = 0

Public Value As String
Attribute Value.VB_VarUserMemId = 0

发表评论 评论 (1 个评论)

回复 lynnwang 2012-9-23 19:34
新手的好东东。
但注意
Attribute NewEnum.VB_MemberFlags = "40"
在VBA中是没有用的,MDB编译后也如此。
老朱,你见多识广,看看下面的链接,给我一些建议
类作为后台MDE,有什么要注意的
http://www.access-cn.com/forum.php?mod=viewthread&tid=114597&page=1&extra=#pid668364

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-2 14:53 , Processed in 0.068986 second(s), 18 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回顶部