preventing changes on a form/subform...

B

Brad Pears

Instead of locking a form depending on certain data, I would like to simply
prevent any changes from happening. The problem is, this form contains a
subform which contains yet another subform and I want to prevent changes on
those as well...

What is the syntax to prevent changes on a subform?

I used me.allowedits=false for the current form and tried the following for
a subform, but it does not want to work...

First subform:

Forms![Cust Service]![child-Issues].AllowEdits = False

It's subform:
Forms![Cust Service]![child-Issues].form!.[child27].AllowEdits = False

Can anyone help?

Thanks,

Brad
 
A

Allen Browne

It is the form within the subform control that has the AllowEdits property,
so try:
Forms![Cust Service]![child-Issues].Form.AllowEdits = False
Forms![Cust Service]![child-Issues].form![child27].Form.AllowEdits = False

For an explanation, see:
Referring to Controls on a Subform
at:
http://members.iinet.net.au/~allenbrowne/casu-04.html

One of the problems with setting the AllowEdits property is that it even
locks the unbound controls. As a result, I prefer to lock the individual
controls. The code below locks/unlocks all the bound controls on a form,
except for any you list as exceptions. It also locks/unlocks any subforms,
and continues the process recursively:

Public Function LockBoundControls(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 = Trur 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 on " & conMod & " at " &
Now()
End Select
Next

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
Dim vardummy As Variant
On Error Resume Next
vardummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Brad Pears said:
Instead of locking a form depending on certain data, I would like to
simply
prevent any changes from happening. The problem is, this form contains a
subform which contains yet another subform and I want to prevent changes
on
those as well...

What is the syntax to prevent changes on a subform?

I used me.allowedits=false for the current form and tried the following
for
a subform, but it does not want to work...

First subform:

Forms![Cust Service]![child-Issues].AllowEdits = False

It's subform:
Forms![Cust Service]![child-Issues].form!.[child27].AllowEdits = False

Can anyone help?

Thanks,

Brad
 
B

Brad Pears

Does not seem to work. Any other ideas? I get the following...

You entered an expression that has an invalid reference to the property
form/report.

Any ideas?

Thanks,

Brad

Allen Browne said:
It is the form within the subform control that has the AllowEdits property,
so try:
Forms![Cust Service]![child-Issues].Form.AllowEdits = False
Forms![Cust Service]![child-Issues].form![child27].Form.AllowEdits = False

For an explanation, see:
Referring to Controls on a Subform
at:
http://members.iinet.net.au/~allenbrowne/casu-04.html

One of the problems with setting the AllowEdits property is that it even
locks the unbound controls. As a result, I prefer to lock the individual
controls. The code below locks/unlocks all the bound controls on a form,
except for any you list as exceptions. It also locks/unlocks any subforms,
and continues the process recursively:

Public Function LockBoundControls(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 = Trur 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 on " & conMod & " at " &
Now()
End Select
Next

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
Dim vardummy As Variant
On Error Resume Next
vardummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Brad Pears said:
Instead of locking a form depending on certain data, I would like to
simply
prevent any changes from happening. The problem is, this form contains a
subform which contains yet another subform and I want to prevent changes
on
those as well...

What is the syntax to prevent changes on a subform?

I used me.allowedits=false for the current form and tried the following
for
a subform, but it does not want to work...

First subform:

Forms![Cust Service]![child-Issues].AllowEdits = False

It's subform:
Forms![Cust Service]![child-Issues].form!.[child27].AllowEdits = False

Can anyone help?

Thanks,

Brad
 
B

Brad Pears

I stand corrected. Sometimes it works - most of the time it gives me the
error. I have this code running on an "on current" event. The wierd thing is
I have the exact same code running on the "on-click" of a particular check
box and the code runs just fine ALL of the time there.

I can not figure this out. There seems to be no rhyme nor reason as to why
it works one time and not the next!!

If I copy/paste the exact same code from the on-click event, it will work -
once! Then I copy the whole project to my windows 2003 term server to test,
and it fails! I then go back to my developement project and it fails there
too!!!

There must be something really buggy in Access with this code - or I've got
some corruption somewhere...

Thanks,

Brad


Brad Pears said:
Does not seem to work. Any other ideas? I get the following...

You entered an expression that has an invalid reference to the property
form/report.

Any ideas?

Thanks,

Brad

Allen Browne said:
It is the form within the subform control that has the AllowEdits property,
so try:
Forms![Cust Service]![child-Issues].Form.AllowEdits = False
Forms![Cust Service]![child-Issues].form![child27].Form.AllowEdits = False

For an explanation, see:
Referring to Controls on a Subform
at:
http://members.iinet.net.au/~allenbrowne/casu-04.html

One of the problems with setting the AllowEdits property is that it even
locks the unbound controls. As a result, I prefer to lock the individual
controls. The code below locks/unlocks all the bound controls on a form,
except for any you list as exceptions. It also locks/unlocks any subforms,
and continues the process recursively:

Public Function LockBoundControls(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 = Trur 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 on " & conMod & " at " &
Now()
End Select
Next

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
Dim vardummy As Variant
On Error Resume Next
vardummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Brad Pears said:
Instead of locking a form depending on certain data, I would like to
simply
prevent any changes from happening. The problem is, this form contains a
subform which contains yet another subform and I want to prevent changes
on
those as well...

What is the syntax to prevent changes on a subform?

I used me.allowedits=false for the current form and tried the following
for
a subform, but it does not want to work...

First subform:

Forms![Cust Service]![child-Issues].AllowEdits = False

It's subform:
Forms![Cust Service]![child-Issues].form!.[child27].AllowEdits = False

Can anyone help?

Thanks,

Brad
 
B

Brad Pears

Thanks. It's working now!
Allen Browne said:
It is the form within the subform control that has the AllowEdits property,
so try:
Forms![Cust Service]![child-Issues].Form.AllowEdits = False
Forms![Cust Service]![child-Issues].form![child27].Form.AllowEdits = False

For an explanation, see:
Referring to Controls on a Subform
at:
http://members.iinet.net.au/~allenbrowne/casu-04.html

One of the problems with setting the AllowEdits property is that it even
locks the unbound controls. As a result, I prefer to lock the individual
controls. The code below locks/unlocks all the bound controls on a form,
except for any you list as exceptions. It also locks/unlocks any subforms,
and continues the process recursively:

Public Function LockBoundControls(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 = Trur 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 on " & conMod & " at " &
Now()
End Select
Next

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
Dim vardummy As Variant
On Error Resume Next
vardummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Brad Pears said:
Instead of locking a form depending on certain data, I would like to
simply
prevent any changes from happening. The problem is, this form contains a
subform which contains yet another subform and I want to prevent changes
on
those as well...

What is the syntax to prevent changes on a subform?

I used me.allowedits=false for the current form and tried the following
for
a subform, but it does not want to work...

First subform:

Forms![Cust Service]![child-Issues].AllowEdits = False

It's subform:
Forms![Cust Service]![child-Issues].form!.[child27].AllowEdits = False

Can anyone help?

Thanks,

Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top