Disable edit all but 1 field?

H

Harmannus

Hallo,

I have code to disable edits and deletions for all the fields on my form if
a criteria is met. See below. How can i make an exception for the statusID
field itself as it also is on the form?

In the Forms Current event:
If Me![StatusID] = 1 Then
Me.AllowEdits = False
Me.AllowDeletions = False
e.g. me.StatusID.locked=false doesn't work....
Else
Me.AllowEdits = true
Me.AllowDeletions = True
End If
Endsub

Thanx for any tips!


Regards,
Harmannus
 
A

Art

Perhaps I have misunderstood you. Is the statusID field
bound to a primary key field in the underlying table? If
so, access will not permit users to change its value.

If you want this value to be editable, you will need to
place it in another text field in the table that is not
the primary key.

Hope this helps
 
D

Dirk Goldgar

Harmannus said:
Hallo,

I have code to disable edits and deletions for all the fields on my
form if a criteria is met. See below. How can i make an exception for
the statusID field itself as it also is on the form?

In the Forms Current event:
If Me![StatusID] = 1 Then
Me.AllowEdits = False
Me.AllowDeletions = False
e.g. me.StatusID.locked=false doesn't work....
Else
Me.AllowEdits = true
Me.AllowDeletions = True
End If
Endsub

Thanx for any tips!

You'll have to leave the form's AllowEdits property set to True, and set
the Locked property of each individual control to either False or True
as appropriate. If there are a lot of controls to be handled in this
way, then instead of listing them individually you can loop through the
form's Controls collection. Note that only data controls have a Locked
property, so your code should either check the type of a control before
attempting to set the Locked property, or else trap and ignore the error
that is raised if you try to set it when it doesn't exist, or else find
a different way of identifying the controls to be locked/unlocked.

For example, in form design view you could set the Tag property of each
of the controls to be locked/unlocked to, say the tage "Lock". You
would exclude non-data controls and the StatusID control. Then you
could use code like this:

' This sub must go in the form's module.
Private Sub LockUnlock(LockIt As Boolean)

Dim ctl As Control

For Each ctl In Me.Controls
If ctl.Tag = "Lock" Then
ctl.Locked = LockIt
End If
Next ctl

End Sub

and in your Current event:

If Me![StatusID] = 1 Then
Me.AllowDeletions = False
LockUnlock True
Else
Me.AllowDeletions = True
LockUnlock False
End If
 
H

Harmannus

Hallo,

Thanx for the tip!

Works great. One thing though. Changing the statusID to 1 (so lock) and
saving with a save button (code= DoCmd.DoMenuItem acFormBar, acRecordsMenu,
acSaveRecord, , acMenuVer70) doesn't update the lock value if i stay in the
changed record. Records are still editable. Tried some requiry commands but
that doesn't have effect.

Any suggestions how to get the records updated....


Regards,
Harmannus


Dirk Goldgar said:
Harmannus said:
Hallo,

I have code to disable edits and deletions for all the fields on my
form if a criteria is met. See below. How can i make an exception for
the statusID field itself as it also is on the form?

In the Forms Current event:
If Me![StatusID] = 1 Then
Me.AllowEdits = False
Me.AllowDeletions = False
e.g. me.StatusID.locked=false doesn't work....
Else
Me.AllowEdits = true
Me.AllowDeletions = True
End If
Endsub

Thanx for any tips!

You'll have to leave the form's AllowEdits property set to True, and set
the Locked property of each individual control to either False or True
as appropriate. If there are a lot of controls to be handled in this
way, then instead of listing them individually you can loop through the
form's Controls collection. Note that only data controls have a Locked
property, so your code should either check the type of a control before
attempting to set the Locked property, or else trap and ignore the error
that is raised if you try to set it when it doesn't exist, or else find
a different way of identifying the controls to be locked/unlocked.

For example, in form design view you could set the Tag property of each
of the controls to be locked/unlocked to, say the tage "Lock". You
would exclude non-data controls and the StatusID control. Then you
could use code like this:

' This sub must go in the form's module.
Private Sub LockUnlock(LockIt As Boolean)

Dim ctl As Control

For Each ctl In Me.Controls
If ctl.Tag = "Lock" Then
ctl.Locked = LockIt
End If
Next ctl

End Sub

and in your Current event:

If Me![StatusID] = 1 Then
Me.AllowDeletions = False
LockUnlock True
Else
Me.AllowDeletions = True
LockUnlock False
End If

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
D

Dirk Goldgar

Harmannus said:
Hallo,

Thanx for the tip!

Works great. One thing though. Changing the statusID to 1 (so lock)
and saving with a save button (code= DoCmd.DoMenuItem acFormBar,
acRecordsMenu, acSaveRecord, , acMenuVer70) doesn't update the lock
value if i stay in the changed record. Records are still editable.
Tried some requiry commands but that doesn't have effect.

Any suggestions how to get the records updated....

If all you have in the Current event is the code to lock the form, then
you can just call that event procedure after the code that saves the
record. Just add the line

Call Form_Current

where appropriate.
 
H

Harmannus

Hallo,

Thanx for the reply and code!

Any idea how much this code (as mentioned in earlier messages) effects the
performance of the active table/field?


Regards,
Harmannus
 
D

Dirk Goldgar

Harmannus said:
Hallo,

Thanx for the reply and code!

Any idea how much this code (as mentioned in earlier messages)
effects the performance of the active table/field?


Regards,
Harmannus

I'm not sure I understand your question; I'm not sure what you mean by
"the performance of the active table/field". But simply calling the
Form_Current event procedure should have no perceptible impact on
performance or responsiveness of the form, unless there is more code in
that procedure than what we have been discussing.
 
H

Harmannus

Hallo,

Thanx for the answer. You interpreted my question correctly. Just wondering
if there would be performance issues....

Regards,

Harmannus
 

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

Similar Threads


Top