9

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

braX
  • 11,506
  • 5
  • 20
  • 33
maxhugen
  • 1,870
  • 4
  • 22
  • 44

2 Answers2

13

VBA has two functions: TypeName and TypeOf (TypeOf does not have a VBA documentation article, but the function exists nonetheless).

However, for a good example of their use, you can refer to the Determining Object Type article from the VB.NET documentation.

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
nos.17
  • 13
  • 7
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • 2
    Couldn't find any reference to a TypeOf keyword in Access VBA. Suspect this is .NET only. [Here](http://office.microsoft.com/en-us/access-help/typename-function-HA001228928.aspx?CTT=5&origin=HA010131676) is the MSDN reference to the TypeName function in Access VBA. By the way, this may be handy: a [list of all Access VBA functions by category](http://office.microsoft.com/en-us/access-help/access-functions-by-category-HA010131676.aspx). – Simon Elms Oct 25 '12 at 20:53
  • 1
    @simon, it is an operator and is present in - i would guess - all versions of VBA .. http://msdn.microsoft.com/en-us/library/0ec5kw18%28v=VS.80%29.aspx – Gabriele Petrioli Oct 26 '12 at 00:11
  • 1
    @Gaby: I think the reference you provided is for VB.NET, not VBA. When I looked for a reference to TypeOf I was only able to find articles for VB.NET, couldn't find anything for VBA. – Simon Elms Nov 01 '12 at 02:47
  • Maybe this link could be usefull as well: http://www.mrexcel.com/forum/excel-questions/663582-visual-basic-applications-difference-between-type-type-name.html – Daniel Dušek Dec 19 '13 at 21:44
  • Indeed, it might be helpful to mention that the TypeOf "operator" doesn't have its own section in Help. Instead, Access VBA help refers to it as an "If TypeOf" construct in the same section as the If...Then...Else Statement. – Bobort Mar 04 '15 at 20:33
7

Simplest way to determine the access type in access is to do an object lookup within the Access' system tables.

Here would be the lookup:

DLookup("Type","MSysObjects","NAME = '" & strObject & "'")

strObject is the name of the object within Access

The result is one of the number below OR NULL if the object does not exist in Access

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

so, the dlookup would provide "-32768" for a Form or "-32764" for a Report Hope that helps

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
Tom Davidson
  • 71
  • 1
  • 1
  • This method only works with Access objects, it won't return control types. TypeName returns Access object types and control types. – DataWriter Aug 05 '17 at 18:16