Results 1 to 5 of 5

Thread: Multiple Validations in VB 6.0

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2001
    Location
    Hawaii
    Posts
    43

    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.
    Cynde

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    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

  3. #3
    AIS_DK
    Guest
    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

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    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:
    1. Private Sub Command1_Click()
    2.     Dim c As Control
    3.     For Each c In Me
    4.         'if this is a textbox, check content
    5.         If TypeOf c Is TextBox Then
    6.             If c.Text = "" Then
    7.                 MsgBox "need input"
    8.                 c.SetFocus
    9.                 Exit Sub
    10.             End If
    11.         End If
    12.     Next
    13. 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:
    1. Private Sub Command1_Click()
    2.     Dim c As Control
    3.     Dim bAtleastOneSelected As Boolean
    4.     For Each c In Me
    5.         'if this is a textbox, check content
    6.         If TypeOf c Is TextBox Then
    7.             If c.Text = "" Then
    8.                 MsgBox "need input"
    9.                 c.SetFocus
    10.                 Exit Sub
    11.             End If
    12.         End If
    13.        
    14.         If TypeOf c Is OptionButton Then
    15.             If c.Value Then bAtleastOneSelected = True
    16.         End If
    17.     Next
    18.     If Not bAtleastOneSelected Then MsgBox "Select at least one option."
    19.    
    20. End Sub
    -= a peet post =-

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2001
    Location
    Hawaii
    Posts
    43

    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....
    Cynde

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