Adding new row using button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ykhamitkar
    New Member
    • May 2007
    • 64

    Adding new row using button

    Hi there,

    I have a form where I want to add new row using button and done want a default new record,

    I have changed "Allow Additions" to No for the forms properties
    and have added following code for the button

    AllowAdditions = True
    DoCmd.GoToRecor d , , acNewRec

    Now when I click on the button and type something in newly added record (though button) it automatically creates a blank record which I dont want

    I want user can only add a new record on click of a button

    Any thoughts

    Thanks
  • DonRayner
    Recognized Expert Contributor
    • Sep 2008
    • 489

    #2
    You could add

    Code:
    AllowAdditions = False
    to the after update event for each of your fields.

    Comment

    • OldBirdman
      Contributor
      • Mar 2007
      • 675

      #3
      I'd like an answer also. Adding an event to each and every control seems combursome. Perhaps using the OnDirty Event would work, but I've had difficulties when I also try to create new records in "Related" tables while creating the new record in the primary table.

      OldBirdman

      Comment

      • DonRayner
        Recognized Expert Contributor
        • Sep 2008
        • 489

        #4
        Best way to do it is not use the built in navigation control, use your own controls instead. That way you have complete control over what buttons are enabled for use at each moment.

        You could then use something like the following in the on current event for the form

        Code:
            If Me.NewRecord = True Then
                Me.first.Enabled = False
                Me.previous.Enabled = False
                Me.next.Enabled = False
                Me.last.Enabled = False
                Me.search.Enabled = False
                Me.add.Enabled = False
             Else
         
                Me.last.Enabled = True
                Me.search.Enabled = True
                Me.add.Enabled = True
                Me.first.Enabled = True
         
                If Me.Recordset.RecordCount < 2 Then
                    Me.Recordset.MoveLast
                    Me.Recordset.MoveFirst
                End If
         
                If Me.CurrentRecord <> 1 Then
                    Me.previous.Enabled = True
                Else
                    Me.previous.Enabled = False
                End If
         
                If Me.Recordset.RecordCount = Me.CurrentRecord Then
                    Me.next.Enabled = False
                Else
                    Me.next.Enabled = True
                End If
             End If

        Comment

        • beacon
          Contributor
          • Aug 2007
          • 579

          #5
          I ran into this same problem when I was creating Data Access Pages (before they basically became extinct).

          The only way around it I found was to do as Don suggested and create my own navigation buttons with specific code written for the given circumstances. When it comes to the navigation buttons that are built in, there really doesn't seem to be much leeway for developers to alter their functionality.

          Comment

          • OldBirdman
            Contributor
            • Mar 2007
            • 675

            #6
            Where is this going? Whether there are navigations on the form or not, whether they are used or not, whether they are enabled/visible or not doesn't change the original question "What should the code be for adding a new record?"
            I dislike the nav buttons that Access supplies, and NEVER use them. I have my own "Add New" command button, with DoCmd.GoToRecor d , , acNewRec after removing any filters for the form )I assumed that ykhamitkar did also). This presents me with a new record filled in (default) to the current record, which is not what I want. Changing the "Move First" button to Enabled = False doesn't help. As a matter-of-fact, moving to the first record strongly implies that I am finished with the Add-New.

            OldBirdman

            Comment

            • DonRayner
              Recognized Expert Contributor
              • Sep 2008
              • 489

              #7
              OldBirdman;

              The problem that the origional poster is having is. When in datasheet view and running his code, as soon as he starts typing into the new record access is creating another blank record for data entry directly below the one he is typing in.

              Thus the origional poster is looking for a way to not have that blank record available to his users to enter an additional record without clicking on his button.

              While AllowAdditions is true, access will always create this blank record when the current blank record has anything added to it. He will have to change AllowAdditions to false before his users get a chance to enter any additional records.

              Thus using the AllowAdditions = False in the afterupdate event for the fields will accomplish what he wants. Your suggestion on using the OnDirty event will probally work as well.

              As for your problem with the addnew command, I'm not quite sure what the problem that you are having is. In a Form/Subform combination the fields that are linked in a Child/Parent relationship between the two will always be filled in on the subform. It couldn't work any other way and still keep the relationships intact. Could you post back with some more information or examples?

              Comment

              • OldBirdman
                Contributor
                • Mar 2007
                • 675

                #8
                Thanks for the explanation. I had missed the DataSheet view in the initial problem. My problem is somewhat different, and I'll post in a new thread when I can isolate it from a project too complex to discuss here.

                OldBirdman

                Comment

                Working...