Strange SetFocus problem (1 Viewer)

ffenton

New member
Local time
Today, 01:02
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?
 
Local time
, 19:02
Joined
Mar 4, 2008
Messages
3,856
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
 

ffenton

New member
Local time
Today, 01:02
Joined
Oct 22, 2009
Messages
7
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.
 

ajetrumpet

Banned
Local time
, 19:02
Joined
Jun 22, 2007
Messages
5,638
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!)
 

ffenton

New member
Local time
Today, 01:02
Joined
Oct 22, 2009
Messages
7
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"
 

ffenton

New member
Local time
Today, 01:02
Joined
Oct 22, 2009
Messages
7
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?
 
Local time
, 19:02
Joined
Mar 4, 2008
Messages
3,856
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.
 

ffenton

New member
Local time
Today, 01:02
Joined
Oct 22, 2009
Messages
7
Unfortunately that didn't work :-(
I'm beginning to think it's a bug in Access ...
 

ffenton

New member
Local time
Today, 01:02
Joined
Oct 22, 2009
Messages
7
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
 

kimba

New member
Local time
, 17:02
Joined
Nov 16, 2012
Messages
2
After all these years, this is still a problem in 2010. Thank you for the solution!
 

Users who are viewing this thread

Top Bottom