Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set focus / gotocontrol not woking properly in 2k 2

Status
Not open for further replies.

hwkranger

MIS
Nov 6, 2002
717
US
I'm very experienced in access.

I'm having some serious issues with the .setfocus and docmd.gotocontrol methods.

Here's my code:

Private Sub OperatorID_AfterUpdate()
'Operator.OperatorID
If DCount("OperatorID", "tbloperator", "OperatorID = " & "'" & Me.OperatorID & "'") = 0 Then
MsgBox ("This is not a valid Operator ID!")
'DoCmd.GoToControl ("OperatorID")
'Me.OperatorID.SetFocus
Forms!Operator!OperatorID.SetFocus

Me.OperatorID = ""
End If

End Sub

it's acting as if the setfocus or docmd's aren't even there. As you can see i've tried it 3 different ways (2 of them are commented out) and it doesn't work at all.


Randall Vollen
National City Bank Corp.

Just because you have an answer - doesn't mean it's the best answer.
 
Oddly Enough, I solved my own problem.

The question is WHY did I have to do it this way:

I had to set focus to another control, then bring the focus back. Does that make any sense?



Randall Vollen
National City Bank Corp.

Just because you have an answer - doesn't mean it's the best answer.
 
Randall, I suggest you try something else (since transfering focus to another control then right back is a bit kludgy). Make this a BeforeUpdate event instead of an AfterUpdate event. It would look something like this:

Private Sub OperatorID_BeforeUpdate(Cancel As Integer)
'Operator.OperatorID
If DCount("OperatorID", "tbloperator", "OperatorID = " & "'" & Me.OperatorID & "'") = 0 Then
MsgBox ("This is not a valid Operator ID!")
Cancel = True
OperatorID.Undo
End If
End Sub

If DCount = 0, you should get the MsgBox, but the cursor should stay in the OperatorID control. Ergo there's no need to SetFocus to another control then SetFocus right back to OperatorID.

Just a suggestion. Of course if it ain't broke, don't fix it.
 
Hello DBelsey,
This is a great solution!
I had the same problem and was just going to put a question on this forum. Your answer is just what I needed.
Thanks very much.
 
Hi hwkranger,

dbelsey's solution is the right one for you; validation should normally go in the BeforeUpdate event - it's technically too late After the update. But let me try and explain as best I can.

Following any input (keystroke, mouseclick, whatever) there are a series of events which, in effect, go onto an event stack. Updating of a control happens when you leave that control and go somewhere else. At this point you are (amongst other things) Updating the control, Exiting the control, Losing the Focus from the control, Entering another control, and Getting the Focus on another control.

With particular relevance to your situation, at the time the AfterUpdate event code runs the control being updated still has the Focus so setting the focus to it changes nothing. After the code has run a later event on the stack sets the Focus to some other control (as determined by the input) and this makes it APPEAR that your SetFocus has not worked.

AFAIK there is no direct way to control the event stack. One indirect way, as you have discovered, is to artificially set the focus to another control and then back again but if you find a need to do this it usually means your code is in the wrong place and, as already stated, in your case you should be coding in the BeforeUpdate event, where returning Cancel=True stops further processing and clears the event stack.

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top