Lock fields based on status change

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dposton
    New Member
    • Jun 2012
    • 1

    Lock fields based on status change

    i have a form that captures data but once a specific status is selected it can no longer be changed (status 3 or 6),
    then if status = 5 it locks specific fields on the form but leaves a few fields still open.
    else all fields are available.
    I have some VB knowledge but not much.
    I'm hoping i don't have to setfocus on each field then change the property since there are many. Then is the status changes back from 5 to 4 have to undo the same way...
    Here's what i have so far:
    Private Sub Status_Change()

    If Status.Value = 3 Or Status.Value = 6 Then

    MsgBox "You cannot change the status of this NCR Case, please see a CAPA administrator."

    Else

    If Status.Value = 5 Then
  • Mihail
    Contributor
    • Apr 2011
    • 759

    #2
    First of all, Welcome to the forum !

    Second:
    In a form you have not fields.
    Only the tables and the queries have fields.
    A form have only controls that can be (or not) bound to a field.

    Is not necessary to set focus to each control but is necessary to set, for each control, it's locked property as you need based on the status.

    Use the tag property for each control to store the status when control is locked (or when is not locked).

    In the bellow code I use the tag property to store the statuses when the control is locked (see also, the attachment):
    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub cmdStatus1_Click()
        Call SetLock(1)
    End Sub
    
    Private Sub cmdStatus2_Click()
        Call SetLock(2)
    End Sub
    
    Private Sub cmdStatus3_Click()
        Call SetLock(3)
    End Sub
    
    Private Sub cmdStatus4_Click()
        Call SetLock(4)
    End Sub
    
    Private Sub SetLock(Status As Long)
    Dim ctl As Control, i As Long, ArrayStatus
    On Error Resume Next
        For Each ctl In Me.Controls
            If ctl.Tag <> "" Then 'This control is afected by status
                ctl.Locked = False 'Now, the control is, no more, locked
                ctl.Value = "You can change me. Try !"
                ArrayStatus = Split(ctl.Tag, ",")
                For i = 0 To UBound(ArrayStatus)
                    If ArrayStatus(i) = Status Then 'The Status "say" that this control must be locked
                        ctl.Locked = True
                        ctl = "Can't change me. Try !"
                Exit For
                    End If
                Next i
            End If
        Next ctl
    End Sub
    Attached Files

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      A Variation along the same lines, with the Tag Property of 'Involved' Controls = 'Lock':
      Code:
      Private Sub Status_AfterUpdate()
      On Error Resume Next
      Dim ctl As Control
      
      With Me
        If IsNull(![Status]) Then
          Exit Sub
        Else
          For Each ctl In .Controls
            If ctl.Tag = "Lock" Then
              ctl.Locked = (![Status] = 3 Or ![Status] = 6)
            End If
          Next
        End If
      End With
      End Sub

      Comment

      Working...