Lock fields when current record is not a new record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chip0105
    New Member
    • Nov 2013
    • 9

    Lock fields when current record is not a new record

    When a form is opened fields are locked. When a new record is added (by pressing an add new record button), fields are unlocked. When the record is saved the fields are then locked again.

    However, if a user click the add new record button then the user uses the navagation button at the bottom of the screen to navigate to a previous record, the locked fields are unlocked because a new record was going to be created but in fact never was. How can I prevent this from happening?

    My create new record code is as follows:
    Code:
    Private Sub btnAddNewRec_Click()
    
        DoCmd.GoToRecord , , acNewRec
    
        If Me.NewRecord Then
        Me.btnSaveNewRec.Visible = True
        Me.[txtCoordArea].Locked = False
        Me.[Full Name].Locked = False
        Me.[Badge ID].Locked = False
        Me.[SSN Last 5].Locked = False
        Me.[Term Date].Locked = False
        Me.[Requester Name].Locked = False
        Me.[Supplier].Locked = False
    
        Else
        If Not Me.NewRecord Then
        Me.[txtCoordArea].Locked = True
        Me.[Full Name].Locked = True
        Me.[Badge ID].Locked = True
        Me.[SSN Last 5].Locked = True
        Me.[Term Date].Locked = True
        Me.[Requester Name].Locked = True
        Me.[Supplier].Locked = True
    
        End If
    
        End If
    
    End Sub
    My Save New Record Code is:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    On Error GoTo ErrorMessage
    
    Dim LResponse As Integer
    
        If Me.Dirty Then LResponse = MsgBox("Do you wish to SAVE your changes?", vbYesNo)
    
            If LResponse = vbYes Then    'User chose Yes - Updated
            [txtDateRecordUpdated].Value = Now()
            [RecUpdated].Value = True
            [txtRecordUpdatedBy].Value = Forms!frmUtility!Full_Name
            DoCmd.RunCommand acCmdSave
    
        Else    'User chose No - Not Updated
    
        End If
    
        Me.[txtCoordArea].Locked = True
        Me.[Full Name].Locked = True
        Me.[Badge ID].Locked = True
        Me.[SSN Last 5].Locked = True
        Me.[Term Date].Locked = True
        Me.[Requester Name].Locked = True
        Me.[Supplier].Locked = True
    
        Me.btnSaveNewRec.Visible = False
    
    Exit_Form_BeforeUpdate:
        Exit Sub
    
    ErrorMessage:
        MsgBox Err.Description
        Resume Exit_Form_BeforeUpdate
    
    End Sub
    Any assistance would be greatly appreciated.
  • chip0105
    New Member
    • Nov 2013
    • 9

    #2
    After I took a short break and stepped away from my coding, I realized I could perform this function using the following:

    Code:
    Private Sub Form_Current()
    '
       If Not Me.NewRecord Then
          Me.btnSaveNewRec.Visible = False
    '
          Me.[txtCoordArea].Locked = True
          Me.[Full Name].Locked = True
          Me.[Badge ID].Locked = True
          Me.[SSN Last 5].Locked = True
          Me.[Term Date].Locked = True
          Me.[Requester Name].Locked = True
          Me.[Supplier].Locked = True
    '
        End If
    '
    End Sub
    Last edited by zmbd; Nov 14 '13, 08:30 PM.

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      Chip.
      Glad you found the on_current event of the form.

      Might I make a few suggestions to clean up your code:

      Refering to JUST The first block of code in the original posting:
      1) Take lines 5 thru 24 in 1st code block (btnAddNewRec_C lick) of post 1
      1a) move these to a new sub within the form's code and name it "toggle_field_s tate"
      1b) Alter your code to this:
      Code:
      Private Sub btnAddNewRec_Click()
      
          DoCmd.GoToRecord , , acNewRec
          Call toggle_field_state
      
      End Sub
      2) in the on current event of the form reuse your state toggling code.

      Code:
      Private Sub Form_Current()
      
          Call toggle_field_state
      
      End Sub
      3) There's no error trapping here. May not really be needed; however, I prefer to have error trapping in all my code just to keep things from the user.

      4) Lines 15 and 16 can be combined into the "ELSEIF" structure, or, indeed, better yet line 16 can be deleted as the current record is either new or not; thus, no real need to test it twice, in either case you will need to remove the "ENDIF" associated with line 16 located at line 25.

      If the second block of code was a second question then you need to split that question into a new thread.

      And I haven't looked at your second post closely; however, you really should get used to indenting things properly. It makes the code easier to follow... I've done this for you in the second post. (^-^).

      -z

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        The General Idea is as follows:
        Code:
        On Error Resume Next
        Dim ctl As Control
        
        For Each ctl In Me.Controls
          ctl.Locked = (Not Me.NewRecord)
        Next
        
        'Can Restablish Error Checking here...

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          Nice loop, one I use myself provided that I want every control locked, including the command buttons :)
          Although, I usually toss in a check for the control type, the control name, or for some value in the tag property so that I don't lock the command button.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32662

            #6
            I would go with the following to ensure only new records can be added :
            Code:
            Private Sub Form_Current()
                Me.AllowEdits = Me.NewRecord
            End Sub

            Comment

            Working...