Oli
Oli

Reputation: 561

VBA MS Access 2010 How to call a Subroutine of an Object?

I've been trying to create a Subroutine in VBA for my Access application:

Public Sub addProduct(ByRef Product As Product, AsFoo As Integer, Optional Mutual As Boolean = True)
    Products.Add (Product)

    If (Mutual) Then
        Select Case AsFoo
            Case 0
                Product.setProjectmanager = Me
            Case 1
                Product.setVIP1 = Me
            Case 2
                Product.setVIP2 = Me
            Case 11
                Product.setVIP1A = Me
            Case 22
                Product.setVIP2A = Me
        End Select
    End If
End Sub

That one should just add the given Product to a Collection of Products and set the reference to the User reference, if Mutual is true.

That one should work... the problem is that I don't know how to call that my current try is:

User.addProduct(Product, 0, True)

But the IDE wants to have a = at the end so I thought that would work:

User.addProduct(Product, 0, True) = Product

But that causes a Compile Error: Expected function or Variable

Upvotes: 0

Views: 6743

Answers (2)

Trace
Trace

Reputation: 18899

I'm not sure if this is what you want to do, but if it is adding class instances to a collection, I propose something like this:

Sub Main_sub()

dim colProduct as Collection 
dim cProduct as clsProduct
dim asFoo as integer    
dim Mutual as boolean


set colProduct = new collection
set cProduct = new clsproduct

asFoo = ? 'Define
Mutual = ? 'Define

'if the AddProduct function resides in the clsProduct class: 
set cProduct = cproduct.AddProduct(cProduct, asFoo, Mutual)
colProduct.add cProduct

set cProduct = nothing
set colProduct = nothing

end sub

And for private Product class properties Projectmanager, VIP1, VIP2, VIP1A,VIP2A

Public function addProduct(cProduct as clsProduct, AsFoo As Integer, Optional Mutual As Boolean) as cProduct

            If (Mutual) Then 
                Select Case AsFoo 
                Case 0 
                    cProduct.Projectmanager = Me 
                Case 1 
                    cProduct.VIP1 = Me 
                Case 2 
                    cProduct.VIP2 = Me 
                Case 11 
                    cProduct.VIP1A = Me 
                Case 22 
                    cProduct.VIP2A = Me 
                End Select 
            End If
set addProduct = cProduct
End Sub 

You declare properties in a class module this way:

Private pVIP1 as <Type>

And getters / setters:

Public Property Get VIP1() As <Type>
    VIP1 = pVIP1
End Property

Public Property Let VIP1(tVIP1 As <Type>)
    pVIP1 = tVIP1
End Property

If the type is an object, you need to use SET instead of LET.

Public Property SET VIP1(tVIP1 As <Type>)
    SET pVIP1 = tVIP1
End Property

Maybe I've got your intentions wrong, because I don't see the purpose of adding Me to each of the cases. But this was the best I could come up with.

Upvotes: 1

Gaffi
Gaffi

Reputation: 4367

Try calling that line with:

User.addProduct Product, 0, True

or

Call User.addProduct(Product, 0, True)

Removing the () or using the Call keyword should work for you.

Upvotes: 1

Related Questions