Help with locking fields after update. Beginner

CTQE

Registered User.
Local time
Today, 11:35
Joined
Feb 27, 2008
Messages
48
Hello All,

May someone provide me with the most simple method to lock one field on a form after its data has been entered.

Is it a simple After Update event procedure?

Thanks in advance.
 
No you can't lock a field that has focus, but onExit or LostFocus would be better I think.

JR
 
I was going to say after update

me."yourcontrolnamehere".locked=true

But Janr disagrees. If afterupdate does not work, you can certainly try another event like Janr said.
 
You could use afterUpdate, just move focus away from the field BEFORE you lock it.

I would also make something to unlock it if the data is incorrect.

JR
 
Do you want it locked immediately after the data is entered? Normally for this kind of thing, you would allow the data be to changed by the user until the record is saved, in case they entered data and then realized that they'd made a mistake.

To lock the textbox after the record is saved

Code:
Private Sub Form_Current()
 If IsNull(Me.TextBoxName) Then
  TextBoxName.Locked = False
 Else
  TextBoxName.Locked = True
 End If
End Sub

If you absolutely have to lock it immediately, you need to use the same code in the OnExit event. Simply using TextBoxName.Locked = True would lock the textbox when the user exited it, even if nothing had been entered.
 
Thank You very much!!!
That code worked perfectly.

Have a nice weekend.
 
Your making me look bad! Really though, linq's approach is probably the way to go on this, depending on what exactly your needs are.
 
but a time will surely come when you DO want to edit those bits you have now locked!
 
No doubt! Someone is bound to make a mistake! Unless there's a separate form, accessible only to the database manager, where corrections can be made, I usually place a command button in one corner of the form, away from anything else, use its code to unlock the control(s.) I then set the button's Transparent Property to Yes. The button is invisible, but you can still click on it if you know where it's at.
 
I came across this post in a search for a similar issue. In my case the text field is being updated after an update of another field (say A) by a command which looks like this

Me.BookingRef = Me.BookingNr

The bookingNr value changes as an update of a different field (say B) in the same form. If the user wants to edit field B, the BookingRef changes which is not desired

Locking the field (BookingRef) by its Properties or by this code in Afterupdate Event of the field is not helping

How does achieve this?
 
Don't have time to experiment, right now (sorry) but you're correct...the Locked and Enabled Properties only apply to physically interacting with a Control...and don't apply when populating a Control via code, as you're doing.

But to be honest, it's hard to understand why if the Control's Value depends on another Control's Value once...why it shouldn't depend on it when the second Control is edited.

Perhaps a clearer explanation of this would help up help you.

Linq ;0)>
 
Don't have time to experiment, right now (sorry) but you're correct...the Locked and Enabled Properties only apply to physically interacting with a Control...and don't apply when populating a Control via code, as you're doing.

But to be honest, it's hard to understand why if the Control's Value depends on another Control's Value once...why it shouldn't depend on it when the second Control is edited.

Perhaps a clearer explanation of this would help up help you.

Linq ;0)>
Let me try to rephrase

I need to create a reference value which looks like CB/2020/Uniqueno. The uniqueno increases by 1 for each record. I do not want to use the PK ID field as at times the record is deleted and the reference no will have a missing number in the series.

I have a field in the table called Unique no
In each record I have a unbund text box wich reads the highest uniqueNr of the table and adds 1 to it, which i then concatenate to create the reference. This trigger for adding 1 and concatenating is an afterupdate event of a cbo in the record

When a record is being created, the ref no will be created correctly. The problem is if someone update the same cbo later after saving the form, the ref no will change!

Alternatively where do I place these 2 codes for increment and Ref no creation such that protection is not needed

Me.UniqueNr = Nz(DMax("UniqueNr", "tblContainerBooking"), 0) + 1
Me.BookingRef = Me.Test

Test is an unbound text box with row source is ="CB/" & [Y1] & "/" & [UniqueNr] ( Y1 is the year value (2020) of another unbound text box)
 
Still having trouble following you...my fault, no coffee yet...but if I read you correctly...once Me.BookingRef is populated...you want it to remain at that Value...even if other value(s) change?

Try checking to see if it's already populated...and only do the data assignment if it's empty:

Code:
If Nz(Me.BookingRef,"") = "" Then

  Me.BookingRef = Me.BookingNr

End If
Linq ;0)>
 
That would assume that the entire Record was entered when the Record was first initiated...which is not always the case. Users sometimes stop mid-Record to visit the loo...go to lunch... (gasp) take a smoke!

Linq ;0)>
 
That would assume that the entire Record was entered when the Record was first initiated...which is not always the case. Users sometimes stop mid-Record to visit the loo...go to lunch... (gasp) take a smoke!

Linq ;0)>
quite true. We cannot assume that complete record would be entered in one go!
 
Since the user should never update this control, simply set its locked property to Yes. That will not prevent your code from putting a value there.
 
The point isn't to prevent physically changing the Control's Value. The point is that the Control is populated the first time a second Control has data entered...but if that second Control is later edited, the original Control's Value doesn't change.
 
The point isn't to prevent physically changing the Control's Value. The point is that the Control is populated the first time a second Control has data entered...but if that second Control is later edited, the original Control's Value doesn't change.
correct

I update the field through an update event of another field and it is locked. The locked property only prevents physical entry to the field, not through an update event.

I solved by using an IF condition. . The update event is nested in an IF condition - it updates only if the target field is Null. Thank you for your suggestion
 
Modify the code that updates so that it updates ONLY for new records. You STILL need to lock the field to prevent users from manually modifying the ID.

Code:
If Me.NewRecord Then
    '' your code goes here
End If
 

Users who are viewing this thread

Back
Top Bottom