Solved

Pre lock access form ?

Posted on 2013-10-24
9
640 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add records to a form to a table 11 40
VBA Access 2016 syntax 6 41
Change date stored as "short text" YYYYMMDD to MM/DD/YYYY? 27 26
Switch 5 16
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…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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…

770 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