Full Version: .SetFocus focusing somewhere else
UtterAccess Forums > Microsoft® Access > Access Forms
Mike11
Hi UA
Any idea why the red code below doesn't do what I thought it should. The focus ends up in the first control of the form rather than the TimeControlname control
......
Private sub TimeControlName_AfertUpdate()
If...
If....
Else
Me.TimeControlName = "00:00"
Me.TimeControlName.SetFocus

End if
End if
End sub
.........
HAs usual, thanks
khaos
Can you set focus to that control manually? Just wondering if it is locked or something.
en
Mike11
The control is not locked and I can click into it no problem.
The piece of code does actually reset the control to zero, but the focus shoots off to the first control after that.
ChrisO
The AfterUpdate event occurs before the LostFocus event so setting the focus to itself does nothing.
You can set the focus to another control and then back to itself as in: -
CODE
Private Sub TimeControlName_AfterUpdate()

    Me.txtDummySetFocus.SetFocus
    Me.TimeControlName.SetFocus
    
End Sub

Hope that helps.
Regards,
Chris.
Mike11
Thanks. worked a treat.
ChrisO
No problems.
ften it’s handy to have a dummy text box on the form for just that purpose.
CODE
Private Sub Form_Open(ByRef intCancel As Integer)

    Me.txtDummySetFocus.Top = 0
    Me.txtDummySetFocus.Left = 0
    Me.txtDummySetFocus.Width = 0
    Me.txtDummySetFocus.Height = 0
    Me.txtDummySetFocus.Visible = True
    Me.txtDummySetFocus.TabStop = False
    
End Sub

Glad it worked.
Regards,
Chris.
Mike11
Just wondering..in my code I make a message box pop up and say "Are you sure this took over an hours to do?".
The code is triggered if TimeControlName > "00:59".
How do I get the message box to show "Are you sure this took " & TimecontrolName & "", so I can be more precise in drawing their attention to something that should of taken 20 minutes and not 20 hours.
I'm searching around some posts, and I'm under the impression that I have to declare TimeControlName as a string???
khaos
A better suggestion for this is a transparent command button with no code. It can receive focus, will never show and is easier to find in design view. I used to do what you do. Major pain is what I thought of that.
TH
Ken
ChrisO
I can’t see why that would not work, you might like to prefix TimeControlName with Me. but it will work without it.
sgBox "Are you sure this took " & Me.TimeControlName
Hope that helps.
Regards,
Chris.
ChrisO
Plenty of ways to skin a cat and always good to know more than one. frown.gif
egards,
Chris.
Mike11
Thanks worked fine.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.