can't lock a subform in subform current event

  • Thread starter BBC via AccessMonster.com
  • Start date
B

BBC via AccessMonster.com

I have a sub form that has an Edit/Lock Button to allow/stop the editing of
the records. The Button is on the mainform (code below) and when clicked it
performs as desired to lock/unlock of the subform. I also need to set the
subform (records) to locked when the user moves to another record on the
subform (records should seldom ever be directly changed even on his subform,
thus the edit button). I tried putting the code farther below in the
subforms current event so that any record movements in the subform will
relock the form. The LOCKED property of the subform is not an option for the
form in the current event (intellisense does not show it and I get an error
if I put it there anyway) . The rest of the subforms properties appear to
work.

The Edit/Lock buttons code (located on the main form) Works as expected
including lockingunlocking the subform.

sFranchisedRegSummary is the name of the subform

Private Sub EditRegSummary_Click()
If Me!sFranchiseRegSummary.Locked Then ' is record on subform locked
Me!sFranchiseRegSummary.Locked = False ' open for editing
Me.EditRegSummary.Caption = "Lock" ' show re-lock caption on Button
Else
Me!sFranchiseRegSummary.Form.Dirty = False ' save the existing
record on subform
Me!sFranchiseRegSummary.Locked = True ' lock subform
Me.fSelectRecd.SetFocus ' combobox record selector back on main
form
Me.EditRegSummary.Caption = "Edit" 'reset mainform Edit Button
caption for next record
End If
End Sub

The subforms current event

Private Sub Form_Current() ' current on subform
Me.ID.SetFocus ' set focus to the subform (ex coming from the mainform)
If Me.Dirty Then Me.Dirty = False ' save the subforms record if needed
when record moves

The following line fails to work. I have tried many forms of
this line

Me.Locked=True ' Lock the subform THIS IS WHAT FAILS

other attempts at coding this have been similar to
Me!sFranchiseRegSummary.Form.Locked
Me.sFranchiseRegSummary.Locked
etc.
If Me.Parent!EditRegSummary.Caption = "Lock" Then
On Error Resume Next
Me.Parent!fSelectRecd.SetFocus ' need 2 of these as the first one
occassionally fails
Me.Parent!fSelectRecd.SetFocus ' and I don't know why (yet)
Me.Parent!EditRegSummary.Caption = "Edit"
End If
End Sub


I have temporarly dealt with this issue by putting the following code in the
main forms
fSelectRecd_GotFocus event which happens at the end of the above event

Me!sFranchiseRegSummary.Locked = True
as per

Private Sub fSelectRecd_GotFocus() ' main form's "find a record" combobox
Me!sFranchiseRegSummary.Locked = True ' added as per the locking
problem
If IsNull(Me![ID]) Then DoCmd.GoToRecord , , acLast
' in case no record selected or empty/new record (should
not happen)
End Sub

This seems to work but doesn't seem the "right" way to code it?
Any thoughts on the "proper" way to do this??
 
A

Allen Browne

Since you cannot lock the subform control while it has the focus, you are
looking for an alternative approach to achieve the same goal.

Perhaps you could just set the subform's AllowEdits to No, while leaving its
AllowAdditions as Yes. This lets you add new records without being able to
modify existing ones.

Alternatively, you could use code to loop through all the controls on the
main form and subforms, and set the Locked property of the controls. Here's
some code that does that:
Locking bound controls on a form and subforms
at:
http://allenbrowne.com/ser-56.html

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

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


BBC via AccessMonster.com said:
I have a sub form that has an Edit/Lock Button to allow/stop the editing
of
the records. The Button is on the mainform (code below) and when clicked
it
performs as desired to lock/unlock of the subform. I also need to set the
subform (records) to locked when the user moves to another record on the
subform (records should seldom ever be directly changed even on his
subform,
thus the edit button). I tried putting the code farther below in the
subforms current event so that any record movements in the subform will
relock the form. The LOCKED property of the subform is not an option for
the
form in the current event (intellisense does not show it and I get an
error
if I put it there anyway) . The rest of the subforms properties appear to
work.

The Edit/Lock buttons code (located on the main form) Works as expected
including lockingunlocking the subform.

sFranchisedRegSummary is the name of the subform

Private Sub EditRegSummary_Click()
If Me!sFranchiseRegSummary.Locked Then ' is record on subform locked
Me!sFranchiseRegSummary.Locked = False ' open for editing
Me.EditRegSummary.Caption = "Lock" ' show re-lock caption on
Button
Else
Me!sFranchiseRegSummary.Form.Dirty = False ' save the existing
record on subform
Me!sFranchiseRegSummary.Locked = True ' lock subform
Me.fSelectRecd.SetFocus ' combobox record selector back on main
form
Me.EditRegSummary.Caption = "Edit" 'reset mainform Edit Button
caption for next record
End If
End Sub

The subforms current event

Private Sub Form_Current() ' current on subform
Me.ID.SetFocus ' set focus to the subform (ex coming from the
mainform)
If Me.Dirty Then Me.Dirty = False ' save the subforms record if
needed
when record moves

The following line fails to work. I have tried many forms of
this line

Me.Locked=True ' Lock the subform THIS IS WHAT FAILS

other attempts at coding this have been similar to
Me!sFranchiseRegSummary.Form.Locked
Me.sFranchiseRegSummary.Locked
etc.
If Me.Parent!EditRegSummary.Caption = "Lock" Then
On Error Resume Next
Me.Parent!fSelectRecd.SetFocus ' need 2 of these as the first one
occassionally fails
Me.Parent!fSelectRecd.SetFocus ' and I don't know why (yet)
Me.Parent!EditRegSummary.Caption = "Edit"
End If
End Sub


I have temporarly dealt with this issue by putting the following code in
the
main forms
fSelectRecd_GotFocus event which happens at the end of the above event

Me!sFranchiseRegSummary.Locked = True
as per

Private Sub fSelectRecd_GotFocus() ' main form's "find a record"
combobox
Me!sFranchiseRegSummary.Locked = True ' added as per the locking
problem
If IsNull(Me![ID]) Then DoCmd.GoToRecord , , acLast
' in case no record selected or empty/new record (should
not happen)
End Sub

This seems to work but doesn't seem the "right" way to code it?
Any thoughts on the "proper" way to do this??
 
B

BBC via AccessMonster.com

Thanks, your (first) idea makes absolute sense. Never even thought about
that.
How easy it is to miss the simpler things in life.

Allen said:
Since you cannot lock the subform control while it has the focus, you are
looking for an alternative approach to achieve the same goal.

Perhaps you could just set the subform's AllowEdits to No, while leaving its
AllowAdditions as Yes. This lets you add new records without being able to
modify existing ones.

Alternatively, you could use code to loop through all the controls on the
main form and subforms, and set the Locked property of the controls. Here's
some code that does that:
Locking bound controls on a form and subforms
at:
http://allenbrowne.com/ser-56.html
I have a sub form that has an Edit/Lock Button to allow/stop the editing
of
[quoted text clipped - 80 lines]
This seems to work but doesn't seem the "right" way to code it?
Any thoughts on the "proper" way to do this??
 

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