0

我一直在尝试在 VBA 中为我的 Access 应用程序创建一个子例程:

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

如果 Mutual 为真,那应该只将给定的产品添加到产品集合中,并将引用设置为用户引用。

那应该可以工作...问题是我不知道如何称呼我目前的尝试是:

User.addProduct(Product, 0, True)

但是 IDE 希望最后有一个 = 所以我认为这会起作用:

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

但这会导致编译错误: Expected function or Variable

4

2 回答 2

1

尝试使用以下命令调用该行:

User.addProduct Product, 0, True

或者

Call User.addProduct(Product, 0, True)

删除()或使用Call关键字应该适合您。

于 2012-07-05T13:09:02.030 回答
1

我不确定这是否是您想要做的,但如果它是将类实例添加到集合中,我建议如下:

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

而对于私有产品类属性 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 

您可以通过以下方式在类模块中声明属性:

Private pVIP1 as <Type>

和吸气剂/二传手:

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

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

如果类型是对象,则需要使用 SET 而不是 LET。

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

也许我误解了你的意图,因为我没有看到将我添加到每个案例的目的。但这是我能想到的最好的。

于 2012-07-05T13:10:33.803 回答