Set focus

P

PeterM

I'm having problems with AC2003. I have code for the AfterUpdate of a text
field. In there I...

If Not Me.txtCostCenter > "" Or IsNull(Me.txtCostCenter) Then
style = vbCritical + vbOKOnly
title = "Data Error!"
msg = "Cost Center is required!"
response = MsgBox(msg, style, title)
Cancel = True
Me.txtCostCenter.SetFocus
End If

when you enter a value in the txtCostCenter to cause an error, it displays
the msgbox and then setsfocus to the next field in the tab order, not on
txtCostCenter. I simply want the cursor back to the field in error after it
displays the msgbox. Can anyone help?

thanks in acvance
 
D

Dirk Goldgar

PeterM said:
I'm having problems with AC2003. I have code for the AfterUpdate of a
text
field. In there I...

If Not Me.txtCostCenter > "" Or IsNull(Me.txtCostCenter) Then
style = vbCritical + vbOKOnly
title = "Data Error!"
msg = "Cost Center is required!"
response = MsgBox(msg, style, title)
Cancel = True
Me.txtCostCenter.SetFocus
End If

when you enter a value in the txtCostCenter to cause an error, it displays
the msgbox and then setsfocus to the next field in the tab order, not on
txtCostCenter. I simply want the cursor back to the field in error after
it
displays the msgbox. Can anyone help?


Use the BeforeUpdate event, and set the event proc's Cancel argument to
prevent the focus from leaving the control:

'----- start of example code -----
Private Sub txtCostCenter_BeforeUpdate(Cancel As Integer)

If len(Me.txtCostCenter & "") = 0 Then
style = vbCritical + vbOKOnly
title = "Data Error!"
msg = "Cost Center is required!"
response = MsgBox(msg, style, title)
Cancel = True
End If

End Sub
'----- end of example code -----

Note that this code will only fire if the user actually enters something in
the text box. I'd recommend using the *form's* BeforeUpdate event to
validate all required fields.
 
D

Dirk Goldgar

(re-sending, as my original reply hasn't appeared)

PeterM said:
I'm having problems with AC2003. I have code for the AfterUpdate of a
text
field. In there I...

If Not Me.txtCostCenter > "" Or IsNull(Me.txtCostCenter) Then
style = vbCritical + vbOKOnly
title = "Data Error!"
msg = "Cost Center is required!"
response = MsgBox(msg, style, title)
Cancel = True
Me.txtCostCenter.SetFocus
End If

when you enter a value in the txtCostCenter to cause an error, it displays
the msgbox and then setsfocus to the next field in the tab order, not on
txtCostCenter. I simply want the cursor back to the field in error after
it
displays the msgbox. Can anyone help?


Use the BeforeUpdate event, and set the event proc's Cancel argument to
prevent the focus from leaving the control:

'----- start of example code -----
Private Sub txtCostCenter_BeforeUpdate(Cancel As Integer)

If len(Me.txtCostCenter & "") = 0 Then
style = vbCritical + vbOKOnly
title = "Data Error!"
msg = "Cost Center is required!"
response = MsgBox(msg, style, title)
Cancel = True
End If

End Sub
'----- end of example code -----

Note that this code will only fire if the user actually enters something in
the text box. I'd recommend using the *form's* BeforeUpdate event to
validate all required fields.
 
J

John Spencer

Try moving the code to the BEFORE UPDATE event, after update does not have a
cancel.

If Len(Me.TxtCostCenter.Text & "") = 0 THEN
style = vbCritical + vbOKOnly
title = "Data Error!"
msg = "Cost Center is required!"
MsgBox msg, style, title
Cancel = True
End IF

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top