How to lock a field in a record or make it read only

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eimbert
    New Member
    • Jul 2007
    • 8

    How to lock a field in a record or make it read only

    Hi, Im fairly new in the Access world. I would like to know how I can prevent users from changing data entered into a specific field of a record. Once I have completed the data Entry they should be able to see the information but not be able to change it.
  • puppydogbuddy
    Recognized Expert Top Contributor
    • May 2007
    • 1923

    #2
    Originally posted by eimbert
    Hi, Im fairly new in the Access world. I would like to know how I can prevent users from changing data entered into a specific field of a record. Once I have completed the data Entry they should be able to see the information but not be able to change it.
    Here is one way, assuming that you are using a bound form for data entry:
    Code:
    Private Sub Form_Current()
    If Not Me.NewRecord Then
       'Lock YourControl for each record.
        Me!YourControl.Locked = True
    Else
        Me!YourControl.Locked = False
    End If
    End Sub

    Comment

    • MikeTheBike
      Recognized Expert Contributor
      • Jun 2007
      • 640

      #3
      Hi
      Originally posted by puppydogbuddy
      Here is one way, assuming that you are using a bound form for data entry:
      Code:
      Private Sub Form_Current()
      If Not Me.NewRecord Then
         'Lock YourControl for each record.
          Me!YourControl.Locked = True
      Else
          Me!YourControl.Locked = False
      End If
      End Sub
      As an alternative is
      Code:
      Private Sub Form_Current()
      If Me.NewRecord Then
          Me.AllowEdits = True
      Else
          Me.AllowEdits = False
      End If
      End Sub
      You could also include
      Me.AllowDeletio ns= ....
      if you want to prevent record deletion also.


      MTB

      Comment

      • damonreid
        Recognized Expert New Member
        • Jul 2007
        • 114

        #4
        You could always just go into the Properties and change
        Enabled = False
        Locked = True

        and ignore the VB side of it.

        Comment

        • eimbert
          New Member
          • Jul 2007
          • 8

          #5
          Thanks for the advise. I have used this code to lock all fields in current form. Once the first record is created the code doesn't allow any editing or deleting. The problem I have now is how to make changes to that record at a later time.
          Any Additional advise will be highly appreciated

          1. Private Sub Form_Current()
          2. If Me.NewRecord Then
          3. Me.AllowAdditio ns = True
          4. Me.AllowDeletio ns = True
          5. Me.AllowEdits = True
          6. Else
          7. Me.AllowAdditio ns = True
          8. Me.AllowDeletio ns = False
          9. Me.AllowEdits = False
          10. End If

          Comment

          • MikeTheBike
            Recognized Expert Contributor
            • Jun 2007
            • 640

            #6
            Originally posted by eimbert
            Thanks for the advise. I have used this code to lock all fields in current form. Once the first record is created the code doesn't allow any editing or deleting. The problem I have now is how to make changes to that record at a later time.
            Any Additional advise will be highly appreciated

            1. Private Sub Form_Current()
            2. If Me.NewRecord Then
            3. Me.AllowAdditio ns = True
            4. Me.AllowDeletio ns = True
            5. Me.AllowEdits = True
            6. Else
            7. Me.AllowAdditio ns = True
            8. Me.AllowDeletio ns = False
            9. Me.AllowEdits = False
            10. End If
            Two things

            Define 'that record' and
            Define under what circumstances you want the record to be edited.


            M TB

            Comment

            • eimbert
              New Member
              • Jul 2007
              • 8

              #7
              Originally posted by MikeTheBike
              Two things

              Define 'that record' and
              Define under what circumstances you want the record to be edited.


              M TB
              Im very new at access and I really don't know much of VBA programming. So Im just looking for an easy way to control the data entry process. Can I create a Command Button and lets say we call it (Edit) and from there you could work in edit mode for that form? A (delete) Button etc. I was thinking about running These command buttons from a Main Form Menu. I just dont know the language to make this work correctly. If you have any better suggestions I would highly appreciate it.

              Comment

              • MikeTheBike
                Recognized Expert Contributor
                • Jun 2007
                • 640

                #8
                Originally posted by eimbert
                Im very new at access and I really don't know much of VBA programming. So Im just looking for an easy way to control the data entry process. Can I create a Command Button and lets say we call it (Edit) and from there you could work in edit mode for that form? A (delete) Button etc. I was thinking about running These command buttons from a Main Form Menu. I just dont know the language to make this work correctly. If you have any better suggestions I would highly appreciate it.
                cmdEdit button

                Code:
                Sub cmdEdit_Click
                On Error GoTo Err_cmdEdit_Click
                
                   Me.AllowEdits = True
                
                Exit_cmdEdit_Click:
                    Exit Sub
                
                Err_cmdEdit_Click:
                    MsgBox Err.Description
                    Resume Exit_cmdEdit_Click
                
                End sub
                The OnCurrent event will reset allow edits when you move to the next record.

                cmdDeleteButton

                Code:
                Private Sub cmdDelete_Click()
                On Error GoTo Err_cmdDelete_Click
                    
                    If MsgBox("Do you want to delete record?", vbYesNo + vbQuestion) = vbNo Then Exit Sub
                
                    RunCommand acCmdDeleteRecord
                
                Exit_cmdDelete_Click:
                    Exit Sub
                
                Err_cmdDelete_Click:
                    MsgBox Err.Description
                    Resume Exit_cmdDelete_Click
                    
                End Sub
                Have you tried the button wizzard when placing buttons on a form.

                There are options for deleting records etc. (although the code generated will be different form above).


                MTB

                Comment

                • eimbert
                  New Member
                  • Jul 2007
                  • 8

                  #9
                  Thanks for your patience and help . I used the code for editing you gave me. I created a Command button on the actual form the data entry is being done on and named it (Edit). I have used the code below to lock the form , and to be able to edit it. When the form is opened all the fields are locked(good) but when I click on the cmdEdit button the fields remain locked. I have saved and closed the form various times but records are still locked. I haven't done the (delete)button until I know that the code does work. Should I put the command (edit) button somewhere other than the data entry form for this code to work?

                  Thank You

                  Private Sub Form_Current()
                  If Me.NewRecord Then
                  Me.AllowEdits = True
                  Me.AllowDeletio ns = True
                  Else
                  Me.AllowEdits = False
                  Me.AllowDeletio ns = False
                  End If
                  End Sub

                  Private Sub cmdEdit_Click()
                  On Error GoTo Err_cmdEdit_Cli ck

                  Me.AllowEdits = True

                  Exit_cmdEdit_Cl ick:
                  Exit Sub

                  Err_cmdEdit_Cli ck:
                  MsgBox Err.Description
                  Resume Exit_cmdEdit_Cl ick
                  End Sub

                  Comment

                  • eimbert
                    New Member
                    • Jul 2007
                    • 8

                    #10
                    I have also used the Code for deleting a record and I get an error " The command or action Deleterecord isn't available now.

                    Comment

                    • MikeTheBike
                      Recognized Expert Contributor
                      • Jun 2007
                      • 640

                      #11
                      Originally posted by eimbert
                      I have also used the Code for deleting a record and I get an error " The command or action Deleterecord isn't available now.
                      To answer your last probelm fist I thonk you need this

                      Me.AllowDeletio ns = True
                      RunCommand acCmdDeleteReco rd
                      Me.AllowDeletio ns = False

                      in the delete code.

                      for now, I do not know why the edit doesn'twork, it does for me.

                      I will think about it over the week end, but I am not sure about
                      Should I put the command (edit) button somewhere other than the data entry form for this code to work?
                      What data entry form and what other form?

                      MTB

                      Comment

                      • KimC
                        New Member
                        • Mar 2007
                        • 4

                        #12
                        This is exactly the issue that came up for me yesterday - my problem was the simpler version that MikeTheBike and puppydogbuddy offered up solutions for. And it worked! Thank you both!

                        Comment

                        Working...