Checking Empty textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IraqiAli
    New Member
    • Feb 2007
    • 19

    Checking Empty textbox

    Ok i have 6 textboxes, and the user is required to fill in each of them. so i want to do a check to make sure none are left blank, but im having a little trouble.

    i tried doing

    Code:
    If txtChoice1.text =  "" Then
    MsgBox ("You have missed out a preference.")
    Else
    If txtChoice2.text = ""  Then
    but that never seemed to work.

    please help.
    Last edited by willakawill; Feb 21 '07, 08:04 PM. Reason: please use code tags when posting code
  • scripto
    New Member
    • Oct 2006
    • 143

    #2
    quick and dirty:

    Code:
    If txtChoice1.text = "" Then
         MsgBox ("You have missed out a preference.")
    elseIf txtChoice2.text = "" Then
         MsgBox ("You have missed out a preference.")
    elseIf txtChoice3.text = "" Then
         MsgBox ("You have missed out a preference.")
    end if
    make elseif one word.

    if you're using VB6, look into using arrays of textboxes to make this easier.
    Last edited by willakawill; Feb 21 '07, 08:04 PM. Reason: please use code tags when posting code

    Comment

    • IraqiAli
      New Member
      • Feb 2007
      • 19

      #3
      Originally posted by scripto
      quick and dirty:

      If txtChoice1.text = "" Then
      MsgBox ("You have missed out a preference.")
      elseIf txtChoice2.text = "" Then
      MsgBox ("You have missed out a preference.")
      elseIf txtChoice3.text = "" Then
      MsgBox ("You have missed out a preference.")
      end if

      make elseif one word.

      if you're using VB6, look into using arrays of textboxes to make this easier.
      it still never worked. even though i fill in all the textboxes - i still get the message.

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by IraqiAli
        it still never worked. even though i fill in all the textboxes - i still get the message.
        Try this:
        Code:
        If Len(Trim(txtChoice1.Text)) = 0 Then
        This is a more generic way to do this. It will check all of the textboxes on your form:
        Code:
        Dim tmp As Control
            
            For Each tmp In Me.Controls
                If TypeOf tmp Is TextBox Then
                    If Len(Trim(tmp.Text)) = 0 Then
                        'put your message here
                    End If
                End If
            Next tmp

        Comment

        • TNT
          New Member
          • Feb 2007
          • 48

          #5
          You could use:
          Code:
          If txtChoice1 <> "" Or txtChoice2 <> "" Or txtChoice3 <> "" Or txtChoice4 <> "" Or txtChoice5 <> "" Or txtChoice5 <> "" Then
          'Code goes here
          End If

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by IraqiAli
            it still never worked. even though i fill in all the textboxes - i still get the message.
            That's really odd. Could you copy and paste back here the code which didn't work? Because willakawill's code really should have worked. I'd have to guess that you might have made a mistake in something like a control or variable name.

            Comment

            • IraqiAli
              New Member
              • Feb 2007
              • 19

              #7
              Originally posted by Killer42
              That's really odd. Could you copy and paste back here the code which didn't work? Because willakawill's code really should have worked. I'd have to guess that you might have made a mistake in something like a control or variable name.
              well i couldnt use this code

              Code:
              Dim tmp As Control
                  
                  For Each tmp In Me.Controls
                      If TypeOf tmp Is TextBox Then
                          If Len(Trim(tmp.Text)) = 0 Then
                              'put your message here
                          End If
                      End If
                  Next tmp
              because it checks all the textboxes on the form, but i have other texboxes than the 6 that need to be filled in. But thanks for the code anyway - i used it for something else i was doing.

              i used Len(Trim(tmp.Te xt)) = 0 with If statements and it worked

              thank you everyone for your help

              Comment

              • willakawill
                Top Contributor
                • Oct 2006
                • 1646

                #8
                You can still use a loop like this just by testing for the common name of your control array:
                Code:
                Dim tmp As Control
                    
                    For Each tmp In Me.Controls
                        If StrComp(Left(tmp.Name, 9), "txtChoice") = 0 Then
                            If Len(Trim(tmp.Text)) = 0 Then
                                'put your message here
                            End If
                        End If
                    Next tmp

                Comment

                • IraqiAli
                  New Member
                  • Feb 2007
                  • 19

                  #9
                  Originally posted by willakawill
                  You can still use a loop like this just by testing for the common name of your control array:
                  Code:
                  Dim tmp As Control
                      
                      For Each tmp In Me.Controls
                          If StrComp(Left(tmp.Name, 9), "txtChoice") = 0 Then
                              If Len(Trim(tmp.Text)) = 0 Then
                                  'put your message here
                              End If
                          End If
                      Next tmp
                  that is really helpful. Thanks :)

                  Comment

                  • willakawill
                    Top Contributor
                    • Oct 2006
                    • 1646

                    #10
                    You are very welcome

                    Comment

                    • knychtell
                      New Member
                      • Jan 2007
                      • 66

                      #11
                      Ok , would you like to use an errorprovider, cause it is better rather than to show a pop-up message everytime a user writes wrong, info. and an errorprovider gives a lot of room for mistakes, wihtout giving off too many msgboxes.

                      add this on every txtbox


                      Code:
                      if txtbox1.text=nothing then
                      errorprovider1.seterror(txtbox1, "Txtbox must b\not be left blank!") 
                      e.cancel'

                      w/c tells a user that he must place a valid value in the 1st txtbox before he can leave and jump to another txtbox.


                      add this to the 2 lines above

                      Code:
                      elseif
                       errorprovider.seterror(txtbox1,"")
                      'which lets the user jump to another txtbox if the user entere a right txt or number
                      
                      end sub
                      add this code to every txtboxes you have for validation purposses.

                      actually you can still add validating and validated event handler to every txtbox.

                      hope this may help ya...
                      Last edited by willakawill; Feb 28 '07, 03:40 AM. Reason: please use code tags when posting code

                      Comment

                      • willakawill
                        Top Contributor
                        • Oct 2006
                        • 1646

                        #12
                        errorprovider is a .net class. No mention of .net in the op's question but that does not mean the op is not using .net. A lot of new programmers who come here don't seem to know what .net is. Largely, I guess, because of the new microsoft naming policy vb 2005

                        Comment

                        Working...