Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pre lock access form ?

Posted on 2013-10-24
9
Medium Priority
?
688 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 58
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 2000 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 58
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

704 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