MS Access Tab Control: Wrong focus
I have a tab control with different pages. When starting up the form with this tab control the tabs get lost and the inner page gets all the screen focus. The tab control is used for开发者_如何学Python navigation so the user will get lost this way.
Is there any way to let the tabs be visible on the screen without just resizing the screen to be smaller?
Desired result:
+--------------------+
| Tab1 | Tab2 | Tab3 |
+--------------------+
| Name: ______ |
Actual screen:
^
+--------------------+ |_|
| Name: ______ | | |
Reducing the size of the tab control works (as you discovered yourself), however there is an alternative workaround:
- Add a command button and align it with the top left corner of the tab control
- Set its Tab Stop property to No (in the 'Other' tab of the command button property sheet)
- Send To Back to put the command button behind the tab control
- In the Form's
OnOpen
orOnLoad
event, call the.SetFocus
method on the command button - Use
SendKeys
(I know, I know...) to tab to the Tab Control
Sample code:
Private Sub Form_Open(Cancel As Integer)
Me.HiddenCommandButton.SetFocus
SendKeys "{Tab}"
End Sub
Explanation of the above steps:
- This allows you to dictate to Access where it should line up the form on screen.
- Setting the Tab Stop to No for the command button prevents users from accidentally tabbing to it and causing confusion.
- Sending the button to the back hides it from the user and prevents it from interfering with any mouse clicking.
- Setting focus to the command button at form start up is necessary since we turned off the Tab Stop property in step 2.
- Using SendKeys (always a last resort, and for good reason) to simulate a tab press provides "keyboard focus" to the Tab Control (as long as the tab control is the first control in the tab order for whatever section of the form the control is a part of).
Miscellaneous Notes: At the asker's request I am including a couple of comments as part of the answer itself:
@mwolfe: One final note on SendKeys...it blows up under UAC in Vista/Win7. If you can live without the keyboard focus, I'd say leave SendKeys out entirely. If you need it you'll either want to add error handling to ignore the error (if you don't mind some of your users losing keyboard focus functionality) or bypass SendKeys and use the WinAPI directly. Karl Peterson offers a turnkey solution here: vb.mvps.org/samples/SendInput I have never used it so can't comment on its reliability, but Karl is a Microsoft MVP so he's get some credibility.
@Roman Glass: mwolfe02 I trust you in that this method will work, but the focus is crucial for me AND some users are working under Windows 7. I will drop this issue for the moment to find out about the user reactions. Nevertheless I think your solution deserves a solved. In the end I have to talk with the WinAPI. Maybe you can edit your answer to include this comment directly. Thanks!
For those who may find this answer in the future, please take note that Step 5 above is only necessary if you need the tab control to receive keyboard focus (a critical requirement for the original asker). If you can live without it, I would suggest that you do.
UPDATE: As David Fenton points out in the comments, you can use Ctl
+ Tab
/Ctl
+ Shift
+ Tab
to move back and forth between the tabs. This is consistent with most tabbed interfaces (eg, browsers) and eliminates the need to use SendKeys
.
Hmmm, I know I've only played with the Tab control once or twice - but never had a problem with it showing up on the form when the form is executed/loaded. Granted, I've never had it in any place except the Detail section of the form, but I'd say check to see if the beast has somehow gotten it's Enabled or Visible property (if applicable) set to No. Of course, I could be wrong - it happens, and I am eager to learn something new when it happens. :)
I don't know that I understand the question, but in a comment, you say that Access doesn't allow you to set focus to a tab control, but you can set focus to the tab page:
Me!ctlTab.Pages(N).SetFocus
...where N is the index of the tab page you want to set focus to), or with:
Me!pgeTabPageName.SetFocus
Both of these work to set focus to the tab page.
You say you want to set focus to the tab control, but I can't figure out why you'd want to do that. The tab control itself has no elements that can get focus -- only the members of its Pages collection do.
Again, I would suggest that you've painted yourself into a corner for some other reason and there's no solution. Therefore, you have to figure out how to avoid getting into that insoluble problem. I simply don't understand your explanation of the problem well enough to be able to offer a solution, but I doubt there's no way to resolve the issue through a different approach.
精彩评论