Author Topic: Is there a better way to evaluate TextBox Value  (Read 4866 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Is there a better way to evaluate TextBox Value
« on: May 07, 2007, 03:15:06 PM »
Is there a better way to streamline this code?
Code: [Select]
      If IsNumeric(txtLengthFt) Then
            If txtLengthFt.Value Mod 1 = 0 Then
                  intL = CInt(txtLengthFt) * 12
            Else
                  MsgBox "Enter a valid Length"
                  Me.Show
            End If
      Else
            MsgBox "Enter a valid Length"
            Me.Show
      End If
            If IsNumeric(txtLengthIn) Then
            If txtLengthIn.Value Mod 1 = 0 Then
                  intL = intL + CInt(txtLengthIn)
            Else
                  MsgBox "Enter a valid Length"
                  Me.Show
            End If
      Else
            MsgBox "Enter a valid Length"
            Me.Show
      End If
I have my logic worked out to determine if a user puts in a numeric value, but I didn't know if this was the best, most efficient way to do this
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Guest

  • Guest
Re: Is there a better way to evaluate TextBox Value
« Reply #1 on: May 07, 2007, 03:28:54 PM »
You could simply restrict the textbox to only allow numbers, thereby avoiding having to check if the value is a number or not.


Code: [Select]
Private Sub txtLengthFt_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case Asc("0") To Asc("9")
        Case Else
            KeyAscii = 0
    End Select
End Sub

Arizona

  • Guest
Re: Is there a better way to evaluate TextBox Value
« Reply #2 on: May 07, 2007, 03:30:48 PM »
Thanks Matt!
I learned something also :-)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Is there a better way to evaluate TextBox Value
« Reply #3 on: May 07, 2007, 03:41:00 PM »
thanks Matt!

What happens if they press a non-number, nothing?  I have never used that, so I am trying it now, but thought I would ask to see if there were any gotcha's I should know about.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Is there a better way to evaluate TextBox Value
« Reply #4 on: May 07, 2007, 03:42:01 PM »
That is SWEET!!  I never knew you could do that.

Thanks Matt
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Is there a better way to evaluate TextBox Value
« Reply #5 on: May 07, 2007, 03:42:49 PM »
Now that you showed me that, I will show you what you just saved me from
Code: [Select]
      If IsNumeric(txtLengthFt) Then
            If txtLengthFt.Value Mod 1 = 0 Then
                  intL = CInt(txtLengthFt) * 12
            Else
                  MsgBox "Enter a valid Length"
                  Me.Show
            End If
      Else
            MsgBox "Enter a valid Length"

            Me.txtLengthFt.SetFocus
            Me.txtLengthFt.SelStart = 0
            Me.txtLengthFt.SelLength = Len(txtLengthFt)
            Me.Show
      End If
      If IsNumeric(txtLengthIn) Then
            If txtLengthIn.Value Mod 1 = 0 Then
                  intL = intL + CInt(txtLengthIn)
            Else
                  MsgBox "Enter a valid Length"
                  Me.Show
            End If
      Else
            MsgBox "Enter a valid Length"
            Me.txtLengthFt.SetFocus
            Me.txtLengthIn.SelStart = 0
            Me.txtLengthIn.SelLength = Len(txtLengthFt)
            Me.Show
      End If
     
      If IsNumeric(txtWidthFt) Then
            If txtWidthFt.Value Mod 1 = 0 Then
                  intW = CInt(txtWidthFt) * 12
            Else
                  MsgBox "Enter a valid Length"
                  Me.Show
            End If
      Else
            MsgBox "Enter a valid Length"

            Me.txtWidthFt.SetFocus
            Me.txtWidthFt.SelStart = 0
            Me.txtWidthFt.SelLength = Len(txtWidthFt)
            Me.Show
      End If
      If IsNumeric(txtWidthIn) Then
            If txtWidthIn.Value Mod 1 = 0 Then
                  intW = intW + CInt(txtWidthIn)
            Else
                  MsgBox "Enter a valid Length"
                  Me.Show
            End If
      Else
            MsgBox "Enter a valid Length"
            Me.txtWidthFt.SetFocus
            Me.txtWidthIn.SelStart = 0
            Me.txtWidthIn.SelLength = Len(txtWidthFt)
            Me.Show
      End If

      If IsNumeric(txtHeightFt) Then
            If txtHeightFt.Value Mod 1 = 0 Then
                  intH = CInt(txtHeightFt) * 12
            Else
                  MsgBox "Enter a valid Length"
                  Me.Show
            End If
      Else
            MsgBox "Enter a valid Length"

            Me.txtHeightFt.SetFocus
            Me.txtHeightFt.SelStart = 0
            Me.txtHeightFt.SelLength = Len(txtHeightFt)
            Me.Show
      End If
      If IsNumeric(txtHeightIn) Then
            If txtHeightIn.Value Mod 1 = 0 Then
                  intH = intH + CInt(txtHeightIn)
            Else
                  MsgBox "Enter a valid Length"
                  Me.Show
            End If
      Else
            MsgBox "Enter a valid Length"
            Me.txtHeightFt.SetFocus
            Me.txtHeightIn.SelStart = 0
            Me.txtHeightIn.SelLength = Len(txtHeightFt)
            Me.Show
      End If
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Is there a better way to evaluate TextBox Value
« Reply #6 on: May 07, 2007, 03:44:35 PM »
ok, since I got that, let me ask this question.  3 of the txt boxes are used for inches.  I was thinking of using CInt() to convert to integer, and doing a < 12 check to make sure they didn't go over where they shouldn't.  Any other ideas
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Is there a better way to evaluate TextBox Value
« Reply #7 on: May 07, 2007, 03:56:37 PM »
on a side note, whilst error checking a valid integer in the previous code, I was popping a msgbox, and sending them back to the form with the offending txtbox highlighted.  BUT, then they had to click the GO button a second time, which is seeming to put the function in a loop.  Should I clear the values of the variables and repopulate them when the data is finally good?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Guest

  • Guest
Re: Is there a better way to evaluate TextBox Value
« Reply #8 on: May 07, 2007, 04:01:34 PM »
ok, since I got that, let me ask this question.  3 of the txt boxes are used for inches.  I was thinking of using CInt() to convert to integer, and doing a < 12 check to make sure they didn't go over where they shouldn't.  Any other ideas
You could do something like this:

Code: [Select]
Private Sub TextBox1_Change()
    If TextBox1.Value > 12 Then
        MsgBox "Whoa pardner!  Nothing greater than 12 is allowed here!", vbCritical + vbOKOnly
        ' Enter a value of '0' in the textbox
        TextBox1.Text = "0"
        ' Highlight the text in the textbox for immediate re-entry of number(s)
        TextBox1.SelStart = 0
        TextBox1.SelLength = TextBox1.TextLength
    End If
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case Asc("0") To Asc("9")
        Case Else
            KeyAscii = 0
    End Select
End Sub

Atook

  • Swamp Rat
  • Posts: 1027
  • AKA Tim
Re: Is there a better way to evaluate TextBox Value
« Reply #9 on: May 07, 2007, 04:47:12 PM »
Look at why the function loops the second time, it shouldn't just validate the data and move on. Do not clear the data your users have entered, it'd be silly to have to enter it all again because you screwed up one field.

If validatedata then
 blah blah
else
 msgbox "You lush! this is wrong!"
endif


David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Is there a better way to evaluate TextBox Value
« Reply #10 on: May 07, 2007, 05:09:55 PM »
I relooked at what I was doing.  Firstly, I moved all my validation code out of the button click event, thus getting rid of the problem.  Secondly, I narrowed down why it was looping.  It was because I fired the event, and didn't exit the click event when I went back to the form, so the event ran a second time after they update the info problem.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Is there a better way to evaluate TextBox Value
« Reply #11 on: May 07, 2007, 09:33:53 PM »
Nice stuff Matt

CmdrDuh- if your code is not in the click event, how do you exit it?

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Is there a better way to evaluate TextBox Value
« Reply #12 on: May 08, 2007, 12:15:54 PM »
Using Matt's suggestion, I used this
Code: [Select]
Private Sub txtLengthFt_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
      Select Case KeyAscii
            Case Asc("0") To Asc("9")
            Case Else
                  KeyAscii = 0
      End Select
End Sub
and this
Code: [Select]
Private Sub txtLengthIn_Change()
      If txtLengthIn.Value >= 12 Then
            MsgBox "Whoa the Pony, Nothing greater than 12", vbCritical + vbOKOnly
            txtLengthIn.Text = "0"
            txtLengthIn.SelStart = 0
            txtLengthIn.SelLength = txtLengthIn.TextLength
      End If
End Sub
to check the values.  Once all txtBoxes are filled in, and valid based on above 2 checks, the click event is enabled. (there are more than 2 checks, I just pasted 2 of them to show what Im doing.  All 6 boxes have the same type of error validation)
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Guest

  • Guest
Re: Is there a better way to evaluate TextBox Value
« Reply #13 on: May 08, 2007, 01:16:41 PM »
Here's a slightly modified version of what have...

If you enter a value GREATER than 12, it will prompt you to convert the inches to feet.
Code: [Select]
Private Sub txtLengthIn_Change()
    Dim vbResult As Long
    If txtLengthIn.Value >= 12 Then
        vbResult = MsgBox("Since you're attempting to enter a value GREATER than 12, would you like to convert it to FEET?", vbQuestion + vbYesNo, "Way to go!")
        If vbResult = vbYes Then
            MsgBox "Converted value = " & txtLengthIn / 12
        ElseIf vbResult = vbNo Then
            MsgBox "Start over!"
            txtLengthIn.Text = "0"
            txtLengthIn.SelStart = 0
            txtLengthIn.SelLength = txtLengthIn.TextLength
        End If
    End If
End Sub

This will allow you to add a PERIOD in the number (in case you don't work with whole inches).
Code: [Select]
Private Sub txtLengthIn_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
   If InStr(1, txtLengthIn.Text, ".") > 0 And KeyAscii = Asc(".") Then
        KeyAscii = 0
        Exit Sub
    End If
    Select Case KeyAscii
        Case Asc("0") To Asc("9"), Asc(".")
        Case Else
            KeyAscii = 0
    End Select
End Sub

Just something else you might want to think about.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Is there a better way to evaluate TextBox Value
« Reply #14 on: May 09, 2007, 10:21:55 AM »
Good thoughts.  I will have to see if I can make use of those.  I like the added period option, as our civil engineers are always using ##.#' format.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)