注冊 登錄
Office中國論壇/Access中國論壇 返回首頁

zhuyiwen的個人空間 http://ctxi.cn/?625 [收藏] [復(fù)制] [分享] [RSS]

日志

關(guān)于 VBA 中 Public Function NewEnum() As IUnknown

熱度 1已有 1995 次閱讀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, "數(shù)量") > 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

當(dāng) 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

發(fā)表評論 評論 (1 個評論)

回復(fù) 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-10-23 08:31 , Processed in 0.104097 second(s), 18 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回頂部