neither beforeupdate nor afterupdate clear and keep focus on textb

G

Guest

sub text_beforeupdate
cancel = true
text.undo
end sub

Then get focus back in text, the bad value highlighted.
Only problem is user can tab or click out of text and the bad value remains.

or if I try:
sub text_afterupdate
text = ""
end sub

Then the bad value is cleared, but of course focus goes to the next control.

Is there a way to clear the value AND keep the focus in the text control?

(Using Access 2003 SP2)
 
R

Rick Brandt

allen said:
sub text_beforeupdate
cancel = true
text.undo
end sub

Then get focus back in text, the bad value highlighted.
Only problem is user can tab or click out of text and the bad value
remains.

No, because BeforeUpdate will just fire again and the user should be trapped
there until they fix the entry. If that is not what you are seeing then you are
doing something other than what you are describing.

I suspect that you might be having problems because "text" is a reserved word.
Try changing the name of your control. ControlName.undo should actually clear
the invalid entry and yet you say it is still there. Something again that
should not be the case.
 
G

Guest

Sorry, I should have copied in the actual code used:

Private Sub txtLateFee_BeforeUpdate(Cancel As Integer)
If Not IsNumeric(txtLateFee) Then
MsgBox "Late Fee must be a number between 0 and 10", , "Fee entry"
Cancel = True
txtLateFee.Undo
End If
End Sub

Then if I enter abc in the field, I get the message box followed by focus
staying in the txtLateFee box with abc highlighted. That is, Undo does not
clear the contents.

Now, if the user tabs or clicks in another box, the invalid abc remains as
the value of the text box. It is the behavior of leaving abc in the text box
that I am trying to prevent (since I cannot prevent the user from tabbing out
of the box).
 
R

Rick Brandt

allen said:
Sorry, I should have copied in the actual code used:

Private Sub txtLateFee_BeforeUpdate(Cancel As Integer)
If Not IsNumeric(txtLateFee) Then
MsgBox "Late Fee must be a number between 0 and 10", , "Fee
entry" Cancel = True
txtLateFee.Undo
End If
End Sub

Then if I enter abc in the field, I get the message box followed by
focus staying in the txtLateFee box with abc highlighted. That is,
Undo does not clear the contents.

Now, if the user tabs or clicks in another box, the invalid abc
remains as the value of the text box. It is the behavior of leaving
abc in the text box that I am trying to prevent (since I cannot
prevent the user from tabbing out of the box).

What value was in the field before you began editing? Sounds like you started
out with "abc" in the control.

BeforeUpdate will not do anything about an incorrect value that was already
there before you made an edit. It will only worry about *new changes*. If the
value started as null or a legal value and you really did enter "abc" then Undo
definitely WILL remove it and you will not be able to tab to another control
until a valid entry is made.
 
G

Guest

Rick, the way you describe it is the way I think it should work, but
something weird is happening at my pc:
1. textbox originally had 5 in it (a valid value)
2. I type abc and press tab (abc shows up not highlighted)
3. get the error message, okay it
4. focus is in the textbox with abc now highlighted
5. I can hit a Save button on the form and I can click to other controls and
the highlighted abc will remain in the textbox

By the way, if I remove the .UNDO, then focus again ends in the textbox with
cursor to right of abc and abc not highlighted. However, I can still click
my Save button and I can get to other controls and unhighlighted abc will
remain in the text box.

You are saying that your expected results would be for the UNDO to cause the
text box to clear, is that right? If so, only my stange experience is
different.
 
R

Rick Brandt

allen said:
Rick, the way you describe it is the way I think it should work, but
something weird is happening at my pc:
1. textbox originally had 5 in it (a valid value)
2. I type abc and press tab (abc shows up not highlighted)
3. get the error message, okay it
4. focus is in the textbox with abc now highlighted
5. I can hit a Save button on the form and I can click to other
controls and the highlighted abc will remain in the textbox

By the way, if I remove the .UNDO, then focus again ends in the
textbox with cursor to right of abc and abc not highlighted.
However, I can still click my Save button and I can get to other
controls and unhighlighted abc will remain in the text box.

You are saying that your expected results would be for the UNDO to
cause the text box to clear, is that right? If so, only my stange
experience is different.

This is a bound TextBox right?

Actually it would be better to just make the field in the table a Number
DataType (size = Byte) and then use a validation rule of "Between 0 and 10".
That is better because the rule is enforced regardless of what mechanism is used
to update the table. Using code in your form still allows any update method
besides your form to insert invalid values.
 
G

Guest

1. The field is not bound
2. There are other fields requiring more complicated validation so locking
to a specific range for each field would not always work.
The issue can be replicated with a trivial mdb which I am sending you.
Thanks for your help.
Allen
 
G

Guest

Problem is cleared up by Rick:
Since the text box control was not bound, I should not have used .UNDO.
Once .UNDO was removed, Access worked as desired.
 

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