After Update and Set Focus Issue

T

tkosel

I have a form with a control named "LabelQTY". The control has an
AfterUpdate Event reproduced below.

Private Sub LabelQTY_AfterUpdate()
If Me.LabelQTY < 2 Then
MsgBox "Label Quantity must be at least 2!", , "Qty must be at least
2!"
Me.LabelQTY = 2
Me.LabelQTY.SetFocus
End If
End Sub

If I enter a 1 in the field, the event fires off, but the focus still moves
to the next control on the form. There are no other events for this or any
other control. What causes this, how can I get the focus to go back to
LabelQTY to allow the user to change it to a number larger than 2 if desired?
 
F

fredg

I have a form with a control named "LabelQTY". The control has an
AfterUpdate Event reproduced below.

Private Sub LabelQTY_AfterUpdate()
If Me.LabelQTY < 2 Then
MsgBox "Label Quantity must be at least 2!", , "Qty must be at least
2!"
Me.LabelQTY = 2
Me.LabelQTY.SetFocus
End If
End Sub

If I enter a 1 in the field, the event fires off, but the focus still moves
to the next control on the form. There are no other events for this or any
other control. What causes this, how can I get the focus to go back to
LabelQTY to allow the user to change it to a number larger than 2 if desired?

Use the control's BeforeUpdate event:

Private Sub LabelQTY_BeforeUpdate(Cancel as Integer)
If Me.LabelQTY < 2 Then
MsgBox "Label Quantity must be at least 2!", , "Qty must be
at least 2!"
Cancel = True
End If
End Sub

The Cancel = True will stop the update and return focus to the
LabelQTY control for re-entry. There is no need to force the value to
2 (nor can you in this event). Why not just set the default value of
this control to 2?

Why are you naming a Text control "LabelXXX"?
I would find it very confusing, especially if I came back to this
program after having not worked on it for a period of time.
Why not simply name it "Qty"? Or "txtQty".
 
T

tkosel

fredg said:
Use the control's BeforeUpdate event:

Private Sub LabelQTY_BeforeUpdate(Cancel as Integer)
If Me.LabelQTY < 2 Then
MsgBox "Label Quantity must be at least 2!", , "Qty must be
at least 2!"
Cancel = True
End If
End Sub

The Cancel = True will stop the update and return focus to the
LabelQTY control for re-entry. There is no need to force the value to
2 (nor can you in this event). Why not just set the default value of
this control to 2?

Why are you naming a Text control "LabelXXX"?
I would find it very confusing, especially if I came back to this
program after having not worked on it for a period of time.
Why not simply name it "Qty"? Or "txtQty".

Fred

While I appreciate your response, this suggestion will not meet my needs. I
had already ruled out the before update event. I already have the default
value set to 2, just didn't tell you that. If the user changes it to 1, I
want the message to display as I already indicated. I want the 2 to be
placed in the field as indicated, but want the focus to go back to that
control so the user can put in a larger number if s/he decides thats what
s/he wants.

The before update event will do all that except allow me to place the 2 in
that field. This leaves me in the same boat as the after update event I had
in place originally, except with the after update event, I can replace the 1
with a 2. The only problem with the after update event is that the focus
goes to another control.

This is the part I do not understand. Why won't the after update event
allow me to set the focus back to the contol in question?

As far as my naming convention of the cotrol, I don't see why you would
concern yourself with my logic for naming controls. It happens to be the
name LabelQTY actually assists me in knowing what that control is for with
some other actions I take on that form. (It is the number of labels that I
print!) I don't need to preface the name of a text box control with the
designator txt, as I am smart enough to know that control is a text box.

Anyhow, thanks for your prompt assistance.
 

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