Setfocus tabs to the next field

PRD

Registered User.
Local time
Today, 08:10
Joined
May 18, 2011
Messages
72
Hello, I have a Form with the following four fields - JPGA, JPGB, JPGC and JPGD. The tab sequence for these four fields is 1, 2, 3 and 4 respectively. I use the ‘AfterUpdate’ event procedure to do some data validation and when I find an error I want to be able to focus the cursor back to the field that is in error.

So for example if I validate JPGB and find an error I will code the following statement…

If JPGBerror = “Y” Then
Me.JPGB.Setfocus
Exit Sub
End If

When this statement is executed the cursor goes to JPGC (not JPGB). Also, if I code the following statement as a test…

If JPGBerror = “Y” Then
Me.JPGD.Setfocus
Exit Sub
End If

The cursor will correctly go to JPGD. So my question is, how can I get the Setfocus command to work if I want to tab back to the field in error? Thank you for your time.
 
Perhaps it would be better to validate in the BeforeUpdate event from which the update can be cancelled.
 
I will give it a try first thing Friday morning. Thx!
 
Bob -

Good morning, when I use the BeforeUpdate event I get the following error message..

"Run-time error: '2108'

You must save the field before you execute the GoToControl function or the SetFocus method."

Looks like a Catch-22 - any other ideas? Thx.
 
That's because if you're using the Before Update event, and you Cancel the event, focus never leaves the Control in the first place. Remove the SetFocus completely, as it's unnecessary.
 
Beetle -

Thanks, but if I remove the SetFocus command in the BeforeUpdate event I am back to square one - the cursor tabs to the next field (JPGC) in the event I find an error.
 
Are you cancelling the event?

In other words, your code should look something like;

Code:
Private Sub txtYourTextBox_BeforeUpdate (Cancel As Integer)

'check for your error condition and Cancel the update if needed
If Me!txtYourTextBox <> X Then
   [COLOR="Red"]Cancel = True[/COLOR]
   Msgbox "Invalid data for this field."
End If

End Sub

If you cancel the event then focus will remain in the current control.
 
Beetle -

I did not cancel the event, I will try again first thing Monday. Thx!
 
Beetle -

Sorry for not getting back to you sooner but I just wanted to let you know that the 'cancel' statement in the BeforeUpdate event worked perfectly. Thanks again for your help!
 

Users who are viewing this thread

Back
Top Bottom