How do I create a class that takes on the traits of a collection. For example, I have custom clsCreditCardTransaction class, which is a individual transaction. However, I want to create a clsCreditCardTransactions class which will be a collection of all the clcCreditCardTransaction objects. How do I create a custom collection class?
Chergh, yes, I know how to set a collection object, with the statements you wrote, but that is not what I am asking. Let me explain a bit better.
When you use a collection object, any object for this matter, there are methods available such as Add(), Remove(), Item(). I guess my question is, if I create a new class such as clsTransactions, do I need to add those methods manually to the class. I am imaging that there is a way for my class to take the traits of a collection, without add those methods. Why reinvent the wheel? So is there a way for my new class to "inherit" the methods of a default collection object? I am coming from a VB.NET environment. Not sure if this can be done in Access VBA.
Examples of what I am trying to do:
--------
Public Class clsTransactions as Collection 'Specify current class as 'collection
blah blah...
End Class
----------
Public Sub Test()
Dim oTransCol as New clsTransactions
Dim oTran as New clsTransaction
oTransCol.Add 'Intellesense brings up collection methods .Item .Remove
End Sub
Hi, I know that problem has been asked before, and had look. Found this post and threw together the definition that in a quick test seems to behave like a true collection - you can do For Each...Next on it etc, but in this sample, it accept only Currency as data type.
To import this, you should open Notepad.exe, paste the text below in, then save as a .cls file. In VBE, File -> Import File and choose the .cls. You'll note some lines will go missing, but that's okay. They're hidden. The only thing is that we can't hide the NewEnum (which is the magic we need to get For Each...Next to work) because VBA does not process the attribute value 40 which makes it a hidden property.
But you can at least edit the data type and thus use your custom object instead of currency data type, and be aware that Item is not correctly implemented - I didn't look into what would be the assumed default for Key, Before and After. You need to add logic to handle the missing arguments if necessary but hopefully you have a starting point.
Code:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Class1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Compare Database
Option Explicit
Private c As Collection
Public Sub Add(Item As Currency, Optional Key As String, Optional Before As Variant, Optional After As Variant)
c.Add Item, Key, Before, After
End Sub
Public Sub Remove(Index As Variant)
c.Remove Index
End Sub
Property Get Item(Index As Variant) As Currency
Attribute Item.VB_UserMemId = 0
Item = c.Item(Index)
End Property
Public Function Count() As Long
Count = c.Count
End Function
Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
Set NewEnum = Me_Collection.[_NewEnum]
End Property
PS Forgot to answer the other question - in this specific case, you have to add methods manually because VBA does like the interfaces which contains an underscore in the name so we can't do a Implements. Even if we did get to use Implements, we still have to manually add the implementation code because Implements only create an interface, not inheritance. Furthermore, I find Implements to be seriously lacking so I don't think it would really helped that much anyway.
This subject baffles me. I understand it for the most part but applying it in a practical situation seems to elude me... Does anyone have some sample databases or know of any tutorials on the topic?
Ken you may find this attachment useful. It uses amongst other things a collection class to retrieve and store values from the database properties which can be used thought the live of the session.