+ Reply to Thread
Results 1 to 7 of 7

prevent user moving out of textbox if input is bad

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    Office 2019 Enterprise
    Posts
    473

    prevent user moving out of textbox if input is bad

    Hi,

    I'm looking for the answer to this please...

    I have a text box and I want it so that if the user has not typed in the number 1 then it won't let him leave the box.

    Can anyone tell me how to do this?

    Thanks

    John

  2. #2
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    What is your code so far ?

    Carim

  3. #3
    Forum Contributor spinkung's Avatar
    Join Date
    10-27-2006
    Posts
    199
    Hi,

    i recently used something like this....

    If Trim(Me.txtDate) = "" Then
    MsgBox ("Please Enter a Recieved Date.")
    Me.txtDate.SetFocus
    Exit Sub
    End If
    guess you would have this...

    If Trim(Me.txtbox1) = 1 Then
    MsgBox ("Please Enter a 1")
    Me.txtbox1.SetFocus
    Exit Sub
    End If
    HTH
    Spinkung.

  4. #4
    Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    Office 2019 Enterprise
    Posts
    473
    Hi, thanks for the quick reply..

    My code is

    Private Sub TextBox1_AfterUpdate()
    If TextBox1.Value <> "1" Then
    MsgBox "This should be 1"
    Me.TextBox1.SetFocus
    End If
    End Sub


    This code recognises that 1 hasn't been entered and returns the correct msgbox but it still jumps to the next box.

    Any ideas?

    Thanks

    John

  5. #5
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    Private Sub TextBox1_Change()
               Dim okstop As Boolean
               Dim yesno_continue As Boolean
               Dim mytext As String
               okstop = False
    
               Do
                   mytext = TextBox1.Value
                   If Not IsNumeric(mytext) And mytext <> "" Then
                       TextBox1.Value = ""    'Clears the TextBox
    
                       'Shows a message box that informs you that you typed
                       'something other than a number.
                       yesno_continue = MsgBox("Please type only numbers." & _
                           Chr(13) & "Continue?", vbYesNo)
                   Else
                       okstop = True   'You typed a number in the TextBox.
                   End If
    
               'Continue with the loop if you click Yes.
               'Stop the loop if they typed a number in the TextBox.
               Loop Until (yesno_continue = vbNo) Or (okstop = True)
           End Sub
    HTH
    Carim

  6. #6
    Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    Office 2019 Enterprise
    Posts
    473
    Thanks for the reply.

    I tried your method (I had to edit it to suit) but the msgbox keeps coming back up every time I cancel it and I have to close Excel to start working again.

    Thanks for your help but I think I am just going to leave it for now.

    Cheers

    John

+ 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