Test if a particular field has got focus

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LSteve
    New Member
    • Mar 2008
    • 4

    Test if a particular field has got focus

    Hi All
    Is there any way to test if a form field has got focus. I have a button that runs code to filters by selection, but if any other field other than the field [MthFwd] is selected i want a message box to appear and tell the user to select a month from the MthFwd field before running the filter. Here is my code:
    Code:
    Private Sub cmdFBSelection_Click()
    
         Screen.PreviousControl.SetFocus 
    
         'Code to check if MthFwd has focus. If not  then msbbox and exit sub
    
         DoCmd.RunCommand acCmdFilterBySelection 'Runs filter
    
    
    End Sub
    Thanks in advance for any help

    LSteve
    Last edited by NeoPa; Apr 11 '08, 11:20 PM. Reason: Please use [CODE] tags
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi. One way to do what you need to do is to check the name of the control returned by Screen.Previous Control.
    [code=vb]Private Sub cmdFBSelection_ Click()
    Dim Ctrl as Control
    Set Ctrl = Screen.Previous Control
    If Ctrl.Name <> "Mthfwd" then
    MsgBox "your message"
    Ctrl.Setfocus
    Exit Sub
    end if
    DoCmd.RunComman d acCmdFilterBySe lection 'Runs filter
    End Sub[/code]
    I am not convinced that this is the right way to go as all it shows is that the last control that had focus was the one mentioned. It does not show that a value has been updated. If you need to ensure that a value has been updated you could use the After Update event for that control to set a value accordingly, then test for this value in your selection code.

    As an example, you could set the control's tag property (which is normally unused) to a text value in the control's After Update event code then test for that value in your selection click routine:
    [code=vb]Private Sub MthFwd_AfterUpd ate()
    Me!MthFwd.Tag = "Updated"
    End Sub[/code]

    [code=vb]Private Sub cmdFBSelection_ Click()
    If Me!Mthfwd.Tag <> "Updated" then
    MsgBox "your message"
    Me!MthFwd.Setfo cus
    Exit Sub
    end if
    DoCmd.RunComman d acCmdFilterBySe lection 'Runs filter
    End Sub[/code]

    Hope these ideas help.

    -Stewart

    Comment

    • Stewart Ross
      Recognized Expert Moderator Specialist
      • Feb 2008
      • 2545

      #3
      I had a further thought: if all you need to do is to make sure there is a value in Mthfwd to filter upon, why not just test for Mthfwd null?

      [code=vb]If IsNull(Me!Mthfw d) then
      msgbox "no value selected"
      Me!Mthfwd.SetFo cus
      Exit Sub
      End If[/code]
      -Stewart

      Comment

      • LSteve
        New Member
        • Mar 2008
        • 4

        #4
        Thanks Stuart for your help. I now have it working by using your suggestion of using the tag property. When MthFwd had focus i set the tag to a value of 1. when it lost focus it was set to a value of 0. This value is then used to run the relavent code.
        There is so much to learn for us newbies.

        Thanks again
        LSteve

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32662

          #5
          In a form, Me.ActiveContro l is the one with the focus. If the one you want to test has a .Name that is different, then it's not got the focus.

          Comment

          Working...