Lock Particular fields in a form

rajeshsarin

Registered User.
Local time
Today, 23:38
Joined
Sep 25, 2009
Messages
50
Folks,

I have a main form (Client), which has 15 subforms).

Once a client is created, I wish to the Client_No, DOB, FName & LName fields. A few other fields on the main form (which are drop down boxes), should remain flexible.

How do I do this, I have tried using the Allows Edit = No in Form property, but then that locks everything up (including the subform).

I am not very good be VB, but can cut & paste code.

Please Help

Raj
 
I think this is what you are looking for;

Code:
Me.ControlName.Enabled =False
Me.ControlName.Locked = True

For each control you want to lock.

You will also need to put the following in the forms on Current event

Code:
If [B][COLOR="DarkOrange"]This is a New record[/COLOR][/B] Then  [COLOR="SeaGreen"] 'The Bold Orange text will need to be replaced with a logical test to determine if you are on a new record.[/COLOR]
     Me.ControlName.Enabled =True
     Me.ControlName.Locked = False
Else
     Me.ControlName.Enabled =False
     Me.ControlName.Locked = True
EndIf
 
Thanks,

I used this and it works fine, only problem is that once I lock the fields I cannot search on that form using Control F and match data in those fields. For example I have locked Cust ID, FName, LName, DOB. Normally users use a drop down box to select existing customers on a form, but sometimes they want to seach a customer using their ID or DOB? How to do this on the same form.
 
Try using
Code:
Me.AllowEdits = False
To prevent edits without locking or disabling controls, this should still allow you to search those fields, and
Code:
Me.AllowEdits = True
will allow the field to be edited if needs be.

With the AllowEdits property set to False you will still be able to add new records, just not edit existing records.
 
Try using
Code:
Me.AllowEdits = False
To prevent edits without locking or disabling controls, this should still allow you to search those fields, and
Code:
Me.AllowEdits = True
will allow the field to be edited if needs be.

With the AllowEdits property set to False you will still be able to add new records, just not edit existing records.

thanks where do I put this code?
 
You'll need to figure out at what point you want to forbid edits and then trigger the following code;
Code:
Me.AllowEdits = False
 
I am using the following code in in my main client form. and have also set the tag to For New for Client ID, DOB, Fname, LName fields.

Private Sub Form_Current()
If Me.NewRecord Then
For Each CTL In Me.Controls
If CTL.Tag = "ForNew" Then
CTL.Locked = False
CTL.Enabled = True
End If
Next CTL
Else
For Each CTL In Me.Controls
If CTL.Tag = "ForNew" Then
CTL.Locked = True
CTL.Enabled = False
End If
Next CTL
End If
End Sub
 
I don't think you're going to be able to do everything you want to do, as you've described it in your original post. Manipulating AllowEdits is going to prevent all controls from being changed, including those dropdown boxes as well as the subforms.

You're going to have to use the original method proposed by John, manipulating the various controls' Enabled and Locked properties, in conjunction with a custom "Find" button, rather than using the native "Find" function (Control + F)

First you'll need code in the Form_Current event. I've done it for the first name and last name fields,. You'll have to do the same for the DOB and CustID fields, as well.
Code:
Private Sub Form_Current()
 If Not Me.NewRecord Then
  Me.FName.Locked = True
  Me.FName.Enabled = False
  Me.LName.Locked = True
  Me.LName.Enabled = False
 Else
  Me.FName.Locked = False
  Me.FName.Enabled = True
  Me.LName.Locked = False
  Me.LName.Enabled = True
End If
End Sub
Next you'll need to create a custom "Find" button. Name it "FindButton" and put this code behind it, also adding the DOB and CustID fields to it, if needed. This unlocks and enables the fields before running the Find function. When the record is pulled up, the Form_Current event will fire and the fields will once again be locked.
Code:
Private Sub FindButton_Click()
  Me.FName.Locked = False
  Me.FName.Enabled = True
  Me.LName.Locked = False
  Me.LName.Enabled = True
  DoCmd.RunCommand acCmdFind
End Sub
 
So, when using the ControlName.locked in the form_load event, it behaved nicely.
Later, I try to use the me.ControlName.locked
and the form just ignored me.
If I refresh the form, it was hit or miss.
Is it necessary to run both the
Me.ControlName.Enable
Me.ControlName.Locked

or is it form's Current Event?
 
Looking at the code above a little closer, this is what seems to make it work.
Instead of setting the Enabled property to False - where the user can not read what is in the field, the Locked makes the Enabled (as false) brighten up again.

Kind of like floorwax. You can't touch it, but it takes the dullness out!

-- To disable the control (make the control appear dimmed and unable to receive focus), set the Enabled property to No.
To make the data in the control readable, but not allow users to change the data, set the Locked property to Yes. If you set the Enabled property to No and the Locked property to Yes, the control won't appear dimmed, but it won't be able to receive focus.

One more question- It works, but I am calling the Form_current subroutine every time some status changes. Is it necessary to call the Sub Form_Current ?
 

Users who are viewing this thread

Back
Top Bottom