Solved Get Object Type in Access VBA (1 Viewer)

Pac-Man

Active member
Local time
Today, 21:41
Joined
Apr 14, 2020
Messages
416
Hello,

Does there exist a function which when passed an access object gives its type in acObjectType format i.e. 2 for form, 3 for report and so on.

I need to find and saves object type in a table but couldn't find any such function. Before, the same was being performed by macro using screen. ActiveObject and when I converted this into VBA, screen.ActiveObject always gives me object type as table even when the code was run from a form (same form which had the macro and now converted VBA). So now I am in search of a built-in function of way to write a function which when passed an object, for instance gfGetObjectType(mObj as Object) as acObjectType

Best Regards
Abdullah
 

MarkK

bit cruncher
Local time
Today, 09:41
Joined
Mar 17, 2004
Messages
8,181
Check out the VBA.Typename() and VBA.VarType() functions. There is also the TypeOf operator that can determine if an object inherits from or implements a type, so if you have a class called cJob that implements interface ITask...
Code:
Option Compare Database
Option Explicit

Implements ITask

Private m_id As Long

Function Load(JobNumber As Long) As cJob
    m_id = JobNumber
    Set Load = Me
End Function

Property Get ITask_DateStart As Date
   ITask_DateStart = DLookup("DateStart", "tJob", "JobNumber = " & m_id)
End Property

... then you can write a function ...
Code:
Function GetImplements(obj as Object) as String
   Dim tmp as String
   If TypeOf obj is ITask Then tmp = "ITask "
   If TypeOf obj is cJob Then tmp = tmp & "cJob "
   If TypeOf obj is cAppointment Then tmp = tmp & "cAppointment "
   . . .
   GetImplements = tmp
End Function
So if cJob, cAppointment, cMeeting and cScheduleItem all implement ITask, then you can distinguish what underlying objects are in your ITask collection using TypeOf.
 

Pac-Man

Active member
Local time
Today, 21:41
Joined
Apr 14, 2020
Messages
416
This info is available in the Type field of the system table MSysObjects.
See this thread ms access - What are the definitions of the 'Flags' field values in the MSysobjects Table - Stack Overflow
I also have an example database with all the info you need for this View Database Objects - Mendip Data Systems
Thanks for the link and demo db. I might not have understand it properly but I think Flags of MSysObjects do not gives object type in acObjectType (please click me) format. Like the db you provided, Form1 is a form and flag value is 0 whereas it should be acForm (i.e. 2).

I created the following function but it only works for forms and report but I doubt there might be a better way:
Code:
Function gfObjectType(Obj As Object) As AcObjectType
    Dim sTypeName As String
    sTypeName = TypeName(Obj)

    If sTypeName Like "Form_*" Then
        gfObjectType = acForm
    ElseIf sTypeName Like "Report_*" Then
        gfObjectType = acReport
    End If
End Function
 

Pac-Man

Active member
Local time
Today, 21:41
Joined
Apr 14, 2020
Messages
416
Check out the VBA.Typename() and VBA.VarType() functions. There is also the TypeOf operator that can determine if an object inherits from or implements a type, so if you have a class called cJob that implements interface ITask...
Code:
Option Compare Database
Option Explicit

Implements ITask

Private m_id As Long

Function Load(JobNumber As Long) As cJob
    m_id = JobNumber
    Set Load = Me
End Function

Property Get ITask_DateStart As Date
   ITask_DateStart = DLookup("DateStart", "tJob", "JobNumber = " & m_id)
End Property

... then you can write a function ...
Code:
Function GetImplements(obj as Object) as String
   Dim tmp as String
   If TypeOf obj is ITask Then tmp = "ITask "
   If TypeOf obj is cJob Then tmp = tmp & "cJob "
   If TypeOf obj is cAppointment Then tmp = tmp & "cAppointment "
   . . .
   GetImplements = tmp
End Function
So if cJob, cAppointment, cMeeting and cScheduleItem all implement ITask, then you can distinguish what underlying objects are in your ITask collection using TypeOf.
Thanks for the code sample but pardon me for saying that I could not understand how to implement it to get object type form report or query or other object as acObjectType as I'm still a learner in VBA. And also I get error something like "user defined function not defined" when I try to use TypeOf function in access. I think Access don't have it, if I'm not wrong.
 

isladogs

MVP / VIP
Local time
Today, 17:41
Joined
Jan 14, 2017
Messages
18,219
As already stated, the Type field in MSysObjects provides the info you want.
For example, local tables have Type=1, queries have Type =5, forms -32768, reports -32764 etc as shown in the example database and the link provided.

The Flags field gives additional information e.g. query type (select/update etc)

The acObjectType enumeration is a different approach and the values do not match the Type values in MSysObjects. You can't mix the two methods.
 

MarkK

bit cruncher
Local time
Today, 09:41
Joined
Mar 17, 2004
Messages
8,181
Maybe it would help if you describe your purpose in greater detail. It's clear you need to distinguish types from each other, and it's clear that there are a number of different ways to do that. Why, for instance, does the result have to be expressed as an acObjectType enumeration value? I am constantly concerned about what type an object is, but I never use acObjectType as a means to make that distinction.
 

Pac-Man

Active member
Local time
Today, 21:41
Joined
Apr 14, 2020
Messages
416
Maybe it would help if you describe your purpose in greater detail. It's clear you need to distinguish types from each other, and it's clear that there are a number of different ways to do that. Why, for instance, does the result have to be expressed as an acObjectType enumeration value? I am constantly concerned about what type an object is, but I never use acObjectType as a means to make that distinction.
Thanks a lot for your reply and your time.
Actually in my app there is a filter which uses macros to create filter during run-time (on a emulated split form). While creating filter it saves object type in acObjectType format using screen.ActiveObjectType and object name where filter is being applied. When I converted it to VBA, object type was not being saved correctly. However using MSysObject table, as suggested by @isladogs, I will get type of object and save it, but currently I'm away from PC and couldn't do it till now.

Best Regards
Abdullah
 

Users who are viewing this thread

Top Bottom