 |
| Access VBA Discuss using VBA for Access programming. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 26th, 2005, 04:57 PM
|
|
Registered User
|
|
Join Date: May 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Can't move focus to control <control
In an MS Access 2003 app, when a user finishes editing a subform named subfrmContactDetail, and tries to click anywhere outsideof the the subform, I want to save the data (if desired) and then move focus to another subform (subfrmContactGrid).
The code in Before Update event is shown below. After the 'Do you want to save changes' dialog, I get the error that the application cannot change focus to the control subfrmContactGrid.
This happens no matter what control I try to change focus to. I have ensured that the controls are enabled, unlocked and visible.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("Do you want to save your changes?", vbQuestion + vbYesNo, "Confirm") = vbNo Then
Cancel = True
DoCmd.RunCommand acCmdUndo
End If
Forms("frmMain").Controls("subfrmContactGrid").Set Focus
End Sub
Any help is appreciated. I do not no how to attach the small mdb file, else I would.
|
|

May 27th, 2005, 08:10 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Generally, you cannot move focus to a control that is disabled. So you've checked this.
Are you spelling subfrmContactGrid correctly?
Assuming the BeforeUpdate is in frmMain, why don't you try:
Me.subfrmContactGrid.SetFocus
Another possibility is that Access is trying to process the Undo while you're trying to move focus on the form. You might try putting DoEvents after your undo.
If the BeforeUpdate is not in frmMain, you may have to activate frmMain before you can SetFocus to a control on it. I'm not sure how to do that, but you might try:
Forms("frmMain").SetFocus
Forms("frmMain").Controls("subfrmContactGrid").Set Focus
Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
|
|

May 27th, 2005, 08:17 AM
|
|
Registered User
|
|
Join Date: May 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
rjweers: Thank you for your help.
I have tried all the things you've stated, the end result is still the same.
The object name is spelled correctly (otherwise Access gives a different error), and I've even tried to put just the setfocus commands in the Event proc, for good measure.
|
|

May 28th, 2005, 09:50 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
|
|
I'm sorry. I wasn't paying attention to what you were doing.
You can't set focus to a subform. You have to set focus to a field in the subform. On top of that, setting focus to a field in a subform doesn't work.
Here's an ugly solution for you.
Create a text field on the master form (You can make the field size 0,0 so nobody sees it. And I would put it close to the subform so if anyone tabs to it they don't notice the cursor movement.) Make sure the table order is the text field, then subfrmContactGrid. On the GotFocus event of the text field, SendKeys "{Tab}". Then in subformContactDetail after your update, Me.Parent.textfield.SetFocus.
This will get the cursor to the first field in the subform.
Unfortunately, SendKeys does some strange things with NumLock. So if your user has something that displays on the screen to indicate they've hit the numlock key (which I do) or has an application that audio reports a change (which I do - sMaRTcaPs), they know something is going on in your application. Users (like me) are likely to get a bit peeved at you toggling their NumLock.
I don't know when SendKeys developed that side effect. But I sure wish MS would fix it.
|
|

June 2nd, 2005, 03:47 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
I'm a bit at a loss with your code and question. If your subform is bound to the table, then you don't have to do anything. The record will be automatically saved once you move off the subform.
If your subform is UNbound, nowhere in your code do I see a command to save the record. On the Subform's afterupdate event,
Code:
Private Sub Form_AfterUpdate()
If MsgBox("Do you want to save your changes?", vbQuestion + vbYesNo, "Confirm") = vbYes Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
Else
Cancel = True
DoCmd.RunCommand acCmdUndo
End If
Me.Parent.Form.subfrmContactGrid.SetFocus
End Sub
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|
|

June 20th, 2006, 09:18 PM
|
|
Registered User
|
|
Join Date: Jun 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
There are two quite annoying bugs in Access 2003. If you want to set the focus on a control, you must first set the focus to another field on the form, I usually use the prior field, and then code to set the focus to the field that you really want. This is my example code:
Me!MINREQFLAG.SetFocus
Me!EmailAddress.SetFocus
Now I found out that the me.undo command does not work. My solution for that is this code:
DoCmd.RunCommand acCmdUndo
Karen S. Long
MarkXX Consulting, LLC
103 Clearview Drive
McMurray, PA 15317
(724)942-4831
|
|

June 21st, 2006, 07:36 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
Quote:
|
quote:There are two quite annoying bugs in Access 2003. If you want to set the focus on a control, you must first set the focus to another field on the form
|
Really? I have never encountered this. The situation I encounter is that if you want to disable a control or render it invisible, you must set focus elsewhere before you do that or you'll get an error message. But I never have encountered what you described.
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|
|

September 19th, 2006, 05:24 AM
|
|
Registered User
|
|
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Had this problem and found that it occured when I had a 'GotFocus' event on the control that I was trying to SetFocus to. Don't know if this was just because the GotFocus event was re-opening the form that was being closed after the SetFocus Event (Calendar popup fired on textbox GotFocus event and when date clicked tried to SetFocus back to the TextBox to close calendar form when error occured).
|
|

October 26th, 2007, 12:16 PM
|
|
Registered User
|
|
Join Date: Oct 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you for your contributions to this thread. In my case the field was disabled. I am working on code for a customer which I've maintained here and there but don't know it as well as I would if it were my own. Depending if the user logging in was admin or not, the problematic field was either disabled or enabled but the code was trying to SetFocus to it regardless. Thanks for quick access to the cause of the problem!
Marlene
|
|
 |