How Do I create a Button that unlocks a form for editing?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sam Zuhbi
    New Member
    • Sep 2010
    • 10

    How Do I create a Button that unlocks a form for editing?

    Hi,

    I am new to access and I am trying to find my way around, I have created a form but I do not want any editing to occur unless I click on an Edit button. But I still want to navigate the forms to see which record needs editing can someone help?

    Sam
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    You start by adding a button to your form, and name it btn_Edit and give it caption "Edit",and under Events look for OnClick and then click the 3 ... on the right hand side. Select Event Procedure, and you will now enter the VBA (Visual Basic for Applications window). Here you can write the code. When you start it will look something like:

    Code:
    Private sub btn_Edit_OnClick()
    
    End Sub
    Between the 2 lines you now add:
    Code:
    Private sub btn_Edit_OnClick()
      Me.AllowEdits=True
    End Sub
    the Me. is a reference to the form itself.

    Now you need one more thing and that is to disallow edits whenever the user moves to the next record, which can be done be selecting your form (click small box in upper left corner of form and go to events again. Here you click Current and again click the ... and select Event Procedure.
    It will go to VBA window looking like this:
    Code:
    Private Sub Form_Current()
       Me.AllowEdits=False
    End Sub
    Thats the basic functionality, and you can then choose to make it more advanced from here, by for instance changing the button caption when it is pressed. When you click it:
    Code:
    Private sub btn_Edit_OnClick()
      Me.AllowEdits=True
      Me.btn_Edit.Caption="Editing"
    End Sub
    Code:
    Private Sub Form_Current()
       Me.AllowEdits=False
       Me.btn_Edit.Caption="Edit"
    End Sub

    Comment

    • Sam Zuhbi
      New Member
      • Sep 2010
      • 10

      #3
      Worked Fantastic but I need a bit more help

      this has worked wonders and I cannot thank you enough for your quick help. But I have one more question? Can this also work on trying to create a new record. what I did was play with it and I screwed up my Autonumber which I am going and reset using the crude methods of Access. is is possible to also make sure no new records are created unless I press this button?

      Comment

      • TheSmileyCoder
        Recognized Expert Moderator Top Contributor
        • Dec 2009
        • 2322

        #4
        You should in the future make a new thread for a new question, but maybe some nice Moderator will split it :P

        Anyways:
        Adding a button, btn_New, caption "New", click the Events, and the OnClick button to get the code window and write:
        Code:
        Private Sub btn_New_Click()
            Me.AllowAdditions = True
            DoCmd.GoToRecord , , acNewRec
        End Sub
        This will allow user to go to a new record, but we also want to prevent him from navigating by mousewheel to a new record (maybe you don't care, depends on your application)
        So in our Form_Current() we add:
        Code:
          Me.AllowAdditions=Me.NewRecord
        This will turn off additions unless we are looking at a new record. Access can be a bit funny though, if you have a form with no records yet, and AllowAdditions= False, because then it will hide the entire form (including the buttons...) so to prevent this case from happening, we do:
        Select Form->events->OnLoad and write:
        Code:
        Private Sub Form_Load()
          Me.AllowAdditions=True
        End Sub

        Comment

        Working...