CurrentObjectType Property

Microsoft Access Visual Basic

CurrentObjectType Property

       

You can use the  CurrentObjectType property together with the Application object to determine the type of the active database object (table, query, form, report, macro, module, data access page, server view, database diagram, or stored procedure). The active database object is the object that has the focus or in which code is running.

The CurrentObjectType property is set by Microsoft Access to one of the following Microsoft Access intrinsic constants.

Setting Description
acTable (0) The active object is a table.
acQuery (1) The active object is a query.
acForm (2) The active object is a form.
acReport (3) The active object is a report.
acMacro (4) The active object is a macro.
acModule (5) The active object is a module.
acDataAccessPage (6) The active object is a data access page.
acServerView (7) The active object is a server view.
acDiagram (8) The active object is a database diagram.
acStoredProcedure (9) The active object is a stored procedure.

The following conditions determine which object is considered the active object:

  • If the active object is a property sheet, command bar, menu, palette, or field list of an object, the CurrentObjectType property returns the type of the underlying object.

  • If the active object is a pop-up form, the CurrentObjectType property refers to the pop-up form itself, not the form from which it was opened.

  • If the active object is the Database window, the CurrentObjectType property returns the item selected in the Database window.

  • If no object is selected, the CurrentObjectType property returns True.

  • If the current state is ambiguous (the active object isn't a table, query, form, report, macro, or module) for example, if a dialog box has the focus the CurrentObjectType property returns True.

You can use this property with the SysCmd method to determine the active object and its state (for example, if the object is open, new, or has been changed but not saved).

Example

The following example uses the CurrentObjectType and CurrentObjectName properties with the SysCmd function to determine if the active object is the Products form and if this form is open and has been changed but not saved. If these conditions are true, the form is saved and then closed.

Public Sub CheckProducts()
    
    Dim intState As Integer
    Dim intCurrentType As Integer
    Dim strCurrentName As String

    intCurrentType = Application.CurrentObjectType
    strCurrentName = Application.CurrentObjectName
    
    If intCurrentType = acForm And strCurrentName = "Products" Then
        intState = SysCmd(acSysCmdGetObjectState, intCurrentType, _
                   strCurrentName)
             
        ' Products form changed but not saved.
        If intState = acObjStateDirty + acObjStateOpen Then
            
            ' Close Products form and save changes.
            DoCmd.Close intCurrentType, strCurrentName, acSaveYes
        End If
    End If
End Sub