从类中访问集合中的项目

时间:2014-01-22 21:51:54

标签: vba excel-vba vb6 excel

-EDIT已修复 我错过了一件事,做错了一件事。首先,我错过了一个按索引访问集合的函数。我应该在我的模块代码

中使用for循环而不是每个循环

我忘了将它添加到集合类

Public Function GetPayRecords(ByVal index As Variant) As PayRecords
      Set GetPayRecords = pObjCol.item(index)
End Function

并替换

For Each vItem In .GetPayRecords
    ....code to do stuff    
Next vItem

在模块

Dim x As Integer
For x = 1 To .Count
    Debug.Print .GetPayRecords(x).PY_PayRecord.CEOCompanyID
    Debug.Print .GetPayRecords(x).PY_PayRecord.OrigBankID
Next x

我正在编写一个包含8个类的程序。每个类代表一种特定的记录类型。 我有一个包含这8个类的整体类,在模块中进行编码时这是简单的。我只需要声明一个类,它允许我访问所有8个类。我有一个包含所有记录类型的集合。一旦完成加载各个记录的所有逻辑,它们就会被添加到集合中。这一切都很完美,我可以看到集合中的所有记录。最后一步,恰好是我遇到问题的地方,我需要按记录类型提取集合中的每个项目并将其写入csv。我遇到的问题是试图遍历每条记录。

以下是结构的外观

  • clsAllRecordTypes

    • clsRecordType1
    • clsRecordType2
    • ...
    • clsRecordType8

收藏

  • clsColRecords
  

问题在于检索

模块

    Dim PayRecord As PayRecords 'Class of Classes
    Dim PayRecordList As bankCollection 

    ...code to load all the payrecords

    With payrecordlist
         Foreach vItem in .pObjCol

               debug.print .pObjCol.Item(?) ' not sure why i can't see all 8 

         next vItem
    End With

当我将vItem添加到手表时,我可以看到每个记录类型都填充了信息,但我无法访问它。下面是类和集合

班级

选项明确

'This class is a representation of all the record types that apply to our Payment Manager
'It aggregates all the record types (classes) into one class.  That one class is used in the main processing module for simplicty
'
Private pPayRecord As New PayRecord
Private pPNAR_OP As New PNAR_OP
Private pPNAR_RP As New PNAR_RP
Private pSuppACHREC As New SuppACHRec
Private pSuppCCRRec As New SuppCCRRec
Private pSuppCHKRec As New SuppCHKRec
Private pDocumentDelieveryRec As New DocumentDeliveryRecord
Private pInvoiceRecords As New InvoiceRecords

Public Property Get PY_PayRecord() As PayRecord
    Set PY_PayRecord = pPayRecord
End Property
Public Property Let PY_PayRecord(ByVal newPayRecord As PayRecord)
    Set pPayRecord = newPayRecord
End Property

Public Property Get PA_PNAR_OP() As PNAR_OP
    Set PA_PNAR_OP = pPNAR_OP
End Property
Public Property Let PA_PNAR_OP(ByVal newPNAR_OP_Record As PNAR_OP)
    Set pPNAR_OP = newPNAR_OP_Record
End Property

Public Property Get PA_PNAR_RP() As PNAR_RP
    Set PA_PNAR_RP = pPNAR_RP
End Property
Public Property Let PA_PNAR_RP(ByVal newPNAR_RP_Record As PNAR_RP)
    Set pPNAR_RP = newPNAR_RP_Record
End Property

Public Property Get AC_SuppACH() As SuppACHRec
    Set AC_SuppACH = pSuppACHREC
End Property
Public Property Let AC_SuppACH(ByVal newSuppACH_Record As SuppACHRec)
    Set pSuppACHREC = newSuppACH_Record
End Property

Public Property Get AC_SuppCCR() As SuppCCRRec
    Set AC_SuppCCR = pSuppCCRRec
End Property
Public Property Let AC_SuppCCR(ByVal newSuppCCR_Record As SuppCCRRec)
    Set pSuppCCRRec = newSuppCCR_Record
End Property

Public Property Get AC_SuppCHK() As SuppCHKRec
    Set AC_SuppCHK = pSuppCHKRec
End Property
Public Property Let AC_SuppCHK(ByVal newSuppCHK_Record As SuppCHKRec)
    Set pSuppCHKRec = newSuppCHK_Record
End Property

Public Property Get DocumentDeliveryRecord() As DocumentDeliveryRecord
    Set DocumentDeliveryRecord = pDocumentDelieveryRec
End Property
Public Property Let DocumentDeliveryRecord(ByVal newDocumentDeliveryRecord As DocumentDeliveryRecord)
    Set pDocumentDelieveryRec = newDocumentDeliveryRecord
End Property

Public Property Get InvoiceRecords() As InvoiceRecords
    Set InvoiceRecords = pInvoiceRecords
End Property
Public Property Let InvoiceRecords(ByVal newInvoiceRecord As InvoiceRecords)
    Set pInvoiceRecords = newInvoiceRecord
End Property

集合类

Option Explicit
Private pHeaderRec As New HeaderRec
Private pNewPayRecords As New PayRecords
Public pObjCol As Collection
Private pTrailerRec As New TrailerRec

Private Sub Class_Initialize()
    Set pObjCol = New Collection
End Sub
Private Sub Class_Terminate()
    Set pObjCol = Nothing
End Sub

Public Property Get HD_HeaderRecord() As HeaderRec
    Set HD_HeaderRecord = pHeaderRec
End Property
Public Property Let HD_HeaderRecord(ByVal newHeaderRecord As HeaderRec)
    Set pHeaderRec = newHeaderRecord
End Property

Sub Add(ByVal newPayRecs As PayRecords)
    pObjCol.Add newPayRecs
End Sub
Property Get Count() As Long
    Count = pObjCol.Count
End Property

Public Property Get TR_TrailerRecord() As TrailerRec
    Set TR_TrailerRecord = pTrailerRec
End Property
Public Property Let TR_TrailerRecord(ByVal newTrailer_Record As TrailerRec)
    Set pTrailerRec = newTrailer_Record
End Property

1 个答案:

答案 0 :(得分:0)

如果这没有用,我很抱歉,因为你的解释很难理解。但是,我假设你说你有一个Payrecords类型的对象,其中包含对其他七种类型PNAR_OP,PNAR_RP等对象的引用。后面这些对象中的每一个都包含你想要的“20-30个字段”得到。你问如何循环所有这些。

一种简单的方法是使用数组。是的,您可以通过集合或(更好的)字典进行操作,但是数组可以工作,它们很容易理解,并且当集合在尿布中运行时它们正在遍历对象。

让您的Payrecords拥有Object(6)类型的属性。初始化时,实例化七个对象中的每一个并将其添加到数组中(例如,“Set myPayrecordsObjects(3)= New SubCCRRec”等)。要循环,只需使用for next循环来遍历7个对象。

由于您未提供有关如何在这些对象中构建“字段”的信息,因此我建议您遍历ADO对象的Fields集合以循环遍历这些对象。 (如果你没有使用ADO Fields系列,那么你对细节的关注会得到我的回报。)

相关问题