Solved

Pre lock access form ?

Posted on 2013-10-24
9
612 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
 
LVL 57
ID: 39616426
Helen has already posted some...or are you looking for something more?

Jim.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now