Disable all controls in 'details' section

2019-05-24 21:10发布

问题:

I am looking for some command which will disable all controls present in the 'details' section of an MS Access Form. One way is to disable each and every control independently. I am looking for a control-independent way which would disable all the controls present in the details section of MS Access Form.

This feature might be useful, if the form is to be enabled based on some login credentials and the fields for entering credentials is placed in the header section of the form. So, as soon as user does login, all the controls in that form are enabled. Uplon logout, they shall be disabled.

I tried searching for it but it seems the same is not supported. Or no one had such a requirement before.

Please help me if anyone knows.

回答1:

A form has various properties that match this requirement: Allow Edits, Allow Additions and Allow Deletions. Allow Edits works for unbound controls, it even works on unbound forms.

If you have a requirement to disable certain controls, you can set the tab property for these controls to a string and then iterate through the form's control collection to disable those controls.

Dim frm As Form
Dim ctl As Control

Set frm = Forms!MyOpenForm

For Each ctl In frm.Controls

    If ctl.ControlType <> acLabel And ctl.ControlType <> acTabCtl Then
        If ctl.Tag = "AdminHide" Then
            If varWho = "Authorized" Then
                ctl.Visible = True
            Else
                ctl.Visible = False
            End If
        End If

    End If

Next


回答2:

You can disable all of the controls in the detail section directly without having to bother with tags:

Dim ctrl As Control
For Each ctrl In Detail.Controls
    If (TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox)
        ctrl.Enabled = False
    End If
Next

Similarly, you can get at the controls in the header and footer using FormHeader.Controls and FormFooter.Controls.

I usually prefer to use ctrl.Locked = True instead of ctrl.Enabled = False since users can still copy text from locked controls and use text filters on them, but that's up to you.