Link to home
Start Free TrialLog in
Avatar of Vaibhavjoshi2005
Vaibhavjoshi2005

asked on

Pre lock access form ?

I have an access form and it also have 5-6 subforms. The form can be used for new record creation or also for display of existing records. Data once entered usually does not change and any inadvertent change should be controlled.
I notice that sometimes while displaying existing records I might by mistake press a key and data might get changed. It will also get saved if by mistake I press ctrl + S.
So I need to implement a procedure whereby while displaying a new record the data should remain in locked mode ( for main form as well as subforms) and only doing something (like unchecking some checkbox) will unlock cells for data entry. How it can be done ?
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Two ways:

1. Set the forms AllowEdit property to false

2. Loop through all controls on the form and set their enabled property to false.

With either, you'll need to provide a button to the user to "unlock" the form and allow editing.

Then a button to "save" or "cancel", both of which will put the form back into a locked state.

Jim.
ASKER CERTIFIED SOLUTION
Avatar of Helen Feddema
Helen Feddema
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vaibhavjoshi2005
Vaibhavjoshi2005

ASKER

Can somebody help me by giving simple code for locking in VBA. I presume it will not lock the button itself .
Helen has already posted some...or are you looking for something more?

Jim.
Not for points, just for clarification...

<<
 I presume it will not lock the button itself .
>>

The code Helen posted will do what you are asking for, locking any controls that you want locked.  You simply need to type "Lock" in the tag property for those controls (see the screenshot below)

The button itself will not get locked unless you specifically type Lock in its Tag property (the code alone will not lock the button)

User generated image
Where do I put the code given by Helen ? On click event of the button ?
Hi, I am able to achieve this ; Now two questions

I have put up code like below ;

Private Sub Command306_Click()
Dim ctl As Access.Control
   
On Error Resume Next

      For Each ctl In Me.Officerssubform.Controls
         Debug.Print ctl.Name
         If ctl.Tag = "Lock" Then
            ctl.Locked = True
            
         End If
      Next ctl
      
End Sub

Open in new window

So I will repliacte this for each of the subform , however the VBA window does not allow me specify the main header form ?

Is there anyway to stop clicking in the field and make field grey also on locking ?
<<
So I will repliacte this for each of the subform , however the VBA window does not allow me specify the main header form ?
>>

Not sure what you mean... try explaining again or post screenshots.

<<Is there anyway to stop clicking in the field and make field grey also on locking ?>>

Setting enabled = False will make the field grey.

         If ctl.Tag = "Lock" Then
            'ctl.Locked = True  -- set enabled = false to make the control grey
             ctl.enabled = false
            
         End If

Open in new window

All you need is the Me keyword -- not the name of the current form.  Here is the code as run from a command button on the form:

   Dim ctl As Access.Control
   
On Error Resume Next

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

Open in new window


This is to lock the controls; to unlock them, make another button that sets the Locked property to False.  Or you could have a little option group (Lock/Unlock), and pick up the value of the selected option to either lock or unlock the controls.  

And do give your controls meaningful names, such as cmdLock.  It makes your code much more readable.