IsNumeric Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • munkeyb0i
    New Member
    • Nov 2006
    • 5

    IsNumeric Function

    Hi,
    The program that I have to make is suppose to check if the things I entered in the textbox is numeric and also if it's 9 digits. I tried a lot of ways to do it but none of them seem to work. Maybe someone can help me. This is my code for it:

    Private Function TestInput() As Boolean

    If IsNumeric(txtID .Text) and Len(Val(txtID.t ext)) Then
    MsgBox("Please enter an appropriate FID.", MsgBoxStyle.Exc lamation, "Enter FID Properly")
    txtFID.Focus()
    Return False
    End If
    End Function

    Thanks
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    This might be some help (just off the top of my head, not tested)
    Code:
    Private Function TestInput() As Boolean
      If IsNumeric(txtID.Text) and Len(Val(txtID.text) = 9) Then
        TestInput = True
      Else
        MsgBox("Please enter an appropriate FID.", MsgBoxStyle.Exclamation, "Enter FID Properly")
        txtFID.[B]Set[/B]Focus()
        TestInput = False ' (Not strictly necessary, as False is default)
      End If
    End Function
    Also note, we may be using different versions of VB - this should work in VB6.

    Also, according to the manual, the input for IsNumeric must be a variant, not a string. I haven't checked this.

    Comment

    • munkeyb0i
      New Member
      • Nov 2006
      • 5

      #3
      Yeah unfortunately, that doesn't work. It might be b/c i'm using VB5 but don't know. Thanks for your effort though

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by munkeyb0i
        Yeah unfortunately, that doesn't work. It might be b/c i'm using VB5 but don't know. Thanks for your effort though
        Hm... you could try leaving out the IsNumeric check. Try this...
        Code:
        Private Function TestInput() As Boolean
          If Len(Format(Val(txtID.text))) = 9) Then
            TestInput = True
          Else
            MsgBox("Please enter an appropriate FID.", MsgBoxStyle.Exclamation, "Enter FID Properly")
            txtFID.SetFocus()
            TestInput = False ' (Not strictly necessary, as False is default)
          End If
        End Function
        Incidentally, I think we'll need more information than "doesn't work" to help further. It's a bit vague.

        Comment

        • munkeyb0i
          New Member
          • Nov 2006
          • 5

          #5
          Yeah I finally got it. I just made 2 if statements and it works perfectly.


          'return false if it is not all numbers inputted in the textbox
          If IsNumeric(txtID .Text) = False Then
          MsgBox("Please enter an appropriate ID.", MsgBoxStyle.Exc lamation, "Enter ID Properly")
          txtFID.Focus()
          Return False
          End If

          'return false if it's not 9 digits long
          If Len(txtID.Text) <> 9 Then
          MsgBox("Please enter an appropriate ID.", MsgBoxStyle.Exc lamation, "Enter ID Properly")
          txtFID.Focus()
          Return False
          End If

          Thanks for your help killer

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            No worries. I'm glad it helped.

            I'm curious, though - based on the .Focus method and the Return statement, this is not VB6 or earlier. Must be some version of VB.Net, correct?

            Comment

            • munkeyb0i
              New Member
              • Nov 2006
              • 5

              #7
              Oh yeah...sorry... it's VB.Net. I guess it's a lot different from VB6 eh?

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by munkeyb0i
                Oh yeah...sorry... it's VB.Net. I guess it's a lot different from VB6 eh?
                Details do differ quite a bit, but the logic tends to work the same across lots of languages, so we can often help. Feel free to try us. (As with this thread, you may need to adjust the answers slightly.)

                You could also try the .Net forum when you have questions, as they may have more directly related experience.

                Comment

                • munkeyb0i
                  New Member
                  • Nov 2006
                  • 5

                  #9
                  Originally posted by Killer42
                  Details do differ quite a bit, but the logic tends to work the same across lots of languages, so we can often help. Feel free to try us. (As with this thread, you may need to adjust the answers slightly.)

                  You could also try the .Net forum when you have questions, as they may have more directly related experience.
                  Oh okay then...thanks:D

                  Comment

                  Working...