Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
HELP Button Loses Focus
Dustin Barnes 
   
3 years ago
Hello All! I am stumped on this one. As simple as it will sound, I do not understand it.

I have a Main form [frmMain] which has a Subform [frmSub]. [frmSub] as a KeyDown event that monitors the TAB key. Within the KeyDown event, I establish a RecordsetClone. I then compare [frmSub]'s CurrentRecord to the RecordsetClone's record with each press of the TAB key. If I am on the last record and press the TAB key, I want to set the focus to a button [btnRegister] located on [frmMain].

If I single step the code, it works great, the issue is, once the Keydown event executes End Sub, my textbox control [txtFirstName] receives the focus. I DO NOT tell any part of any of my code to set the focus to the [txtFirstName] control. I have munipulated the code in various ways to figureout why the button loses focus and the textbox gets the focus but just left scratching my head. HELP!!

DetailsPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   Dim db As Database
   Dim rs As Recordset

   Set db = CurrentDb
   Set rs = Me.RecordsetClone

   If KeyCode = 9 Then
      If Me.CurrentRecord = rs.RecordCount Then
'         Forms!frmAdminNewUser.Controls!txtFirstName.SetFocus
            Me.Parent!btnRegister.SetFocus
      End If
   End If

   rs.Close
   db.Close
   Set rs = Nothing
   Set db = Nothing
End Sub
Dustin Barnes OP  @Reply  
   
3 years ago
Let me note, if I changethe SetFocus command to Me.Parent!txtFirstName.SetFocus, then [txtFirstName] loses focus and the next textbox, [txtLastName] receives the focus!?!?!?
Kevin Robertson  @Reply  
          
3 years ago
This line?

Forms!frmAdminNewUser.Controls!txtFirstName.SetFocus
Dustin Barnes OP  @Reply  
   
3 years ago
Yes I know, I do not need the database object.... just habit.
Dustin Barnes OP  @Reply  
   
3 years ago
Hey Kevin, thanks for the quick response. Let me clean-up my code. The Forms!frmAdminNewUser.Controls!txtFirstName.SetFocus is commented out in the code I provided, so it doesn't execute. Was using it in my debugging. Revised code below works but once it exits the sub, [txtFirstName] receives the focus. I have searched all of my code and I do not set focus to the [txtFirstName] control anywhere. As mentioned, if I changed the SetFocus line to Me.Parent!txtFirstName.SetFocus, then once the Keydown sub exits, [txtLastName] gets the focus.

DetailsPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   Dim rs As Recordset
   Set rs = Me.RecordsetClone

   If KeyCode = 9 Then
      If Me.CurrentRecord = rs.RecordCount Then
            Me.Parent!btnRegister.SetFocus
      End If
   End If

   rs.Close
   Set rs = Nothing
End Sub
Dustin Barnes OP  @Reply  
   
3 years ago
If I set the SetFocus line to Me.Parent!txtLastName.SetFocus, then my subForm receives the focus, therefore, I never leave the subForm. If I set the subForm to receive the focus on this event, then again, I never leave the subForm. No matter which control I pick, after the Keydown event exits, a different control than the one specified (unless the subform) gets the focus.
Dustin Barnes OP  @Reply  
   
3 years ago
If I disable all of the textboxes on [frmMain], meaning all I have left are buttons and the subForm, then after the button specified receives the focus and the Keydown event ends, the subForm gets the focus again. I wonder if this is a bug with Access M365?
Dustin Barnes OP  @Reply  
   
3 years ago
I am stupid!

I forgot to throw away the key after setting the focus.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   Dim rs As Recordset
   Set rs = Me.RecordsetClone

   If KeyCode = 9 Then
      If Me.CurrentRecord = rs.RecordCount Then
            Me.Parent!btnRegister.SetFocus
            KeyCode = 0 '<~~~~Don't forget *facepalm*
      End If
   End If

   rs.Close
   Set rs = Nothing
End Sub

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/6/2026 12:39:42 PM. PLT: 1s