Reputation: 38
There is a similar question to this but it is for C#, Check if TextBox is empty and return MessageBox?.
There is another solution that check if textbox is empty https://www.daniweb.com/software-development/visual-basic-4-5-6/threads/414651/checking-if-textbox-is-empty, but this works if you are going to check all the textbox in the form. I would like to check some textbox in the form if they are empty or not.
I've written this code to check if textboxes are empty
Private sub checkEmpty()
If text1.text = "" Or text2.text="" Then
blank = true
End If
End Sub
Then added this code to my command button
Private Sub command1_Click()
checkEmpty
If blank = true Then
Msgbox "a text box is empty"
Else
Msgbox "Text box has text"
End If
End Sub
The problem when I start the program it gives the output "Text box has text"
even if there are no text in the text boxes.
What is wrong with this code?
Upvotes: 1
Views: 6357
Reputation: 125651
You need to change your procedure to a function that returns a value (I'd change the name at the same time to make it more clear what it does).
Private Function AnyTextBoxEmpty() As Boolean
AnyTextBoxEmpty = text1.Text = "" or text2.Text = ""
End Function
Private Sub command1_Click()
If AnyTextBoxEmpty Then
Msgbox "a text box is empty"
Else
Msgbox "Text box has text"
End If
End Sub
Upvotes: 4