Results 1 to 4 of 4
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Posts
    1,119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Enabled/Locked Properties (97)

    How can I use the Enabled and Locked properties to protect a field. Once there is data in the field, I would like it to be locked. When someone enters a new record, I would like the field to be unlocked. I set the fields property to enable yes, and locked yes. The data in the field cannot be typed over but when someone adds a new record that field cannot be typed into either. I would like the field to be unlocked when a new record is entered.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Enabled/Locked Properties (97)

    You can only do this in a form, not in a table or query. Write code in the OnCurrent event of the form. Let's say that your form contains a text box txtField bound to the field you want to locked/unlocked. The code could look like this:

    Private Sub Form_Current()
    ' Set Locked property based on whether user is entering a new record
    [txtField].Locked = Not Me.NewRecord
    End Sub

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Posts
    1,119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enabled/Locked Properties (97)

    That worked. If you want to do this to multiple fields, would you just list them or do you have to put "and" or "or"?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Enabled/Locked Properties (97)

    You need a separate instruction for each field. If you wish, you can store the value of Me.NewRecord in a boolean variable:

    Private Sub Form_Current
    Dim fLocked As Boolean
    fLocked = Not Me.NewRecord
    [txtField1].Locked = fLocked
    ...
    [txtField37].Locked = fLocked
    End Sub

    If you want to lock/unlock *all* text boxes on the form, you could use code like this:

    Private Sub Form_Current()
    Dim ctl As Control
    ' Loop through all controls
    For Each ctl In Me.Controls
    ' Check if we have a text box
    If ctl.ControlType = acTextBox Then
    ' If so, set Locked property
    ctl.Locked = Me.NewRecord
    End If
    Next ctl
    ' Release object memory
    Set ctl = Nothing
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •