How to undoing a TextBox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alinagoo
    New Member
    • Apr 2010
    • 53

    #1

    How to undoing a TextBox

    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
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by alinagoo
    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
    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:
    Code:
    Private Sub Margin_AfterUpdate()
    Dim ctlTBox As TextBox
    
    Set ctlTBox = Me![Margin]
    
    If ctlTBox > 10 Then
      ctlTBox = ctlTBox.OldValue
    End If
    End Sub

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      What exactly did you try?

      Code:
      Private Sub TextBoxName_AfterUpdate()
       If Me.TextBoxName.Value = "9999" Then
        Me.TextBoxName.Value = Me.TextBoxName.OldValue
       End If
      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)>

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        I don't know what you tried but cancelling an update if the value doesn't fit is pretty straightforward :
        Code:
        Private Sub YourTextBox_BeforeUpdate(Cancel As Integer)
            If Me.YourTextBox = "Invalid Value" Then Cancel = True
        End Sub

        Comment

        • alinagoo
          New Member
          • Apr 2010
          • 53

          #5
          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

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            NeoPa's method should work if done properly. What did you try (This was asked before)?

            Comment

            • alinagoo
              New Member
              • Apr 2010
              • 53

              #7
              Originally posted by NeoPa
              NeoPa's method should work if done properly. What did you try (This was asked before)?
              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

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32669

                #8
                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 :
                Code:
                Private Sub Margin_BeforeUpdate(Cancel As Integer)
                    With Me.Margin
                        If .Value > 10 Then
                            Call .Undo
                            Cancel = True
                        End If
                    End With
                End Sub
                This leaves the cursor in the control, as well as reverting the data to that before any changes were attempted.

                Comment

                Working...