The FocusSetOnCurrent Function

Public Function FocusSetOnCurrent( _
    strForm As String, _
    Optional strControlIfNew As String, _
    Optional strControlElse As String _
    ) As Integer
        
    'Copyright (c) Brendan Reynolds/Timarco Ltd, 1999.
    'All rights reserved.
    
    'e-mail [email protected]
    
    'This function sets the focus to the control specified
    'in the strControlIfNew argument if the form specified
    'in the strForm argument is at the new record, or to the
    'control specified in the strControlElse argument if the
    'form is not at the new record.
        
    'Arguments
    
    'strForm: The name of the form.
    
    'strControlIfNew: The name of the control that is to
    'receive the focus if the form is at the new record.
    
    'strControlElse: The name of the control that is to
    'receive the focus if the form is not at the new record.
    
    'Return Values
    
    'Returns 1 if focus was set to strControlIfNew, 2 if
    'focus was set to strControlElse, 3 if focus was not set.
    
    'Use (from the Current event of a form)
        
    'intResult = FocusSetOnCurrent(strForm:=Me.Name, _
    '   strControlIfNew:="SomeControl", _
    '   strControlElse:="SomeOtherControl" _
    ')
    
    'If the form is at the new record, focus is set to the
    'control "SomeControl", otherwise focus is set to the
    'control "SomeOtherControl.
    
    'Both strControlIfNew and strControlElse are optional.
    'Either can be omitted, if we wish to change the focus
    'only if the form is at the new record, or only if the
    'form is not at the new record. Both can be omitted, and
    'this will not raise any error, but the function would
    'have no effect.
    
On Error GoTo Err_Routine

    Dim lngErrNum As Long
    Dim frm As Form
            
    Set frm = Forms(strForm)
    
    If (frm.NewRecord = True) And _
            (strControlIfNew <> vbNullString) Then
        frm(strControlIfNew).SetFocus
        FocusSetOnCurrent = 1
    ElseIf (frm.NewRecord = False) And _
            (strControlElse <> vbNullString) Then
        frm(strControlElse).SetFocus
        FocusSetOnCurrent = 2
    Else
        FocusSetOnCurrent = 3
    End If
    
Exit_Routine:
    On Error Resume Next
    Set frm = Nothing
    Exit Function
    
Err_Routine:
    lngErrNum = Err.Number
    Select Case Err
        '
    Case Else
        Err.Raise lngErrNum
        Resume Exit_Routine
    End Select
    
End Function

Download this function in plain text format, ready for import into your own Microsoft Access 97 or Microsoft Access 2000 application, here.

home