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

Another SetFocus Question - SetFocus from SubForm

P: 675
This should be so, so simple I hesitate to ask.

I have a Form named "fNavButtons" used as a SubForm in control named "subformNavButtons". subformNavButtons is a control on my main form named "fAAA".

fNavButtons has 2 CommandButtons, cmdNextRec and cmdPrevRec. These are on a subform to allow an AutoRepeat to continue when form fAAA gets another record.

If I am at the first record, I want cmdPrevRec.Enabled = False, and if I am at the last record, cmdNextRec.Enabled = False. Otherwise the command buttons are .Enabled = True.

The problem is that with filters I can have one record, and therefore both need to be .Enabled = False. I can't seem to assign .Enabled = False for both simultaneously. This would mean the subform has no control enabled. I get run-time error 2164 - You can't disable a control while it has the focus on the 3rd line of the code below.
Expand|Select|Wrap|Line Numbers
  1. . . . 
  2. Forms!fAAA.SetFocus
  3. Forms!fAAA!txtTitleName.SetFocus
  4. Forms!fAAA!subformNavButtons.Form!cmdNextRec.Enabled = False
The above lines of code should move the focus to the main form fAAA and set the focus to a textbox named "txtTitleName". This should allow line 4 of code to execute, as the subform does not have the focus. It is line 4 of code where the error occurs. "" says I'm doing the correctly, but I'm too close to it and cannot see my error.

If I remove the 4th line of code, the focus is actually at txtTitleName, and keyboard entries go to that control. Good, but I still have an enabled control that should be .Enabled = False
May 26 '07 #1
Share this Question
Share on Google+
8 Replies

Expert Mod 10K+
P: 14,534
Have a look at this thread and see if it helps you out.
May 26 '07 #2

Expert 2.5K+
P: 3,072
Hmm, why not add a << (First) and >> (Last) button that can be enabled always and ease the scrolling perhaps too ?

May 26 '07 #3

P: 675
I saw this thread. I am trying to also create my own navigation buttons, but in this case only the "Next" and "Previous" buttons, not the First, Last, or NewRecord. I have no trouble writing this code.

These 2 command buttons are on a subform. This is to allow me to hold the buttons down, and have the AutoRepeat = True. This cannot be done if the buttons are on the form bound to a recordset. I can detect the first or last record, and set cmdNextRec.Enabled = True/False. Similarly with cmdPrevRec.
Expand|Select|Wrap|Line Numbers
  1. If boolStart Then 'I am at first record
  2.     cmdNextRec.SetFocus
  3.     cmdPrevRec.Enabled = False
  4. End If
  5. If BoolEnd Then 'I am at last record
  6.     cmdPrevRec.SetFocus
  7.     cmdNextRec.Enabled = False
  8. End If
If I apply a filter that only has one record, so boolStart = True and boolEnd = True. The above code will fail because line 6 cannot SetFocus to a control that .Enabled = False. If I comment out line 6, line 7 fails because I cannot set .Enabled = False if the object has the focus.

There are no other controls on this subform to accept the focus. If I set the focus to a control on the main form, fAAA, I still cannot set both command buttons on the subform with .Enabled = False. Apparently the subform focus is independent of the main form. Although the main form receives the keystrokes, there must be something to have the focus if I return to the subform.

I would like the subform itself to have the focus if both commands are not enabled. But I cannot set the focus to the subform if any control on the subform can have the focus, and I cannot set enabled to False for both commands if the subform does not have the focus. Catch 22!
May 26 '07 #4

P: 675
Response to Nico:

Adding a cmdFirst and a cmdLast button makes no difference, they too would have to both be .Enabled = False. If I am at the last record, there should not be an enabled button that allows me to go to the last record or the first record. I'm already there.

I have this grevience against programs that have buttons that then give me a MsgBox telling me I can't do what the button says it does. MsgBox is to respond to an error that cannot be prevented, such as "Enter a number between 2 and 4" and the user enters a 5. Also, I don't like to push a button and have nothing happen. Makes me think the response is slow, or the computer is hung up somehow.

Not sure how two new buttons reduce scrolling.
May 26 '07 #5

Expert 2.5K+
P: 3,072
Well, when there are more than 2 records the First and Last button will effectively allow a user faster access to the last/first record.
The First and Last buttons never have to be disabled, as they are appropriate (however useless) when only one record has been found. It's however a solution to the problem that a form needs at least one active control that can hold the focus. The alternative would be a dummy control and I think that the First and Last buttons at least will add functionality to your subform :-)

Personally I never use a setup like this. I prefer to show the user all rows in a datasheet subform and have him use the right-click popup menu to filter the rows. Besides such a datasheet subform I offer a set of buttons with the basic actions like [Add], [Update], [Delete], [Print], etc.

May 27 '07 #6

P: 675
I am going to close this thread. Access Help for SetFocus Method implies that the form can have the focus if there is no control able to have the focus. Apparently true only if there are no controls on the form when loaded.

I had tried to strip all extraneous information from the problem before I presented it to this forum. I think it is easier to understand a simple piece of code rather than seeing the entire program. Whether I should or should not have a Delete Command is not a part of the question. I am inferring from the answer that the answer is that I cannot do what I want.

Thank you for your time and thoughts in attempting to answer this question.

Old Birdman
May 30 '07 #7

Expert 2.5K+
P: 2,653
You are right thais is really too simple to ask.
Simply add the third control to your navigation subform. This can be for example blank option group without label which looks like frame and call SetFocus method for this control before disabling buttons.

Example of working code:

Private Sub btn1_Click()
Me.btn1.Enabled = False
End Sub

Private Sub btn2_Click()
Me.btn2.Enabled = False
End Sub

The form contains 3 controls: 2 buttons (btn1, btn2) placed in the frame of option group (Frame2). Click on the button disables it. All two buttons may be disabled without getting error message.

The problems with dummy control are the folowing:
1) not all controls can actually receive focus (e.g. Label, Image etc)
2) the control has to be visible to receive focus.

Option group frame control does fit these needs or you should use any custom control.

Good luck.
Jun 3 '07 #8

P: 675
This is a very interesting solution.

Although Access won't let me set the focus to a control with .Visible = False, it will allow a frame to get the focus, even if that frame has no controls (option buttons, check boxes, or toggle buttons) within it. Furthermore, it will allow .BorderStyle = Transparent, making it not visible.

I can find no unpredictable or unacceptable results to this solution. Nothing is highlighted, there is no indication about which control has the focus. Random mouse clicks in the vicinity of the invisible control will eventually hit this control, but Access responds as if the form itself were selected.

Don't know why I didn't think of this. I just knew that there was a SIMPLE solution out there somewhere.

Thank you for your solution.

Jun 7 '07 #9

Post your reply

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