Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
963 views
in Technique[技术] by (71.8m points)

vba - Determine Object Type

Is there a way to determine the Object type, when passing a reference to a function?

I'm using a security permissions function, which determines if the user has permission to view/edit the Form passed to it by reference. I'd like to expand this to include reports as well.

To keep the function generic, I'd like to pass a ref for either a Form or a Report as an Object, eg: function gfSecurity_Permission(obj as Object)

However, I'd need to determine the type of the object within the function.

Does anyone know of a way to do that?

MTIA

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Take a look at

typeOf and typeName

Generic object variables (that is, variables you declare as Object) can hold objects from any class. When using variables of type Object, you may need to take different actions based on the class of the object; for example, some objects might not support a particular property or method. Visual Basic provides two means of determining which type of object is stored in an object variable: the TypeName function and the TypeOf...Is operator.

TypeName and TypeOf…Is
The TypeName function returns a string and is the best choice when you need to store or display the class name of an object, as shown in the following code fragment:

Dim Ctrl As Control = New TextBox  
MsgBox(TypeName(Ctrl))

The TypeOf...Is operator is the best choice for testing an object's type, because it is much faster than an equivalent string comparison using TypeName. The following code fragment uses TypeOf...Is within an If...Then...Else statement:

If TypeOf Ctrl Is Button Then  
    MsgBox("The control is a button.") 
End If

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...