J
Joel Wiseheart
For reasons I won't go into, I need to check for required
entries at a form level, not the table level.
I use the following code, which sits in a module for
generic use, to check for required fields at the form
level. It works by putting the word "Required" in the Tag
property on the form controls that are required, and
passing the form name as a variable to the function.
It also uses the caption, not the field name, for the
error mesage displayed to the user (The LEN function, -1,
is used to remove the ":" character at the end of the
label).
The module code is:
Public Function RequiredFieldCheck(frm As Form) As
Integer
'Variables: frm = Form to check for blank fields on.
Dim ctl As Control
Dim str As String
For Each ctl In frm.Controls
If IsNull(ctl) And ctl.Tag = "Required" Then
RequiredFieldCheck = True
str = Left$(ctl.Controls(0).Caption, Len
(ctl.Controls(0).Caption) - 1)
MsgBox ("The required field '" & str & "'
was left blank.")
ctl.SetFocus
Exit For
End If
Next
End Function
And is called by this code on the form:
Private Sub Form_BeforeUpdate(Cancel As Integer)
'Purpose: If record update was not cancelled, check for
required fields left blank.
On Error GoTo Error_Form_BeforeUpdate
If fDelete = False Then
Cancel = RequiredFieldCheck(Me)
End If
Exit_Form_BeforeUpdate:
Exit Sub
Error_Form_BeforeUpdate:
Call ErrorHandlerCustom(Me.Name, "Form_BeforeUpdate")
Resume Exit_Form_BeforeUpdate
End Sub
It works great. That is, if the blank field is on the main
form.
I have been having problems using this same module, if
it's checking for blank fields on a subform. If I use the
Call statement on the subform, and either pass (Me) or
(Forms!formname!subformobject.Form) as the form variable,
I get an error message when it is opened as part of the
main form, saying the form can't be found.
Is this just a problem with the syntax of how I am
referencing the form name passed to the function, or is
there a problem with the function itself? I feel like I'm
missing something obvious here.
Thanks!
entries at a form level, not the table level.
I use the following code, which sits in a module for
generic use, to check for required fields at the form
level. It works by putting the word "Required" in the Tag
property on the form controls that are required, and
passing the form name as a variable to the function.
It also uses the caption, not the field name, for the
error mesage displayed to the user (The LEN function, -1,
is used to remove the ":" character at the end of the
label).
The module code is:
Public Function RequiredFieldCheck(frm As Form) As
Integer
'Variables: frm = Form to check for blank fields on.
Dim ctl As Control
Dim str As String
For Each ctl In frm.Controls
If IsNull(ctl) And ctl.Tag = "Required" Then
RequiredFieldCheck = True
str = Left$(ctl.Controls(0).Caption, Len
(ctl.Controls(0).Caption) - 1)
MsgBox ("The required field '" & str & "'
was left blank.")
ctl.SetFocus
Exit For
End If
Next
End Function
And is called by this code on the form:
Private Sub Form_BeforeUpdate(Cancel As Integer)
'Purpose: If record update was not cancelled, check for
required fields left blank.
On Error GoTo Error_Form_BeforeUpdate
If fDelete = False Then
Cancel = RequiredFieldCheck(Me)
End If
Exit_Form_BeforeUpdate:
Exit Sub
Error_Form_BeforeUpdate:
Call ErrorHandlerCustom(Me.Name, "Form_BeforeUpdate")
Resume Exit_Form_BeforeUpdate
End Sub
It works great. That is, if the blank field is on the main
form.
I have been having problems using this same module, if
it's checking for blank fields on a subform. If I use the
Call statement on the subform, and either pass (Me) or
(Forms!formname!subformobject.Form) as the form variable,
I get an error message when it is opened as part of the
main form, saying the form can't be found.
Is this just a problem with the syntax of how I am
referencing the form name passed to the function, or is
there a problem with the function itself? I feel like I'm
missing something obvious here.
Thanks!