I have a form with two subforms. I need to hide some controls on the subforms with code in the On Click event of a command button on the parent form. My problem is that if a control on a subform that I need to hide has the focus when I click the command button I get the error 2165: You can't hide a control that has the focus. I've tried all sorts of different control.Setfocus commands to try to set the focus to other controls on the subform, and on the parent form. No matter what the focus always evidently always stays with the last control that had the focus on the subform before I click the cmd buton. This even happens when my first lne of code in the cmd button's On_Click event sets the focus to the cmdButton I just clicked, and that control even shows the focus outline box, but I still get the error. What am I missing? I'm using Access 2000.
I sure would appreciate hearing any thoughts anyone may have.
I was stuck on a similar problem for a while, too.
The solution I came up with was to place a textbox in the page header of the form (go to View | page Header/Footer to see this). I named mine txtFocusCapture. Then, before you do any disabling of controls in the subform, call a txtFocusCapture.SetFocus.
The page header and footer will not display with the form, so your focus is removed from any visible controls, allowing you to set visible/enabled states.
Thanks Mark. Yes, your solution definitely works, and I have another set of forms where I will need to use your solution. In this particular case though I do have some controls on the subform that don't get hidden, so I will just move the focus to one of those before the code to hide the other controls runs.
Thanks again for your reply, it was much appreciated.