By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,575 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,575 IT Pros & Developers. It's quick & easy.

How to undoing a TextBox

P: 53
Hi all.
I wanted to change value of a TextBox to previous value of that TextBox when a user types some special data but i couldn't change value in BeforeUpdate or AfterUpdate Event handler!
Is any special command or method exists to undo only one TextBoxe's value of a form?
Thanks in advanced
Jul 1 '10 #1

✓ answered by ADezii

@alinagoo
Let's say that you have a 'Bound' Field on a Form named [Margin]. Let's also suppose that if at any time, a User enters a Margin of > 10 into this Field for an existing Record, you then wish to restore the Original Value of that Field. In the AfterUpdate() Event of the [Margin] Field:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Margin_AfterUpdate()
  2. Dim ctlTBox As TextBox
  3.  
  4. Set ctlTBox = Me![Margin]
  5.  
  6. If ctlTBox > 10 Then
  7.   ctlTBox = ctlTBox.OldValue
  8. End If
  9. End Sub

Share this Question
Share on Google+
7 Replies


ADezii
Expert 5K+
P: 8,702
@alinagoo
Let's say that you have a 'Bound' Field on a Form named [Margin]. Let's also suppose that if at any time, a User enters a Margin of > 10 into this Field for an existing Record, you then wish to restore the Original Value of that Field. In the AfterUpdate() Event of the [Margin] Field:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Margin_AfterUpdate()
  2. Dim ctlTBox As TextBox
  3.  
  4. Set ctlTBox = Me![Margin]
  5.  
  6. If ctlTBox > 10 Then
  7.   ctlTBox = ctlTBox.OldValue
  8. End If
  9. End Sub
Jul 1 '10 #2

missinglinq
Expert 2.5K+
P: 3,532
What exactly did you try?

Expand|Select|Wrap|Line Numbers
  1. Private Sub TextBoxName_AfterUpdate()
  2.  If Me.TextBoxName.Value = "9999" Then
  3.   Me.TextBoxName.Value = Me.TextBoxName.OldValue
  4.  End If
  5. End Sub
should work, replacing 9999 with the triggering value. Omit the quotes, of course, if the value is Numerical instead of Text.

Linq ;0)>
Jul 1 '10 #3

NeoPa
Expert Mod 15k+
P: 31,769
I don't know what you tried but cancelling an update if the value doesn't fit is pretty straightforward :
Expand|Select|Wrap|Line Numbers
  1. Private Sub YourTextBox_BeforeUpdate(Cancel As Integer)
  2.     If Me.YourTextBox = "Invalid Value" Then Cancel = True
  3. End Sub
Jul 1 '10 #4

P: 53
Thanks you all
But NeoPa's method does not worked properly and the method used by Missinglinq and ADezii with OlaValue property works fine.
Good Luck
Jul 1 '10 #5

NeoPa
Expert Mod 15k+
P: 31,769
NeoPa's method should work if done properly. What did you try (This was asked before)?
Jul 1 '10 #6

P: 53
@NeoPa
I used "Cancel = True" in BeforeUpdate Event handler of a TextBox to convert value of that TextBox to previous saved value if newly typed value is not in a valid range but it does not work!

Thank
Good Luck
Jul 2 '10 #7

NeoPa
Expert Mod 15k+
P: 31,769
My mistake. The Cancel = True simply stops the cursor leaving the control. Attempting to set the value in a BeforeUpdate() event procedure always triggers an error, so the trick to doing it properly is to use the .Undo() method.

Using ADezii's situation as an example :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Margin_BeforeUpdate(Cancel As Integer)
  2.     With Me.Margin
  3.         If .Value > 10 Then
  4.             Call .Undo
  5.             Cancel = True
  6.         End If
  7.     End With
  8. End Sub
This leaves the cursor in the control, as well as reverting the data to that before any changes were attempted.
Jul 2 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.