user1921704
user1921704

Reputation: 179

Do action if user starts to delete data from textbox

I have an access form with a text-box that user can update using a list-box or by editing it directly, I need to make an action if the user starts to delete from this text-box, if he starts to add data no problem only if he starts to delete from it, something like :

Private Sub textbox_AfterUpdate()

   If Me.[textbox].SelStart = Len(Me.[textbox].Text) - 1 Then

    ' do something
   else

    ' do something else

   End If


End Sub

Upvotes: 0

Views: 357

Answers (2)

Fionnuala
Fionnuala

Reputation: 91346

You might like to consider the change event, the text property is the current unsaved content of the control that has focus and the value property is the saved content of the control with focus.

The advantage of Change is that the user cannot simply insert letters into the existing string, for example, the user cannot change 20 meters to 200 meters.

Private Sub AText_Change()
''If the current content is the same as the 
''previous content with something added 
If Me.AText.Text Like Me.AText.Value & "*" Then
    ''Ok
Else
    ''Problem
    MsgBox "Not allowed"
    Me.AText = Me.AText.Value
End If
End Sub

You could also allow the saved content to be a substring of the current content.

Upvotes: 1

Siddharth Rout
Siddharth Rout

Reputation: 149297

You might want to use the KeyDown event instead of AfterUpdate

This captures the Backspace and the Del button.

Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 8 Or KeyCode = 46 Then
        MsgBox "Text was deleted"
    End If
End Sub

There can be other scenarios where the text is deleted for example Ctrl + X So you can trap all that.

Upvotes: 2

Related Questions