In one quick swoop!
Here is a function that either locks or unlocks the controls on a form. You
pass the form as a form object, True to Lock or False to Unlock, and the name
of any controls you want to exclude (Like your combo).
I don't know where I got this code, I didn't write it.
Put the code below in a standard module so you can use it from any form.
There are two functions. The first calls the second.
As to making the controls that contain values appear to be blank, it can be
done. As you know, Access always show the data for some record. The only
time you see the controls blank is if you are on a new record. But, being the
sneaky devil I am, here is a way to do it. I haven't written the code, but
your post made me think about it.
Write a function (or you could add it to this one) that loops through the
controls and saves the Forecolor for each control so you know what color the
text originally was and sets the Forecolor to the same color as the
Backcolor. Now, it appears to be blank
Then to turn the text back on, you would have to go through the controls and
set the forecolor to what it used to be.
and one more thing
TERMINOLOGY ALERT!!!!
"Is there a way to enable ALL fields in a form only after a combo box
selection? I am not worried about enabling/diabling controls, only ALL the
fields in a particular form."
Regardless of what a lot of Microsoft documentation says, in true database
terminology, there are no field on a form. The form's recordset has fields,
but the form itself only has controls. A text box is a control, a combo box
is a control, etc.
Public Function LockBoundControls(ByVal frm As Form, bLock As Boolean,
ParamArray avarExceptionList())
On Error GoTo Err_Handler
'Purpose: Lock the bound controls and prevent deletes on the form any
its subforms.
'Arguments frm = the form to be locked
' bLock = True to lock, False to unlock.
' avarExceptionList: Names of the controls NOT to lock
(variant array of strings).
'Usage: Call LockBoundControls(Me. True)
Dim ctl As Control 'Each control on the form
Dim lngI As Long 'Loop controller.
Dim bSkip As Boolean
'Save any edits.
If frm.Dirty Then
frm.Dirty = False
End If
'Block deletions.
frm.AllowDeletions = Not bLock
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acOptionGroup, acCheckBox,
acOptionButton, acToggleButton
'Lock/unlock these controls if bound to fields.
bSkip = False
For lngI = LBound(avarExceptionList) To UBound(avarExceptionList)
If avarExceptionList(lngI) = ctl.Name Then
bSkip = True
Exit For
End If
Next
If Not bSkip Then
If HasProperty(ctl, "ControlSource") Then
If Len(ctl.ControlSource) > 0 And Not ctl.ControlSource
Like "=*" Then
If ctl.Locked <> bLock Then
ctl.Locked = bLock
End If
End If
End If
End If
Case acSubform
'Recursive call to handle all subforms.
bSkip = False
For lngI = LBound(avarExceptionList) To UBound(avarExceptionList)
If avarExceptionList(lngI) = ctl.Name Then
bSkip = True
Exit For
End If
Next
If Not bSkip Then
If Len(Nz(ctl.SourceObject, vbNullString)) > 0 Then
ctl.Form.AllowDeletions = Not bLock
ctl.Form.AllowAdditions = Not bLock
Call LockBoundControls(ctl.Form, bLock)
End If
End If
Case acLabel, acLine, acRectangle, acCommandButton, acTabCtl,
acPage, acPageBreak, acImage, acObjectFrame
'Do nothing
Case Else
'Includes acBoundObjectFrame, acCustomControl
Debug.Print ctl.Name & " not handled " & Now()
End Select
Next
'Set the visual indicators on the form.
On Error Resume Next
frm.cmdLock.Caption = IIf(bLock, "Un&lock", "&Lock")
frm!rctLock.Visible = bLock
Exit_Handler:
Set ctl = Nothing
Exit Function
Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description
Resume Exit_Handler
End Function
Public Function HasProperty(obj As Object, strPropName As String) As Boolean
'Purpose: Return true if the object has the property.
Dim varDummy As Variant
On Error Resume Next
varDummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function
:
Hi,
Is there a way to enable ALL fields in a form only after a combo box
selection? I am not worried about enabling/diabling controls, only ALL
the fields in a particular form. (It would be bonus if anyone could
tell me how to get rid of data showing up in disabled fields. I want
disabled fields to show no data until they "come to life" after a value
is chosen from the combobox)
I have played around with setting each individ field to visible=false
and then visible = true with "After Update" in combo box and this works
fine but I want to know if there is a quick way to set it such that in
one quick swoop, you can enable "disabled" fields after a combobox
selection...there has to be some code out there...like
Forms![Form_Name].fields.all.enabled=true...I tried this, didn't work.
I want to avoid having to code for each individ. field. every time one
has to be added at a later date.
Thx.
S