Lock a field on specific records in a subform

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WyvsEyeView
    New Member
    • Jul 2008
    • 46

    Lock a field on specific records in a subform

    I have a dataset subform on which I want to lock a field for just two specific records. Each record has a type_id and a desc field. I want to test the value of the type_id field for the current record and if it is "PROD" or "LIBR" I want to lock the field. (I can allow editing of the desc field.) type_id field for all other records should remain unlocked. Here is my code:
    Code:
    Private Sub type_id_Enter()
      If Me.type_id.Value = "PROD" Then
        Me.type_id.Locked = True
        MsgBox "You cannot edit this type.", vbOKOnly
      Else
        Me.type_id.Locked = False
      End If
      If Me.type_id.Value = "LIBR" Then
        Me.type_id.Locked = True
        MsgBox "You cannot edit this type.", vbOKOnly
      Else
        Me.type_id.Locked = False
      End If
    End Sub
    The message box is displaying correctly but I can still edit the PROD or LIBR records after dismissing the box. I have also tried this code with the OnCurrent and BeforeUpdate events, with even less success. Thanks for any advice.
    Last edited by NeoPa; Aug 5 '08, 12:02 AM. Reason: Please use the [CODE] tags provided
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    I only had the problem when the id_Type was PROD, which is even curioser. I modified the code slightly, placing parts in Form_Current and part in the id_Type.Enter event, and it works fine. I would add a line, at the very beggining, in the Form_Current event, setting the initial focus to another control. It will be very irritating to users, and possibly confusing, to have the message pop up when they move to the record and before they'd done anything. This way it will only chastise them if they deliberately move into the field that has PROD or LIBR in it. It will also help to educate them.

    Code:
    Private Sub Form_Current()
    If Me.type_id.Value = "PROD" Or Me.type_id.Value = "LIBR" Then
     Me.type_id.Locked = True
    Else
     Me.type_id.Locked = False
    End If
    End Sub
    Code:
    Private Sub type_id_Enter()
    If Me.type_id.Value = "PROD" Or Me.type_id.Value = "LIBR" Then
     MsgBox "You cannot edit this type.", vbOKOnly
    End If
    End Sub
    Welcome to Bytes!

    Linq ;0)>

    Comment

    Working...