468,253 Members | 1,302 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,253 developers. It's quick & easy.

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.Text 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!
Nov 1 '11 #1
2 2140
Killer42
8,434 Expert 8TB
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.
Dec 18 '11 #2
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.
Expand|Select|Wrap|Line Numbers
  1. If txbAge.Text = String.Empty Then
  2. MsgBox("Wie Alt bist du?")
  3.  
  4. ElseIf txbSemester.Text Is Nothing Then
  5. MsgBox("In welchem Semester bist du?")
  6.  
  7. ElseIf txbMajor.Text = String.Empty Then
  8. MsgBox("Was studierst du")
  9.  
  10. ElseIf female.Checked = False And male.Checked = False Then
  11. MsgBox("Was ist dein Geschlecht?")
  12. Else 
  13. Me.Hide()
  14. AnagramExample.ShowDialog()
  15. End If
  16.  
Jul 22 '20 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Derek | last post: by
7 posts views Thread by Mark | last post: by
reply views Thread by Marty U. | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.