4 Replies Latest reply on Jun 26, 2015 5:05 AM by Tapani Sjoman

    How to handle blank input by a user?

    Aaron Torberg

      I have a UserForm TextBox and I want the user to enter an integer (in my program I assign the text box to an integer variable).  I use a case to evaluate different values for the text box but I cannot get my case to handle a blank input, and what about the use of a alpha character or string?  How can I handle these events?  I am very new to this stuff so please go easy on me...

       

      Thanks!

        • Re: How to handle blank input by a user?
          Roland Schwarz

          Why, that's easy! So easy I can do it without even looking at the code that you did not copy-and-paste. Now, I will proceed to not copy-and-paste my answer.

          • Re: How to handle blank input by a user?
            Tapani Sjoman

            Dim sMsg As String
            Dim Answer As String

             

            sMsg = "Please give a number?"
            Answer = InputBox(sMsg, "Enter Number", "")

             

            If Answer = "" Then
            Answer = "Empty!"
            ElseIf Not IsNumeric(Answer) Then
            Answer = "Some Grab"
            End If

             

            MsgBox "Good! Your number is " & Answer

              • Re: How to handle blank input by a user?
                Aaron Torberg

                Thanks Roland for the help!  Here is the code you so keenly asked for... did I mention I am new?

                 

                Private Sub TextBox1_AfterUpdate()

                 

                Dim SSValves As String

                Dim SSValvesFilename As String

                 

                SSValves = UserForm1.TextBox1

                'If SSValves = Null Then

                '    SSValves = 0

                'End If

                 

                Select Case CInt(SSValves)

                    Case 0 To 9: SSValvesFilename = "0" & SSValves

                    Case 10 To 16: SSValvesFilename = SSValves

                    Case Is > 16: MsgBox "Please select less than 16"

                    Case Is < 0:  MsgBox "No negative values!"

                        UserForm1.TextBox1 = ""

                End Select

                 

                As you can see in the commented out section I tried to deal with this, but it doesn't work...  thanks again for any help!

                  • Re: How to handle blank input by a user?
                    Tapani Sjoman

                    Try somethink like this:

                     

                    Private Sub TextBox1_AfterUpdate()

                     

                    Dim InputNumber As Integer
                    Dim Answer As String
                    Dim SSValvesFilename As String

                     

                    Answer = TextBox1.Value

                     

                    If Answer = "" Then
                    InputNumber = 0 ' default if textbox empty
                     
                    ElseIf Not IsNumeric(Answer) Then
                    InputNumber = -1

                     

                    Else
                    InputNumber = CInt(Answer) ' cast to integer

                     

                    End If

                     

                     

                    If (InputNumber >= 0) And (InputNumber <= 9) Then
                      SSValvesFilename = "0" & InputNumber
                      MsgBox "Good! SSValvesFilename: " & SSValvesFilename
                     
                    ElseIf (InputNumber >= 10) And (InputNumber <= 16) Then
                      SSValvesFilename = "" & InputNumber
                      MsgBox "Good! SSValvesFilename: " & SSValvesFilename

                     

                    ElseIf (InputNumber < 0) Then
                      MsgBox "Give a positive integer number! "

                     

                    ElseIf (InputNumber > 16) Then
                      MsgBox "Give an integer number, max 16! "

                     

                    End If

                     

                    End Sub