Full Version: Can you easily see if a control has the focus?
UtterAccess Forums > Microsoft® Access > Access Forms
bnelson333
I have a routine that runs through every control on the form and either locks/unlocks and enables/disables it based on the user's security level within the database.
Everything works great for fields, except I also want to do the same with command buttons.
It works fine most of the time, but if the routine tries to lock a command button when it has the focus, Access freaks. This doesn't work because one of the buttons is the "submit" button, and if they submit it, and it checks out, it gets further locked down from editing and I want the submit button to be disabled (.enabled=false) so it grays out.
What I'd really like to do is something like:
If MyControl.HasFocus Then '<---- This is the line I don't know what to do with
MyOtherControl.SetFocus
MyControl.Enabled=False
MyControl.SetFocus
Else
MyControl.Enabled=False
End If
Yes, I could easily just always set the focus to another field, make the change, then come back. But this SetPermissions routine is pretty global within the form, it can run under many different situations, not always when the SubmitButton_Click event is run, so I don't always want to refocus on that button.
Any thoughts?
Doug Steele
Access has two built-in properties of the Screen object called ActiveControl and PreviousControl. You can use
!--c1-->
CODE
  If Screen.ActiveControl.Name = "MyControl" Then
    Me.MyOtherControl.SetFocus
    Me.MyControl.Enabled=False
    Me.MyControl.SetFocus
  Else
    Me.MyControl.Enabled=False
  End If
bnelson333
Awesome, thanks!!!
bnelson333
Sorry, looked good on paper, didn't actually work.
get the following message:
The expression you entered requires the control to be in the active window.
It only errors when the form is loaded, otherwise it does return the active control name. SetPermissions runs in the form_current event (which runs right when the form is loaded). It's like Access runs the code before the form is actually loaded, so it doesn't know which control has the focus?
Any thoughts?
GustiAllah
Hi Arokh,...
The point is: Acces can't set disable or invisible to control that already has focus. So before set invisible/disable, set focus to another control. I usually add "additional unused" control with the same name in all of the form, (+Tabfocus=no, heigh/weight =0). so I always set focus to that add control before doing invisible/disable.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.