访问VBA对象类型 [英] Access VBA object types
问题描述
我正在尝试在Access项目中构建一个过程,该过程将接受表或表单作为参数的一部分.如果对象是一个表,那么它应该检查表的字段;如果是表单,则应该是表单的控件.
I am trying to build a procedure in my Access project that will accept either a table or a form as part of the argument. If the object is a table then it should check the table's fields; if it's a form then it should it the form's controls.
我尝试使用变体或对象变量类型作为参数.但是,无论哪种方式,当我检查VarType()时,传递的变量都是对象类型,因此我无法区分它们. (顺便说一下,我目前正在将一个表作为TableDef对象传递.)
I've tried using either variant or object variable types for the argument. But either way when I check the VarType() the passed variable is of type object, so I can't distinguish between them. (I'm currently passing a table as a TableDef object by the way.)
我会尝试根据参数类型重载函数,但显然这在VBA中不可用(据我从
I would try overloading the function based on parameter type but apparently that isn't available in VBA (as far as I know from Function Overloading and UDF in Excel VBA. Any suggestions?
推荐答案
要在VBA中测试某个类,请使用TypeOf
和Is
运算符的组合:
To test for a certain class in VBA, use a combination of the TypeOf
and Is
operators:
Sub WishVBAHadOverloads(ByVal Obj As Object)
If TypeOf Obj Is TableDef Then
Dim Def As TableDef
Set Def = Obj
' work with Def...
Exit Sub
End If
If TypeOf Obj Is Form Then
Dim Frm As Form
Set Frm = Obj
' work with Frm...
Exit Sub
End If
Err.Raise 999, "WishVBAHadOverloads", "Bad argument type - expected a TableDef or Form"
End Sub
这篇关于访问VBA对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!