VBA MS Access 2010 如何调用对象的子程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11344232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA MS Access 2010 How to call a Subroutine of an Object?
提问by Oli
I've been trying to create a Subroutine in VBA for my Access application:
我一直在尝试在 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
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.
如果 Mutual 为真,那应该只是将给定的产品添加到产品集合中,并将引用设置为用户引用。
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:
但是 IDE 想要在最后有一个 = 所以我认为这会起作用:
User.addProduct(Product, 0, True) = Product
But that causes a Compile Error:
Expected function or Variable
但这会导致编译错误:
Expected function or Variable
回答by Gaffi
Try calling that line with:
尝试调用该行:
User.addProduct Product, 0, True
User.addProduct Product, 0, True
or
或者
Call User.addProduct(Product, 0, True)
Call User.addProduct(Product, 0, True)
Removing the ()
or using the Call
keyword should work for you.
删除()
或使用Call
关键字应该对您有用。
回答by Trace
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
对于私有产品类属性 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.
如果类型是对象,则需要使用 SET 而不是 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.
也许我误解了你的意图,因为我没有看到将我添加到每个案例中的目的。但这是我能想到的最好的办法。