|
-
Sep 20th, 2001, 01:43 AM
#1
Thread Starter
Member
Multiple Validations in VB 6.0
I'd like to validate input data from option boxes and textboxes. For instance, 3 options and 2 textboxes:
If optOne.value = False And optTwo.value = False And optThree.value = False And txtOne.text = "" And txtTwo.text = "" then
msgbox "enter something"
end if
1. What do I have to do to make the above statement work out? Separate by the types of controls?
2. How do I setfocus to the textbox that doesn't contain anything?
If txtOne.text = "" then
msgbox "enter something in textbox 1"
txtOne.setfocus
end if
if txtTwo.text = "" then
msgbox "enter something in textbox 2"
txtTwo.setfocus
end if
Do I have to do individual if statements for each? There must be a simple way to say that if selections were left blank then bounce to the text box that needs input.
Newbie here so nothing too advanced please. Super appreciate it!
Thanks...............I just love this country.
-
Sep 20th, 2001, 02:06 AM
#2
Frenzied Member
you can write a loop to check for all
but its best to have a statement for each
like your doing
you seem to be on the right track
then again it depends exactly what you are doing
use OR
not AND for opt checking
-
Sep 20th, 2001, 02:06 AM
#3
I'm not sure I totally clear on what you are looking for, since the code you have supplied, seems to in the right direction.
But here's a suggestion, hope it helps you:
Code:
If me.text1.text = "" then
msgbox "Text1 must be filled out"
me.text1.setfocus
exit sub
end if
'..... Do so for each textbox
if not (me.op1.value or me.opt2.value or me.opt3.value) then
msgbox "You must select one of the options"
exit sub
end if
'Put the rest of your code here
-
Sep 20th, 2001, 02:21 AM
#4
-= B u g S l a y e r =-
I'm assuming that ALL the text boxes MUST have a value...
if so, u can use this loop to check the textboxes.
VB Code:
Private Sub Command1_Click()
Dim c As Control
For Each c In Me
'if this is a textbox, check content
If TypeOf c Is TextBox Then
If c.Text = "" Then
MsgBox "need input"
c.SetFocus
Exit Sub
End If
End If
Next
End Sub
for the option controls, I assume only one of them has to be selected. if so u can expand the loop above to this :
VB Code:
Private Sub Command1_Click()
Dim c As Control
Dim bAtleastOneSelected As Boolean
For Each c In Me
'if this is a textbox, check content
If TypeOf c Is TextBox Then
If c.Text = "" Then
MsgBox "need input"
c.SetFocus
Exit Sub
End If
End If
If TypeOf c Is OptionButton Then
If c.Value Then bAtleastOneSelected = True
End If
Next
If Not bAtleastOneSelected Then MsgBox "Select at least one option."
End Sub
-
Sep 20th, 2001, 03:41 AM
#5
Thread Starter
Member
Thanks...
I doodled a little on the boxes for a while and found out that I can't do the if optOne. value = false OR optTwo.value = False OR etc. because that means I can select an option and the other two options will be false and therefore, elicit the my prompt message to make a selection. So, ais_dk. I will try the If Not statement and see how that works. Then I'll test the rest of examples you guys gave me.
I'll be back. Thanks....
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
|