testing for null value in text boxes
HI All,
I am now trying to display a message when I am writing info to a database that warns me to fill in all fileds(text boxes) before the operation(write to the database) can take place.
I want to test all text boxes at once instead of typing each textbox value in an if statement.
aaa
For example:
Instead of typing
if txtBox1.text = "" and txtbox2.text = "" .........
msgbox "Please fill out all fields before writing to database"
end if
I tried the code below, but am getting errors
Private Sub cmdAdd_Click()
Dim Contrl As Control
For Each Contrl In frmForm.Controls
If (TypeOf Contrl Is TextBox) And
frmForm.Contrl.Text = "" Then
MsgBox "Please fill in all fields"
txtBox1.setfocus
End If
Next Contrl
I get a compile error when I hit the Add button. The message I get is Method or data member not found and Contrl is highlighted. I have tried a few other things, but of course they did not work. Any help would be much appreciated.
Thanks in advance!!!
# 1 Re: testing for null value in text boxes
It's your IF..THEN..ENDIF statement. Change it to:
If (TypeOf Contrl Is TextBox) then
If (frmForm.Contrl.Text = "") Then
MsgBox "Please fill in all fields"
Contrl.setfocus
End If
End If
-Cool Bizs
# 2 Re: testing for null value in text boxes
Try this...
Private Sub cmdAdd_Click()
Dim Ctrl As Control
For Each Ctrl In frmForm.Controls
If TypeOf Ctrl Is TextBox then
If trim(Ctrl.Text) = "" Then
MsgBox "Please fill in all fields"
Ctrl.setfocus
'***********
'Sorry, forgot the following line!
'**************
exit sub
End If
End If
Next Ctrl
'--
Explanation: Vb does not circuit IF expression.
It evaluates both parts of an "if condition1 and condition2"
Thus if you test in codition2 for something that applies only to textboxes, it will crash as soon as it entered the if for a commandbutton, even if it should not go on after evaluating
first condition...That is: in Vb, using And & Or n if expressions
is dangerous (and -moreover!- it does not increase the speed!)
Note also that "dim Control as Control" is not a good thing to do,
due to ambiguity!
'--