Hi, everybody!
Many times Tep-Tips forum's members ask how to be satisfied that all controls on form are updated. Here's function which checks for any control with Null value on anyone opened form including all subforms. Function returns True if such control was found otherwise False.
Simple copy following code into any module and call function when it's needed.
'*********************************
Public Function IsNotUpdatedControl(Optional frm As Form, _
Optional OnlyRecUpdCtls As Boolean = False) As Boolean
'-------------------------------
'Designed by Aivars Laganovskis
'August 20,2001
'-------------------------------
'Function seeks any control with Null value on given (Me if frm is omitted) form
'including all subforms recursively.
'If found, it returns True otherwise False.
'
'Optional parameter "OnlyRecUpdCtls" >>> (Only Record[fields] Updated controls):
' If it's True then only controls with ControlSource
' to recordset field are to be verified.
' Else all form's controls are verified.
'
On Error GoTo Err_IsNotUpdatedControl
Dim ctl As Control
Dim bln As Boolean
Dim rst As Recordset
Dim fld As Field
Dim prp As Property
If frm Is Nothing Then
'Function was called without first parameter
Set frm = Screen.ActiveForm
End If
If OnlyRecUpdCtls Then
'Must be verified only for controls with
'associated recordset fields
If frm.RecordSource = "" Then
'Form have not RecordSet
GoTo Exit_IsNotUpdatedControl
End If
Set rst = frm.RecordsetClone
If rst.EOF Then
'Empty recordset
GoTo Exit_IsNotUpdatedControl
End If
End If
For Each ctl In frm.Controls
If ctl.Properties("ControlType"
= acSubform Then
'Recursive call: finds null values for controls on subform
bln = IsNotUpdatedControl(ctl.Form, OnlyRecUpdCtls)
Else
For Each prp In ctl.Properties
'Check for property
'Only updatable controls are verified
'ie. controls with property "ControlSource"
If prp.Name = "ControlSource" Then
If OnlyRecUpdCtls Then
'Compare field names of form's recordset
'with control's ControlSource
For Each fld In rst.Fields
If fld.Name = ctl.ControlSource Then
'Only if form's recordset field is updated
'via control, its value is verified
bln = IsNull(ctl)
Exit For
End If
Next fld
Else
bln = IsNull(ctl)
End If
Exit For
End If
Next prp
End If
If bln Then 'Control has Null value
Exit For
End If
Next ctl
IsNotUpdatedControl = bln
Exit_IsNotUpdatedControl:
If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If
Exit Function
Err_IsNotUpdatedControl:
MsgBox "Error No " & Err.Number & vbLf & Err.Description, , "Public Function IsNotUpdatedControl"
Resume Exit_IsNotUpdatedControl
End Function
'*********************************
Examples:
1. Verified for all updatables active form's controls
Private Sub Form_UnLoad(Cancel As Integer)
If IsNotUpdatedControl(Me) = True then
Msgbox "You may update all controls on form!"
Cancel = True
End If
End sub
2. Verified only for controls with associated recordset fields
Private Sub Form_UnLoad(Cancel As Integer)
If IsNotUpdatedControl(Me, True) = True then
Msgbox "You may update all controls on form!"
Cancel = True
End If
End Sub
3. On active form:
MyValue=IsNotUpdatedControl()
MyValue=IsNotUpdatedControl(, True)
MyValue=IsNotUpdatedControl(Me)
MyValue=IsNotUpdatedControl(Me, True)
4. On other form:
MyValue=IsNotUpdatedControl(Forms("MyForm"
)
MyValue=IsNotUpdatedControl(Forms!MyForm, True)
Set frm = Forms!MyForm
MyValue=IsNotUpdatedControl(frm)
MyValue=IsNotUpdatedControl(frm, True)
Good luck!
Aivars |-0
Riga,
Latvia
alaganovskis@hotmail.com
Many times Tep-Tips forum's members ask how to be satisfied that all controls on form are updated. Here's function which checks for any control with Null value on anyone opened form including all subforms. Function returns True if such control was found otherwise False.
Simple copy following code into any module and call function when it's needed.
'*********************************
Public Function IsNotUpdatedControl(Optional frm As Form, _
Optional OnlyRecUpdCtls As Boolean = False) As Boolean
'-------------------------------
'Designed by Aivars Laganovskis
'August 20,2001
'-------------------------------
'Function seeks any control with Null value on given (Me if frm is omitted) form
'including all subforms recursively.
'If found, it returns True otherwise False.
'
'Optional parameter "OnlyRecUpdCtls" >>> (Only Record[fields] Updated controls):
' If it's True then only controls with ControlSource
' to recordset field are to be verified.
' Else all form's controls are verified.
'
On Error GoTo Err_IsNotUpdatedControl
Dim ctl As Control
Dim bln As Boolean
Dim rst As Recordset
Dim fld As Field
Dim prp As Property
If frm Is Nothing Then
'Function was called without first parameter
Set frm = Screen.ActiveForm
End If
If OnlyRecUpdCtls Then
'Must be verified only for controls with
'associated recordset fields
If frm.RecordSource = "" Then
'Form have not RecordSet
GoTo Exit_IsNotUpdatedControl
End If
Set rst = frm.RecordsetClone
If rst.EOF Then
'Empty recordset
GoTo Exit_IsNotUpdatedControl
End If
End If
For Each ctl In frm.Controls
If ctl.Properties("ControlType"
'Recursive call: finds null values for controls on subform
bln = IsNotUpdatedControl(ctl.Form, OnlyRecUpdCtls)
Else
For Each prp In ctl.Properties
'Check for property
'Only updatable controls are verified
'ie. controls with property "ControlSource"
If prp.Name = "ControlSource" Then
If OnlyRecUpdCtls Then
'Compare field names of form's recordset
'with control's ControlSource
For Each fld In rst.Fields
If fld.Name = ctl.ControlSource Then
'Only if form's recordset field is updated
'via control, its value is verified
bln = IsNull(ctl)
Exit For
End If
Next fld
Else
bln = IsNull(ctl)
End If
Exit For
End If
Next prp
End If
If bln Then 'Control has Null value
Exit For
End If
Next ctl
IsNotUpdatedControl = bln
Exit_IsNotUpdatedControl:
If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If
Exit Function
Err_IsNotUpdatedControl:
MsgBox "Error No " & Err.Number & vbLf & Err.Description, , "Public Function IsNotUpdatedControl"
Resume Exit_IsNotUpdatedControl
End Function
'*********************************
Examples:
1. Verified for all updatables active form's controls
Private Sub Form_UnLoad(Cancel As Integer)
If IsNotUpdatedControl(Me) = True then
Msgbox "You may update all controls on form!"
Cancel = True
End If
End sub
2. Verified only for controls with associated recordset fields
Private Sub Form_UnLoad(Cancel As Integer)
If IsNotUpdatedControl(Me, True) = True then
Msgbox "You may update all controls on form!"
Cancel = True
End If
End Sub
3. On active form:
MyValue=IsNotUpdatedControl()
MyValue=IsNotUpdatedControl(, True)
MyValue=IsNotUpdatedControl(Me)
MyValue=IsNotUpdatedControl(Me, True)
4. On other form:
MyValue=IsNotUpdatedControl(Forms("MyForm"
MyValue=IsNotUpdatedControl(Forms!MyForm, True)
Set frm = Forms!MyForm
MyValue=IsNotUpdatedControl(frm)
MyValue=IsNotUpdatedControl(frm, True)
Good luck!
Aivars |-0
Riga,
Latvia
alaganovskis@hotmail.com