Re: How to detect all required fields of a form?



On Mon, 19 Mar 2007 18:26:32 -0700, Tom van Stiphout
<no.spam.tom7744@xxxxxxx> wrote:

I just realized the Autonumber portion of my question is no big deal:

blnRequiredField = rs.Fields(ctl.ControlSource).Required Or
(rs.Fields(ctl.ControlSource).Attributes And dbAutoIncrField)

Still struggling with the PK fields though.

-Tom.


I want to indicate requiredness by setting the background color of the
control. Ideally I would call a sub in Form_Load to just do its thing.

Below is what I have so far, but I realize this is of limited use
because both Autonumber fields as well as Primary Key fields not
necessarily have their Required property set, while they are required
for the purpose of the Insert action.

It seems to find out if a field is an Autonumber or PK field can be
complicated.
Does anyone have a brilliant solution?

-Tom.
Code follows:

In the Form_Load event of a form:
SetRequiredFields Me


Public Sub SetRequiredFields(frm As Form)
Dim ctl As Control
Dim rs As DAO.Recordset

Set rs = frm.RecordsetClone

For Each ctl In frm.Controls
If HasProperty(ctl, "ControlSource") Then
if rs.Fields(ctl.ControlSource).Required then
ctl.BackColor = vbRed
End If
Next ctl
Set ctl = Nothing
Set rs = Nothing
End Sub


Private Function HasProperty(ctl As Control, ByVal strPropName As
String)
Dim prop As Property

On Error Resume Next
Set prop = ctl.Properties(strPropName)
HasProperty = (Err.Number = 0)
Set prop = Nothing
End Function


.



Relevant Pages

  • Re: Max Value of Field (with a twist)
    ... I have formatting on the report and if the field ... Dim ctl As Control ... For Each ctl In Me.Controls ... If Me.Controls.Value> varMax Then varMax = ...
    (comp.databases.ms-access)
  • RE: Looping through controls on form and subforms to return control la
    ... Dim ctlName As String ... For Each ctl In Me.Controls ... you will need to identify the subform control on ... How do I loop through all the subforms and concatenate those lists to the ...
    (microsoft.public.access.formscoding)
  • RE: Looping through controls on form and subforms to return contro
    ... Dim ctlName As String ... For Each ctl In Me.Controls ... you will need to identify the subform control on ... How do I loop through all the subforms and concatenate those lists to the ...
    (microsoft.public.access.formscoding)
  • RE: Looping through controls on form and subforms to return contro
    ... Dim ctlName As String ... For Each ctl In Me.Controls ... you will need to identify the subform control on ... How do I loop through all the subforms and concatenate those lists to the ...
    (microsoft.public.access.formscoding)
  • Re: Auto Number
    ... If you want to have "invoice numbers" that you can control, ... The Autonumber datatype exists for the sole purpose of generating ... Create a query with the newly created table as the source ...
    (microsoft.public.access.gettingstarted)