Class Screen (Access VBA)

The Screen object refers to the particular form, report, or control that currently has the focus. To use a Screen class variable it first needs to be instantiated, for example

ActiveControl

You can use the ActiveControl property together with the Screen object to identify or refer to the control that has the focus.

This property setting contains a reference to the Control object that has the focus at run time. You can use the ActiveControl property to refer to the control that has the focus at run time together with one of its properties or methods. The following example assigns the name of the control with the focus to the strControlName variable.


Dim ctlCurrentControl As Control 
Dim strControlName As String 
Set ctlCurrentControl = Screen.ActiveControl 
strControlName = ctlCurrentControl.Name

ActiveDatasheet

You can use the ActiveDatasheet property together with the Screen object to identify or refer to the datasheet that has the focus.

The ActiveDatasheet property setting contains the datasheet object that has the focus at run time. You can use this property to refer to an active datasheet together with one of its properties or methods. For example, the following code uses the ActiveDatasheet property to reference the top row of the selection in the active datasheet.


TopRow = Screen.ActiveDatasheet.SelTop

ActiveForm

You can use the ActiveForm property together with the Screen object to identify or refer to the form that has the focus.

This property setting contains a reference to the Form object that has the focus at run time. You can use the ActiveForm property to refer to an active form together with one of its properties or methods. The following example displays the Name property setting of the active form.


Dim frmCurrentForm As Form 
Set frmCurrentForm = Screen.ActiveForm 
MsgBox "Current form is " & frmCurrentForm.Name

ActiveReport

You can use the ActiveReport property together with the Screen object to identify or refer to the report that has the focus.

This property setting contains a reference to the Report object that has the focus at run time. You can use the ActiveReport property to refer to an active report together with one of its properties or methods. The following example displays the Name property setting of the active report.


Dim rptCurrentReport As Report 
Set rptCurrentReport = Screen.ActiveReport 
MsgBox "Current report is " & rptCurrentReport.Name

MousePointer

You can use the MousePointer property together with the Screen object to specify or determine the type of mouse pointer currently displayed.

The setting for the MousePointer property is an Integer value representing one of the following pointers.


Screen.MousePointer = 11

PreviousControl

You can use the PreviousControl property together with the Screen object to return a reference to the control that last received the focus.

The PreviousControl property contains a reference to the control that last had the focus. After you establish a reference to the control, you can access all the properties and methods of the control. You can't use the PreviousControl property until more than one control on any form has received the focus after a form is opened. Microsoft Access generates an error if you attempt to use this property when only one control on a form has received the focus.


Public Function ProcessData() As Integer 
 
 ' No previous control error. 
 Const conNoPreviousControl = 2483 
 Dim ctlPrevious As Control 
 
 On Error GoTo Process_Err 
 
 Set ctlPrevious = Screen.PreviousControl 
 If ctlPrevious.Name = "txtFinalEntry" Then 
 ' 
 ' Process Data Here. 
 ' 
 ProcessData = True 
 Else 
 ' Set focus to txtFinalEntry and display message. 
 Me!txtFinalEntry.SetFocus 
 MsgBox "Please enter a value here." 
 ProcessData = False 
 End If 
 
Process_Exit: 
 Set ctlPrevious = Nothing 
 Exit Function 
 
Process_Err: 
 If Err = conNoPreviousControl Then 
 Me!txtFinalEntry.SetFocus 
 MsgBox "Please enter a value to process.", vbInformation 
 ProcessData = False 
 End If 
 Resume Process_Exit 
 
End Function