By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,321 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

Test if a particular field has got focus

P: 4
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdFBSelection_Click()
  2.  
  3.      Screen.PreviousControl.SetFocus 
  4.  
  5.      'Code to check if MthFwd has focus. If not  then msbbox and exit sub
  6.  
  7.      DoCmd.RunCommand acCmdFilterBySelection 'Runs filter
  8.  
  9.  
  10. End Sub
Thanks in advance for any help

LSteve
Apr 10 '08 #1
Share this Question
Share on Google+
4 Replies


Expert Mod 2.5K+
P: 2,545
Hi. One way to do what you need to do is to check the name of the control returned by Screen.PreviousControl.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdFBSelection_Click()
  2. Dim Ctrl as Control
  3. Set Ctrl = Screen.PreviousControl
  4. If Ctrl.Name <> "Mthfwd" then
  5.   MsgBox "your message"
  6.   Ctrl.Setfocus
  7.   Exit Sub
  8. end if
  9. DoCmd.RunCommand acCmdFilterBySelection 'Runs filter
  10. End Sub
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub MthFwd_AfterUpdate()
  2.   Me!MthFwd.Tag = "Updated"
  3. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdFBSelection_Click()
  2. If Me!Mthfwd.Tag <> "Updated" then
  3.   MsgBox "your message"
  4.   Me!MthFwd.Setfocus
  5.   Exit Sub
  6. end if
  7. DoCmd.RunCommand acCmdFilterBySelection 'Runs filter
  8. End Sub
Hope these ideas help.

-Stewart
Apr 10 '08 #2

Expert Mod 2.5K+
P: 2,545
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?

Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me!Mthfwd) then
  2.   msgbox "no value selected"
  3.   Me!Mthfwd.SetFocus
  4.   Exit Sub
  5. End If
-Stewart
Apr 10 '08 #3

P: 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
Apr 10 '08 #4

NeoPa
Expert Mod 15k+
P: 31,492
In a form, Me.ActiveControl 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.
Apr 11 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.