+ Reply to Thread
Results 1 to 12 of 12

How to stop running the code when Pressing Backspace

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    How to stop running the code when Pressing Backspace

    Hi,

    I have the following code to add dash mark to TextBox1 value depending on some condtions (mentioned in the code), the code works perfectly, but when we use Backspace Key to clear some charactors it is conflicting with the code running when changing Textbox1 value.

    Private Sub TextBox1_Change()
    
    With TextBox1
        If .Value = "OMTND" Then .Value = .Value & "-"
        If OptionButton1 = True And Len(.Value) = 8 And IsNumeric(Right(.Value, 2)) Then .Value = .Value & "-"
        If OptionButton1 = True And Len(.Value) = 11 And IsNumeric(Right(.Value, 2)) Then .Value = .Value & "-"
    End With
    
    End Sub
    I there any solution for that?

    Thanks,
    Attached Files Attached Files
    Last edited by LoveCandle; 06-24-2009 at 01:37 AM.

  2. #2
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: How to running the code when Pressing Backspace

    hi,

    Try changing your event macro by changing the name to either of the below examples. If you need more information, check out the Excel help files or do a quick search.

    Option Explicit
    Private Sub TextBox1_AfterUpdate()
    '...
    End Sub
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    '...
    End Sub
    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  3. #3
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: How to stop running the code when Pressing Backspace

    Thank you broro 183 for your help, but that is not what I need.

    I have tried these two events .. but they are not that much practical ..

    I want the dashes to be entered while entering data in the textbox not after exiting, in order for the user not to enter them.

    I hope that my point is clearer now,

    Thanks,

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to stop running the code when Pressing Backspace

    Try this:
    Private Sub TextBox1_Change()
        Static iOld As Long
        Dim iNew    As Long
    
        With TextBox1
            iNew = Len(.Value)
            If iNew > iOld Then
                If .Value = "OMTND" Or _
                   (OptionButton1 And _
                    IsNumeric(Right(.Value, 2)) And _
                    (iNew = 8 Or iNew = 11)) Then .Value = .Value & "-"
            End If
            iOld = iNew
        End With
    End Sub
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: How to stop running the code when Pressing Backspace

    Fantastic shg .. that is exactly what I am looking for,

    Thank you soooooo much,

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to stop running the code when Pressing Backspace

    You're welcome. Would you please mark the thread as Solved?

  7. #7
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: How to stop running the code when Pressing Backspace

    Hi,

    I tested your code and found it working properly when entering capital letters (OMTND) ,,

    However, when I enter small letters (omtnd), it doesn't work unless I change the same word in the code to small letters.

    Can you please find me a solution for that problem?

    Thanks,

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to stop running the code when Pressing Backspace

    See Help for the UCase function.

  9. #9
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: How to stop running the code when Pressing Backspace

    I've tried that but the problem is still existing,

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How to stop running the code when Pressing Backspace

    If UCase(.Value) = "OMTND" Or _

  11. #11
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: How to stop running the code when Pressing Backspace

    Hi,

    I had some miss arrangment in my original code and that is what caused the problem,

    Anyhow, The problem was completely solved after using Ucase as you advised.

    Thak you so shg for you help,

    bye,

  12. #12
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: How to stop running the code when Pressing Backspace

    Howdy LoveCandle,
    How go going. Did you get the loggon userform project all working. If you would like to share it i would be greatfull. Ill tuck it away for and use it for somethink later down the track.
    Cheers.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1