HasFocus

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wquatan
    New Member
    • Oct 2007
    • 48

    HasFocus

    I have the following :

    Public Sub ObjectFormattin g(ByRef objTarget As Variant)
    SetObjectBorder Width objTarget, IIf(HasFocus(ob jTarget), 2, 0)
    End Sub

    and

    Function HasFocus(objCon trol As Control) As Boolean
    If Screen.ActiveCo ntrol Is objControl Then
    HasFocus = True
    Else
    HasFocus = False
    End If
    End Function

    Both the Sub as the Function are in another module. The object provided to HasFocus resides on a SubForm

    I always get an error 2474 (control must be in active window) on the Screen.ActiveCo ntrol

    What am I overlooking ?

    Thanks
  • puppydogbuddy
    Recognized Expert Top Contributor
    • May 2007
    • 1923

    #2
    Originally posted by wquatan
    I have the following :

    Public Sub ObjectFormattin g(ByRef objTarget As Variant)
    SetObjectBorder Width objTarget, IIf(HasFocus(ob jTarget), 2, 0)
    End Sub

    and

    Function HasFocus(objCon trol As Control) As Boolean
    If Screen.ActiveCo ntrol Is objControl Then
    HasFocus = True
    Else
    HasFocus = False
    End If
    End Function

    Both the Sub as the Function are in another module. The object provided to HasFocus resides on a SubForm

    I always get an error 2474 (control must be in active window) on the Screen.ActiveCo ntrol

    What am I overlooking ?

    Thanks

    Screen.ActiveFo rm and Screen.ActiveCo ntrol work as long as the form or control that has focus is the Main form. It does not work if the control is on the subform. The following link has a function (Set_Screen_Act iveSubformContr ol()) that you can use with a form or subform. http://support.microsoft.com/kb/210210

    Comment

    • wquatan
      New Member
      • Oct 2007
      • 48

      #3
      Originally posted by puppydogbuddy
      Screen.ActiveFo rm and Screen.ActiveCo ntrol work as long as the form or control that has focus is the Main form. It does not work if the control is on the subform. The following link has a function (Set_Screen_Act iveSubformContr ol()) that you can use with a form or subform. http://support.microsoft.com/kb/210210
      Hi

      Thank you very much, seems this is going to be the solution to my problem. Nevertheless I'm somehow confused how to use it. I've created two functions, which are intented to return a True when the active control is identical to the one provided as parameter. But seems not to work. Any idea what might be wrong ?

      Function ReturnScreenAct iveControl(ByRe f objTarget As Variant)
      If Set_Screen_Acti veSubformContro l() = False Then
      ReturnScreenAct iveControl = False
      Else
      ReturnScreenAct iveControl = IIf(Screen.Acti veControl = objTarget, True, False)
      End If
      End Function

      Function ReturnScreenAct iveControl2(ByR ef txbTarget As Access.TextBox)
      If Set_Screen_Acti veSubformContro l() = False Then
      ReturnScreenAct iveControl2 = False
      Else
      ReturnScreenAct iveControl2 = IIf(Screen.Acti veControl = txbTarget, True, False)
      End If
      End Function

      Thanks

      Comment

      • jaxjagfan
        Recognized Expert Contributor
        • Dec 2007
        • 254

        #4
        Originally posted by wquatan
        Hi

        Thank you very much, seems this is going to be the solution to my problem. Nevertheless I'm somehow confused how to use it. I've created two functions, which are intented to return a True when the active control is identical to the one provided as parameter. But seems not to work. Any idea what might be wrong ?

        Function ReturnScreenAct iveControl(ByRe f objTarget As Variant)
        If Set_Screen_Acti veSubformContro l() = False Then
        ReturnScreenAct iveControl = False
        Else
        ReturnScreenAct iveControl = IIf(Screen.Acti veControl = objTarget, True, False)
        End If
        End Function

        Function ReturnScreenAct iveControl2(ByR ef txbTarget As Access.TextBox)
        If Set_Screen_Acti veSubformContro l() = False Then
        ReturnScreenAct iveControl2 = False
        Else
        ReturnScreenAct iveControl2 = IIf(Screen.Acti veControl = txbTarget, True, False)
        End If
        End Function

        Thanks
        What are you trying to do here? What is the overall process? Are you trying to exit a function or perform an action when a control has focus. I don't know if this applies but I check the value of controls before I allow a process to continue.

        If isnull(Me.txt1) then
        Msgbox "You must enter a value!"
        me.txt1.SetFocu s
        exit sub
        Else
        DoMyThing
        End if

        Comment

        Working...