Locking Form on Open (1 Viewer)

forms_are_nightmares

Registered User.
Local time
Today, 08:40
Joined
Apr 5, 2010
Messages
71
Hope this is easy and I'm just overlooking the obvious.

Scenario:

I have one form that two groups of users use. One group edits the form; the other won't. Rather than writing code to disable all the controls, boxes...etc. on the form, I was hoping there was a way to "lock" the form when it's opened.

Setup:
Two buttons on different forms direct the users to the above form. How can I code the one button to lock the form?

Any help is always appreciated.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:40
Joined
Feb 19, 2002
Messages
43,474
Code:
If somecondition is true Then
    Me.AllowEdits = False
    Me.AllowDeletions = False
    Me.AllowAdditions = False
Else
    Me.AllowEdits = True
    Me.AllowDeletions = True
    Me.AllowAdditions = True
End If
 

GT_engineer

Registered User.
Local time
Today, 11:40
Joined
Aug 23, 2012
Messages
85
would this lock the form and all of its records. What if only certain records are locked; could a On_Current work???
 

bob fitz

AWF VIP
Local time
Today, 16:40
Joined
May 23, 2011
Messages
4,726
would this lock the form and all of its records. What if only certain records are locked; could a On_Current work???
If you put Pat's code in the On Current event of the form, then each record will be locked/unlocked, depending on the value of the expression used in the If/Then statement.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:40
Joined
Feb 19, 2002
Messages
43,474
Where you put the code depends on what criteria you use to determine the lock. If it is by user, then you only need to do it once when the form opens. If it is by some data value then you need to put the code in the form's current event so it runs for each new record.

There are lots of form level events and it is important to understand what they are used for. In the situation we are discussing, you could always use the current event and that would work regardless if your criteria was userID or some data value. The point is, running code for every record is inefficient when you only need to run it once when the form opens. Get in the habit of understanding the "footprint" of your application and do not use resources wastefully.

You should also keep in mind that if you change the parameters of your question, you will most likely get a different answer. In this case, the code stayed the same because you wanted to do the same thing but the reason changed and that changed the recommended event.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 16:40
Joined
Sep 12, 2006
Messages
15,709
the only thing is, that locking all the controls also locks things like unbound comboboxes which you may use for other reasons.

because of this, sometimes it is better to lock selected controls in code, rather than the whole form

note that even if the form is locked for edits, a command button, (vba code) can still be used to change data.


a different approach might even be to have two forms - one for each class of users, which b
puts the coding problem into a different area (ie your switchboard) - but gives you the flexibility of designing the form completely differently in each case.
 

Users who are viewing this thread

Top Bottom