Results 1 to 10 of 10

Thread: Validation question!

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    12

    Question Validation question!

    Hi guys,

    I have an array (person) and I want to validate it. The input box comes up and asks the user to enter a number. If the user enters a number less than 0 I want it to display a message box saying "Invalid number" and then not add the invalid number to the listbox.
    I would then like it to return to the input box asking for the person age and continue doing so until they enter a number of 0 or more.

    Could someone please adapt this coding to help; I've being trying all night




    Do
    age(person) = InputBox("Enter person's age")
    Loop While age(person) < 0
    lstage.AddItem age(person)

  2. #2
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Validation question!

    Code:
    Option Explicit
    Const aMin = 10
    Const aMax = 105
    Const FieldOK = "OK to enter"
    
    Private Sub Command1_Click()
    Text1.Text = ""
    Label1.Caption = "Enter number in range " & aMin & " to " & aMax
    Command1.Enabled = False
    Text1.Enabled = True
    Text1.SetFocus
    End Sub
    
    Private Sub Form_Load()
    Text1.Enabled = False
    Text1.Text = "": Label2.Caption = ""
    End Sub
    
    Private Sub Text1_Change()
    If Text1.Text = "" Then Label2.Caption = "No value": Exit Sub
    If IsNumeric(Text1.Text) Then
      Select Case Val(Text1.Text)
      Case Is < aMin: Label2.Caption = "Too young"
      Case Is > aMax: Label2.Caption = "Too old"
      Case Else: Label2.Caption = FieldOK
      End Select
    Else
      Label2.Caption = "Non-numeric"
    End If
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii <> 13 Then Exit Sub
    If Label2.Caption <> FieldOK Then
      MsgBox "Error"
      Exit Sub
    End If
    MsgBox "Age Entered: " & Val(Text1.Text)
    Text1.Enabled = False: Command1.Enabled = True
    End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    12

    Re: Validation question!

    Hi there thanks very much for your reply but that's far too advanced for my level. I am only a very basic user and if I remember right it was only an extra line (maybe 2) to get that to work

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Validation question!

    Code:
    Dim strTemp As String
    Dim bOK As Boolean
    
    Do
        bOK = False
        strTemp = InputBox("Enter person's age")
        If Val(strTemp) > 0 Then
            age(person) = Val(strTemp)
            bOK = True
        End If
    Loop While Not bOK
    lstage.AddItem age(person)

  5. #5
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Validation question!

    Quote Originally Posted by Jin666
    Hi there thanks very much for your reply but that's far too advanced for my level. I am only a very basic user and if I remember right it was only an extra line (maybe 2) to get that to work
    Do
    age(person) = InputBox("Enter person's age")
    if age(person) < 0 then msgbox "Age is less than zero"
    Loop While age(person) < 0
    lstage.AddItem age(person)

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Validation question!

    Quote Originally Posted by Mr.Mac
    Do
    age(person) = InputBox("Enter person's age")
    if age(person) < 0 then msgbox "Age is less than zero"
    Loop While age(person) < 0
    lstage.AddItem age(person)
    You might also want to check for over 100

  7. #7
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Validation question!

    Quote Originally Posted by Hack
    You might also want to check for over 100
    LOL! True. But if you see my first response here, I went to pains to check for errors of all kinds. But the OP complained that what was really required was a simplistic specific answer to the original question as posted.

    So I did that, and only that, as requested.

    Now to see if the OP is polite enough to return and acknowledge that.

    Mac

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    12

    Re: Validation question!

    Yes thank you all for your input. My program is now working perfectly

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Validation question!

    If you consider this resolved, you could help us out by pulling down the Thread Tools menu and clicking the Mark Thread Resolved menu item. That will let everyone know that you have your answer.

    Thank you.

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Validation question!

    You can try converting to a byte (range 0 - 255) and handle the error from out of range values... unless you plan to allow an age of more than 255 years.

    Code:
    Option Explicit
    
    Private Sub Form_Load()
       Do While Not IsValidAge(InputBox("Enter person's age"))
          MsgBox "Invalid value for age"
       Loop
    End Sub
    
    Public Function IsValidAge(strAge As String) As Boolean
    Dim bytAge As Byte
    
    On Error GoTo ErrHandler
       bytAge = CByte(strAge)
       IsValidAge = True
       Exit Function
    
    ErrHandler:
       Err.Clear
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width