469,591 Members | 1,676 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,591 developers. It's quick & easy.

Make a button on a tab control focused so you can hit the Space Bar

I am trying to set the focus on a button that sits on page 1 of my tab control. It looks like I'm successful at setting the focus because I see the highlight on the button, but when I push the space-bar, I get the sub menu for the main form; the button does not press down. I notice that when I click on the tab control, the highlighted button takes on an "added highlight". It looks like a dotted line over the original highlight. If I press the space-bar after this additional highlight appears, the button presses properly. Here is the code I'm using now to set the focus to the button on the tab control...

Me!tabAllTabs.Pages(0).Controls!cmdAddNote.SetFocu s

How do I get that button to the point that I only need to hit the space-bar to press it, without clicking on the tab control?

... and what is this additional state called? I thought that once you set the focus on a button, you needed only press space or enter to engage it.

Nov 24 '09 #1
21 4533
1,287 Expert 1GB
Controls on tabs are referred to the same way as controls on the main form. You should be able to use from main form code:
Expand|Select|Wrap|Line Numbers
  1. cmdAddNote.SetFocus
Nov 24 '09 #2
234 Expert 100+
I just did some research on this, and what I learned is kind of interesting.

First off, though, may I say I'd never seen anyone refer to a control on a tab control page through the controls collection of the page. I would never have even thought of it.

It appears that, when referencing the control through the route described, the control doesn't receive the focus until the page receives the focus, due to the way the reference is called. It's sort of like when you have focus on a control in one form, then open another form. When the second form opens, the control that had the focus in the old form is still selected, but it won't actually get the focus unless that form becomes the active form.

So, by referencing it in that method, you've in essence selected it, but are unable to give it the actual "focus" unless the page is given focus. If you set the focus for the control directly through the form, which is active and has focus, then focus is on that control immediately.
Nov 25 '09 #3
ChipR, Topher23,

Thank you both for the response.

ChipR, I originally tried what you suggested and got the same result; the button was selected but still did not respond to the space-bar as a push.

Your description of how it works sounds right to me, but I still can't get it to work.

I've tried this...
Expand|Select|Wrap|Line Numbers
  1. Me!cmdAddNotes.SetFocus
and this...
Expand|Select|Wrap|Line Numbers
  1. Me!tabAllTabs.Pages(0).Controls!cmdAddNotes.SetFocus
and this...
Expand|Select|Wrap|Line Numbers
  1. Me.SetFocus 'set focus to form first
  2. Me!cmdAddNotes.SetFocus
and this...
Expand|Select|Wrap|Line Numbers
  1. Me!tabAllTabs.Pages(0).SetFocus 'set focus to tab control page 1 first
  2. Me!cmdAddNotes.SetFocus
... all generate the same result; the button is selected, but it will not respond to a "push" from the space-bar or enter-key until the form is actually clicked.

Here's a little more info: I'm trying to set this in an event procedure generated from the Web Browser Control, Document Complete Event...
Expand|Select|Wrap|Line Numbers
  1. Private Sub wbbWebsite_DocumentComplete(ByVal pDisp As Object, URL As Variant)
  2.     Me!txbPageTitle = Me!wbbWebsite.LocationName
  4.     'Me!cmdAddNotes.SetFocus
  6.     'Me!tabAllTabs.Pages(0).Controls!cmdAddNotes.SetFocus
  8.     Me.SetFocus
  9.     Me!cmdAddNotes.SetFocus
  10. End Sub
The results are always the same. The button is selected, but does not respond to the enter button or space bar until I click on the form. I can't get it to work.
Nov 25 '09 #4
1,287 Expert 1GB
Well, I'm stuck. I wanted to try this out with the Web Browser Control, but I can't find it in Access 2007.
Nov 25 '09 #5
904 Expert 512MB
It's there, you are just not looking hard enough :).

Look under the ActiveX components and then Microsoft Web Browser.

You may also need to make a reference to the Microsoft HTML Controls

Nov 25 '09 #6
Ok, apparently this has nothing to do with the tab control. I tried to set focus to a button on the main form and I have the same problem: The button is selected but it does not respond to a push from either the enter key or the space bar. It will only respond after I click on the form.

Thanks for all the help,
Nov 25 '09 #7
904 Expert 512MB
I haven't tried the problem you are having yet but if you do not need to manually press the key why don't you use the sendkey function to simulate pressing the button? Will that be satisfactory for you?

Nov 25 '09 #8
904 Expert 512MB
I just tried the scenario with a main form/ sub form and had same problem. But then I used the sendkeys statement and it worked.

sendkeys "{ENTER}" did the trick for me after setting focus to the button.

Nov 25 '09 #9
1,287 Expert 1GB
I'm still suspicious of the web browser control, but I don't have a Microsoft HTML Controls reference to enable. The Microsoft HTML Objects reference didn't help. Anyone know if there's something I have to download?
Nov 25 '09 #10
234 Expert 100+
This may have something to do with Access being a bit mentally challenged when it comes to moving between containers. I'd guess the focus gets "stuck" when moving between the container and the main form.

SENDKEYS is one option, as mshmyob pointed out, or you could just run the code you need directly from the DocumentComplete event, rather than clicking the button.
Nov 25 '09 #11
Thanks all,

If wanted to call the button's subroutine every time, I would just call the routine right from the Document Complete event. But in this case, the user has to make a decision. Nine out of ten times, they will likely just push the button and move on - so I wanted to make it easy for them and set the focus to the button so they could just hit the space bar and keep going; however, depending on what comes up in the web control browser, they may not want to hit the button and move on.

The form is set up so the user doesn't have to use the mouse too much. These users prefer hot keys.

If I'm going to use the SENDKEYS option (something I don't think I've ever used before because I always set the hot key in the button's caption with the & symbol), I'll probably have to trap the key and prevent actions in cases where I don't want the button to perform any action.

I'll keep playing with it and see what I come up with.

Thank you all for your help.
Nov 25 '09 #12
I just tried:

SendKeys "{Enter}"

It selected the button, then I got a "Conk" noise - indicates that the Enter key did not take.

Is there a way to simulate a click on the form? Something like a Me.MouseClick

I think that would do it.
Nov 25 '09 #13
I'm out of ideas...

Besides a click on the form, nothing will give the buttons on the form "full focus". Not even hitting tab. Setfocus puts my form into a weird kind of "limbo" and the only way to get the form to work is to click it.

If anyone can figure out how to beat this, please let me know.
Nov 25 '09 #14
1,287 Expert 1GB
What is the focus on prior to tyring to do this?
Nov 25 '09 #15
Typically, original focus is on the form...

I have a form with a recordset of URLs. Each time I advance to a new record, the OnCurrent Event triggers:

Me!wbbWebControl.LocationUrl strUrl

wbbWebControl loads the Url and triggers the DocumentComplete event. In the document complete event is:


cmdNotes gets the focus (I see it highlighted) but it will not respond to the enter key, the space-bar, the hotkey in the caption name ("&Notes") and even the tab button will not move the focus. Nothing happens until you click on the form.
Nov 25 '09 #16
234 Expert 100+
Wow, there's very little information about this anywhere. This appears to be somewhat unique to the WebBrowser control. I was able to track down one other similar situtation, which had no resolution. However, a lot of playing with a mockup I made with a WebBrowser control, a button and a text box revealed something interesting. If you pay very close attention to the control that you are trying to set the focus to, you can sometimes see that, for just an instant, it does have the focus - and then the WebBrowser control snatches it. Oddly, the WebBrowser control doesn't put the focus on the web page, but just leaves it hanging in limbo in the ActiveX container. Experimenting with different control properties was fruitless. I'm starting to think that MS had some reason for programming it this way, they simply failed to tell anyone what that reason was. I suppose they would call it a "feature."
Nov 25 '09 #17
904 Expert 512MB
I have also looked around online and see that it is a problem for many but nobody has given a solution to the problem.

One thing that you need to watch is that your DocumentComplete event may be triggered many times if a site has frames. This event appears to be triggered for each frame. You would also need to use the ReadyState to determine when a site has fully loaded.

I'll keep fooling around and see what I can come up with if anything.

Nov 25 '09 #18
234 Expert 100+
Very interesting. I'm posting from the WebBrowser object in my Access app right now. Kind of fun. Anyway, it turns out the WebBrowser object is, in fact, giving itself focus. The object itself is an instance of an outside program, so when it steals the focus, the last focus given control in Access still has focus in Access. At this point, you're not in Access anymore - you're in an instance of the WebBrowser ActiveX object. Web browsers are primarily mouse-based, which may be the cause of the problem we're having here. Until you click on something on the web page that's displayed, nothing will have focus, so it seems like the focus has "vanished." However, it appears the only way to take the focus back to Access from the WebBrowser control is also through a mouse-click. Also, the Enter key doesn't seem to do anything inside the WebBrowser control, which is why this is all in one great block.
Nov 25 '09 #19
Well, I'm exhausted. I've tried to see if there was a way to send a mouse click through sendkeys, and I found some stuff on it, but nothing beat this problem. Topher23, I did see the button get full focus, but then get pulled back as you described. It leads me to believe that the web browser control might have some other event after the documentComplete that is pulling the focus back. I've tried to figure out what that event might be, but no luck.

This one has me stumped.
Nov 27 '09 #20
32,198 Expert Mod 16PB
I don't have one to play with Adam, but have you tried (in code) selecting, first the main form, then (if you have one involved) the subform, then the page and lastly, the control. Topher's earlier explanation is why this is an issue I'm sure. I hope that this sequence will be a workable solution.

I just finished catching up on all the recent posts which indicate this may not work. May be worth a try, but doubtful if I'm honest :(
Nov 29 '09 #21

Thanks, but I've tried all kinds of games with it and nothing has worked. I tried what you suggested and that didn't work. I came close when I set the web control enable=false immediately after I set the focus to the button. The button did get the proper focus, but the entire form froze. :(

It's time for me to let this issue go. Thanks for looking into it.
Nov 29 '09 #22

Post your reply

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

Similar topics

3 posts views Thread by Jason Kyle Baginski | last post: by
2 posts views Thread by David Cho | last post: by
3 posts views Thread by Boni | last post: by
reply views Thread by gnewsgroup | last post: by
3 posts views Thread by pentahari | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.