Solved

Pre lock access form ?

Posted on 2013-10-24
9
653 Views
Last Modified: 2013-12-05
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 ?
0
Comment
Question by:Vaibhavjoshi2005
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 57
ID: 39597001
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.
0
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 500 total points
ID: 39597998
Here is some code I use for this purpose.  Enter "Lock" in the Tag property of each control that needs to be locked, and then you can lock or unlock these controls as needed.  Make a similar function to unlock them, setting the Locked property to False.

Public Sub LockControls(frm As Access.Form)

   Dim ctl As Access.Control
   
On Error Resume Next

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

Open in new window


You could use the Me keyword instead of a frm variable when running the code from a control on the same form.

This method is more flexible than setting the whole form to not allow edits, since you may need to have a checkbox or some other control to call the code that locks or unlocks.
0
 
LVL 1

Author Comment

by:Vaibhavjoshi2005
ID: 39616168
Can somebody help me by giving simple code for locking in VBA. I presume it will not lock the button itself .
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 57
ID: 39616426
Helen has already posted some...or are you looking for something more?

Jim.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39616740
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)

Type LOCK in the tab property
0
 
LVL 1

Author Comment

by:Vaibhavjoshi2005
ID: 39635049
Where do I put the code given by Helen ? On click event of the button ?
0
 
LVL 1

Author Comment

by:Vaibhavjoshi2005
ID: 39635086
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 ?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39635094
<<
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

0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 39635929
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.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question