|
-
Nov 19th, 2007, 04:38 PM
#1
Thread Starter
New Member
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)
-
Nov 19th, 2007, 05:14 PM
#2
Hyperactive Member
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
-
Nov 19th, 2007, 05:22 PM
#3
Thread Starter
New Member
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
-
Nov 19th, 2007, 07:16 PM
#4
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)
-
Nov 19th, 2007, 07:17 PM
#5
Hyperactive Member
Re: Validation question!
 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)
-
Nov 20th, 2007, 08:43 AM
#6
Re: Validation question!
 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
-
Nov 20th, 2007, 09:04 AM
#7
Hyperactive Member
Re: Validation question!
 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
-
Nov 21st, 2007, 12:32 PM
#8
Thread Starter
New Member
Re: Validation question!
Yes thank you all for your input. My program is now working perfectly
-
Nov 21st, 2007, 12:34 PM
#9
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.
-
Nov 21st, 2007, 07:43 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|