MS Access : Determine Object Type

max*_*gen 8 ms-access object access-vba

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)

但是,我需要确定type函数中的对象.

有谁知道这样做的方法?

MTIA

Gab*_*oli 11

看一眼

typeOf和typeName

通用对象变量(即,您声明为Object的变量)可以保存来自任何类的对象.使用Object类型的变量时,您可能需要根据对象的类采取不同的操作; 例如,某些对象可能不支持特定的属性或方法.Visual Basic提供了两种方法来确定哪种类型的对象存储在对象变量中:TypeName函数和TypeOf ...是运算符.

TypeName和TypeOf ...是
TypeName函数返回一个字符串,当您需要存储或显示对象的类名时,它是最佳选择,如以下代码片段所示:

Dim Ctrl As Control = New TextBox  
MsgBox(TypeName(Ctrl))
Run Code Online (Sandbox Code Playgroud)

TypeOf ... Is运算符是测试对象类型的最佳选择,因为它比使用TypeName的等效字符串比较快得多.以下代码片段使用TypeOf ...在If ... Then ... Else语句中:

If TypeOf Ctrl Is Button Then  
    MsgBox("The control is a button.") 
End If
Run Code Online (Sandbox Code Playgroud)

  • 在Access VBA中找不到对TypeOf关键字的任何引用.怀疑这只是.NET.[here](http://office.microsoft.com/en-us/access-help/typename-function-HA001228928.aspx?CTT=5&origin=HA010131676)是对Access VBA中TypeName函数的MSDN引用.顺便说一下,这可能很方便:[按类别列出所有Access VBA功能列表](http://office.microsoft.com/en-us/access-help/access-functions-by-category-HA010131676.aspx ). (2认同)

小智 6

确定访问中访问类型的最简单方法是在Access的系统表中进行对象查找.

这将是查找:

DLookup("Type","MSysObjects","NAME = '" & strObject & "'")
Run Code Online (Sandbox Code Playgroud)

strObject 是Access中对象的名称

如果Access中不存在该对象,则结果是OR NULL之下的数字之一

1 = Access Table
4 = OBDB-Linked Table / View
5 = Access Query
6 = Attached (Linked) File  (such as Excel, another Access Table or query, text file, etc.)
-32768 = Access Form
-32764 = Access Report
-32761 = Access Module
Run Code Online (Sandbox Code Playgroud)

因此,dlookup将为表单提供"-32768"或为报告希望提供"-32764"帮助