Strange SetFocus problem

ffenton

New member
Local time
Today, 18:57
Joined
Oct 22, 2009
Messages
7
(Access 2007)
I have a form with some controls in the header. By default the form detail is hidden. I have the following code on a button

Code:
Private Sub newclient_Click()
Me.Detail.Visible = True
DoCmd.GoToRecord , , acNewRec
Me.surname.SetFocus
End Sub
The problem is that the focus isn't moving to the control surname.
If I remove the line Me.Detail.Visible = True and manually make the detail visible before clicking the button, then surname receives the focus.

I have noticed that if I click anywhere in the background of the detail section after clicking the button, then the cursor appears in the control.
It appears to be a similar problem to the one discussed here http://www.access-programmers.co.uk/forums/showthread.php?t=175507&highlight=setfocus but no one seems to have discovered the reason for the problem.

It's a very annoying little problem but I'm sure that must be a simple solution?
 
This could be a multi-threading problem. If that is true, using "DoEvents" might resolve it:
Code:
Private Sub newclient_Click()
Me.Detail.Visible = True
DoEvents
DoCmd.GoToRecord , , acNewRec
DoEvents 'Try them both to see if one or the other event is the culprit.
Me.surname.SetFocus
End Sub
 
Thanks for the reply.

I've tried that but unfortunately it hasn't solved the problem....

After setting Me.Detail.Visible = True it then goes to the new record but for some reason won't SetFocus.
 
have you tried seetting the focus FIRST instead of last? also, have you tried putting the target focus box at the top of the tab order list? (needs to have a tab stop too!)
 
Changing the tab order makes no difference.
If I do SetFocus first I get the error "Microsoft Office Access can't move the focus to the control name surname"
 
I've added a message box to the end of the code to check which control actually does have the focus - MsgBox(Me.ActiveControl.Name) - and this returns surname. So surname is the active control but for some reason the cursor isn't visible?
 
Hmmmm...I remember something very long ago about this (VB3 I think?). I believe I had to set the "SelStart" property to the end of the text in the box.
 
Unfortunately that didn't work :-(
I'm beginning to think it's a bug in Access ...
 
Finally found a workaround for this!
Before making the detail section visible I've first moved the focus to a contol in the form header.

Code:
Me.Combo120.SetFocus  'control in form header
Me.Detail.Visible = True
DoCmd.GoToRecord , , acNewRec
Me.surname.SetFocus
 
After all these years, this is still a problem in 2010. Thank you for the solution!
 

Users who are viewing this thread

Back
Top Bottom