There are several ways you could approach this. One would be the verbose one, like this (each method requires adding code to the top of the On_Click event for the button:
- dim bErr as boolean
-
bErr=false
-
If len(nz(me.textbox1))=0 then bErr=true
-
If len(nz(me.textbox2))=0 then bErr=true
-
If len(nz(me.textbox3))=0 then bErr=true
-
...
-
if bErr then
-
msgbox "At least one field has been left blank"
-
exit sub
-
end if
-
Or, a much more elegant solution would be something like this. Decide on a convention to use that will identify each of these problem boxes (It is assumed some text boxes might be okay to be blank). Your convention might be to name these text boxes in a similar manner. I prefer to use the Tag property. Set the Tag property for each required field to some value, like "required." Then your code could look like this
- dim ctl as control
-
dim bErr as boolean
-
for each ctl in me.Controls
-
if ctl.tag = "required" and len(nz(ctl.value))=0 then bErr=true
-
next
-
if bErr then
-
msgbox "At least one field has been left blank"
-
end if
-
-
Or this would add the names of the fields that are blank
- dim ctl as control
-
dim bErr as boolean
-
dim msg as string
-
-
for each ctl in me.Controls
-
if ctl.tag = "required" and len(nz(ctl.value))=0 then
-
bErr=true
-
msg = msg & ctl.name & vbnewline
-
end if
-
next
-
if bErr then
-
msgbox "These fields have been left blank." & vbnewline & msg
-
exit sub
-
end if
-
-
And there are numerous other ways to approach this.
Jim