Checking for empty text boxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jamie Taxer
    New Member
    • Oct 2011
    • 6

    Checking for empty text boxes

    I am using Visual Basic for a Psychology experiment and I would like to check and see if the participants answered all of the questions on one form before moving on to the next. So far this is what I have done and it is not working.

    If txbAge.Text = String.Empty Then
    MsgBox("Wie Alt bist du?")
    Else : Me.Hide()
    AnagramExample. ShowDialog()
    End If
    If female.Checked = False And male.Checked = False Then
    MsgBox("Was ist dein Geschlecht?")
    Else : Me.Hide()
    AnagramExample. ShowDialog()
    End If
    If txbMajor.Text = String.Empty Then
    MsgBox("Was studierst du")
    Else : Me.Hide()
    AnagramExample. ShowDialog()
    End If
    If txbSemester.Tex t Is Nothing Then
    MsgBox("In welchem Semester bist du?")
    Else : Me.Hide()
    AnagramExample. ShowDialog()
    End If

    Using this code, it does check to see if the textboxes are empty but if the participant clicks on the next button without answering any of the questions the message boxes appear one after the other and then the next form appears without giving the participant the chance to answer the items they missed. I am sure this code is not the most efficient or best way to do this (obviously since it isn't working) and would appreciate any and all help. I am completely new at programming.

    Thanks!
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    That's because you are not stopping after you check each one. I'd suggest adding Exit Sub after each MsgBox.

    Also, I'd recommend removing the extra ":" after each Else.

    By the way, judging from the syntax, I'd guess you'll get a better response in the Visual Basic .Net forum. This one is for much older versions of VB, which work a bit differently.

    Comment

    • Otekpo Emmanuel
      New Member
      • Dec 2013
      • 23

      #3
      While is not working is because you are asking the program to move to the next form if any of the textboxes is not empty.
      You are supposed to check for all textboxes before call .ShowDialog
      Try the below codes I believed it should work and post your feedback.
      Code:
      If txbAge.Text = String.Empty Then
      MsgBox("Wie Alt bist du?")
      
      ElseIf txbSemester.Text Is Nothing Then
      MsgBox("In welchem Semester bist du?")
      
      ElseIf txbMajor.Text = String.Empty Then
      MsgBox("Was studierst du")
      
      ElseIf female.Checked = False And male.Checked = False Then
      MsgBox("Was ist dein Geschlecht?")
      Else 
      Me.Hide()
      AnagramExample.ShowDialog()
      End If

      Comment

      Working...