How can I validate an input number as integer in VB6? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How can I validate an input number as integer in VB6?

gogogogo Posts: 120Member
Hi,

I created a command button for the following program. But, when I input letters (not numbers), it will return to print "Four digits or above". How can I validate the input must be integer?

Anyone could give a details instruction to me?

Private Sub Command1_Click()
Dim N As Integer
L10:
N = InputBox("Enter a positive integer:")
If N < 0 Then MsgBox "Positive Integer Pls!! !": GoTo L10
If N < 10 Then
Print "One digit"
ElseIf N < 100 Then
Print "Two digits"
ElseIf N < 1000 Then
Print "Three digits"
Else
Print "Four digits or above"
End If
End Sub

Thanks in advance.

gogo

Comments

  • jacob_miwjacob_miw Posts: 194Member
    : Hi,
    :
    : I created a command button for the following program. But, when I input letters (not numbers), it will return to print "Four digits or above". How can I validate the input must be integer?
    :
    : Anyone could give a details instruction to me?
    :
    : Private Sub Command1_Click()
    : Dim N As Integer
    : L10:
    : N = InputBox("Enter a positive integer:")
    : If N < 0 Then MsgBox "Positive Integer Pls!! !": GoTo L10
    : If N < 10 Then
    : Print "One digit"
    : ElseIf N < 100 Then
    : Print "Two digits"
    : ElseIf N < 1000 Then
    : Print "Three digits"
    : Else
    : Print "Four digits or above"
    : End If
    : End Sub
    :
    : Thanks in advance.
    :
    : gogo
    :

    You can use this:

    'Checks if the number of characters in the inputbox is more than 0 and if it is not numeric
    If len(N) > 0 And Not IsNumeric(N) Then
    MsgBox "Positive integers please!"
    End If

    Of course you'll have to make it fit your program

    Hope it'll work
    jacob_miw

  • gogogogo Posts: 120Member
    Hi,

    Can I write sth like:

    If len(N) > 0 & IsNumeric(N) Then
    MsgBox "Positive integers please!"
    End If

    I don't understand the default value of IsNumeric. Is IsNumeric meant N equals to Numberic value not string?

    Thanks for your kind advice.

    gogo



  • leeosleeos Posts: 1,212Member
    : Hi,
    :
    : Can I write sth like:
    :
    : If len(N) > 0 & IsNumeric(N) Then
    : MsgBox "Positive integers please!"
    : End If
    :
    : I don't understand the default value of IsNumeric. Is IsNumeric meant N equals to Numberic value not string?
    :
    : Thanks for your kind advice.
    :
    : gogo
    :
    :
    :IsNumeric(N) either = True or False depending what N is.

    also you Dim N as Integer right at the begining. So there will always be a type mismatch if N is Alpa numeric


  • KDivad LeahcimKDivad Leahcim Posts: 3,948Member
    : Hi,
    :
    : Can I write sth like:
    :
    : If len(N) > 0 & IsNumeric(N) Then
    : MsgBox "Positive integers please!"
    : End If
    :
    : I don't understand the default value of IsNumeric. Is IsNumeric meant N equals to Numberic value not string?
    :
    : Thanks for your kind advice.
    :
    : gogo

    (Kinda long. I appear to be in a lecturing mood... )

    No, you can't. In VB, the & character is for joining Strings or declaring Longs. (C++'ers hate it! ) An If...Then statement evaluates each section (joined by And or Or) as True or False.
    [code]
    *section 1* * section 2 *
    If Len(N) > 0 And Not IsNumeric(N) Then
    [/code]
    And is a boolean operator, it returns true if both sections are true.

    If they entered something:

    Len(N) > 0

    And it's not a number (Not reverses the True/False value):

    Not IsNumeric(N)

    Then it will execute the section inside the If block, popping up a message saying that an invalid amount was given.

    However, there is a problem with the logic. N is an integer. Len(N) will always return 2 because an integer uses 2 bytes.

    Try this:
    [code]
    Dim N As String
    N = InputBox("Please enter a 4 digit positive integer.")
    If Len(N) = 0 Then

    ElseIf Len(N) = 1 Then

    ElseIf Len(N) = 2 Then

    ElseIf Len(N) = 3 Then

    Else 'All other lengths

    End If
    [/code]
    You can use Val(N) or CInt(N) to make an integer out of the string.

    A better idea than using GoTo is to use Do...Loop:
    [code]
    Dim N As String
    Do
    N = InputBox("Please enter a 4 digit positive integer.")
    Loop Until (IsNumeric(N) And (Val(N) >= 0))
    If Len(N) = 1 Then

    ElseIf Len(N) = 2 Then

    ElseIf Len(N) = 3 Then

    Else 'All other lengths

    End If
    [/code]
    This will make it keep retrying until they enter something that is 0 or above and is a number (strings can do funny things when converted...).

    Hope this helps!

Sign In or Register to comment.