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!!!
[1163 byte] By [OzzyOsbourne] at [2007-11-17 17:04:28]
# 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
coolbiz at 2007-11-10 0:22:50 >
# 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!
'--
Cimperiali at 2007-11-10 0:23:50 >
# 3 Re: testing for null value in text boxes
Thanks Cool Bizs,

I tried your code but it didn't work. I really appreciate your help though. This is a great forum. There's so much to learn about VB. Hopefully some day I will be a "slick" coder like you and the rest who help out on this forum. Thanks again for trying!!!
OzzyOsbourne at 2007-11-10 0:24:52 >
# 4 Re: testing for null value in text boxes
Thanks a million Cimperiali!!!! I really appreciate you helping me.
OzzyOsbourne at 2007-11-10 0:25:57 >