CurrentObjectName 属性

expandtri全部显示

可以使用 CurrentObjectName 属性和 Application 对象确定活动数据库对象的名称。活动数据库对象是获得了焦点的对象,或者其中的代码正在执行的对象。String 型,只读。

expression.CurrentObjectName

expression     必需。返回“应用于”列表中的一个对象的表达式。

说明

CurrentObjectName 属性由 Microsoft Access 设置为一个包含活动对象名称的字符串表达式

该属性仅在使用 Visual Basic 时才可用。

下列条件用于确定对象是否为活动对象:

?如果活动对象是对象的属性表、命令按钮、菜单、模板或属性列表CurrentObjectName 属性将返回基础对象的名称。
?如果活动对象是弹出式窗体CurrentObjectName 属性将引用弹出式窗体本身,而不是其下的窗体。
?如果活动对象是“数据库”窗口CurrentObjectName 属性将返回在“数据库”窗口中选取的对象。
?如果没有选取任何对象,CurrentObjectName 属性将返回零长度字符串 (" ")。
?如果当前状态比较模糊(活动对象不是表、查询、窗体、报表、宏或模块),如一个对话框获得焦点,则 CurrentObjectName 属性返回对话框的名称。

可以将 SysCmd 方法与该属性一起使用,来确定活动对象和它的状态(例如,对象是否打开、新建或是已经更改但还未保存)。

示例

以下示例使用 SysCmd 函数、CurrentObjectTypeCurrentObjectName 属性来确定活动对象是否为“产品”窗体,以及此窗体是否打开而且已经更改,只是没有保存。如果这些条件为真,则窗体将被保存然后关闭。

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