How to Clear Form Controls for Next Entry

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bloukopkoggelmander
    New Member
    • Oct 2006
    • 184

    How to Clear Form Controls for Next Entry

    Hi All

    A simple query I suppose for most fo you. Here goes:

    I have a form for data antry. Once the user has completed and saved the first entry and want to create a new one, I would want them to click on a button that says " New Entry" or something, which then clears all populated form controls ready for a new antry.

    I have found some fragmented information on the web, but nothing to help me out. Something like : Sub Clear_Form().

    Not sure how to do this.

    Any help will be much appreciated as usual.

    Thanks
  • MSeda
    Recognized Expert New Member
    • Sep 2006
    • 159

    #2
    Code:
      DoCmd.GoToRecord , , acNewRec
    should take you to a new record. If there is something more complicated about your form that won't allow this to work post the details about your form.

    Comment

    • vkong85
      New Member
      • Jun 2007
      • 24

      #3
      something that's really simple is to just place a control button into the form. Simply choose the control button option from the toolbox when in design mode for the form and access will walk you through the rest and write the VBA code for you!

      Comment

      • bloukopkoggelmander
        New Member
        • Oct 2006
        • 184

        #4
        Hi there MSeda and vkong85

        Thank you both for your replies. MSeda, I think the code and everything for the form will be too much to post here. The form itself have 34 fields and there is quite a lot of code behind it. Surely there must be some code that empties all controls on a form regardless of your code?

        vkong85, I have tried Access's Add New Record button, but it does not work. Seems like it is missing a trigger to empty all form controls.

        Comment

        • missinglinq
          Recognized Expert Specialist
          • Nov 2006
          • 3533

          #5
          MSeda only said to post your code if the code

          DoCmd.GoToRecor d , , acNewRec

          didn't work, but it really should! Have you tried it yet? If you place it behind a button it will go to a new record, saving the record just entered at the same time.

          Comment

          • bloukopkoggelmander
            New Member
            • Oct 2006
            • 184

            #6
            Hi there missinglinq

            Sorry, I misunderstood him. Right here is the code for the New Rocrd Button :

            Private Sub Command13_Click ()
            On Error GoTo Err_Command13_C lick


            DoCmd.GoToRecor d , , acNewRec

            Exit_Command13_ Click:
            Exit Sub

            Err_Command13_C lick:
            MsgBox Err.Description
            Resume Exit_Command13_ Click

            End Sub




            I have placed this button on a form which I let Access create automatically and it does waht it is supposed to, which is clear all populated fields on the form.

            When doing the same on the form I have created manually, this code refuses to run.

            Hope this helps.

            Comment

            • missinglinq
              Recognized Expert Specialist
              • Nov 2006
              • 3533

              #7
              Can we see the code from the form you created manually, where it refuses to run? Also, make sure on this form that the AllowAdditions property is set to YES.

              Comment

              • bloukopkoggelmander
                New Member
                • Oct 2006
                • 184

                #8
                Hi there missinglinq

                Right now I am propably going to sound extremely dumb, but I am not sure to look where it falls over, as all the code I have done for this page so far works fine. The only addition to the code I have made to try and empty all the controls on it is the code I pasted in my last post. When I do run this, nothing happens, no error or nothing.You might have to beat me with a stick in the right direction man . :-)

                I have also checked the AllowAdditions property and it is set to YES.

                Comment

                • Sequin
                  New Member
                  • Jun 2007
                  • 5

                  #9
                  Going to the new record will only clear bound fields - if some of the form's controls are unbound and handled in code, they won't be cleared

                  Comment

                  • bloukopkoggelmander
                    New Member
                    • Oct 2006
                    • 184

                    #10
                    Damn!!

                    All mine are unbound and I also have various check boxes and radio buttons. So there is no way clearing it using VBA?

                    Comment

                    • Sequin
                      New Member
                      • Jun 2007
                      • 5

                      #11
                      You could do something like this:


                      Dim ctl as Control

                      For Each ctl In me.Controls
                      If ctl.ControlType = acComboBox or ctl.ControlType = acTextBox 'Add other control types here
                      ctl.Value = Null
                      End If
                      Next ctl

                      Comment

                      • bloukopkoggelmander
                        New Member
                        • Oct 2006
                        • 184

                        #12
                        Sequin, you are a Hedgehog God!

                        Well that clears just about all my form controls. So brill thanks for that man. The only problem now is that when I use this code to empty the controls and then try to add a new record, it errors with a massive error.

                        Here is part of it:

                        Microsoft Access can't append all the records in the append query.
                        Microsoft Access set 0 field(s) to Null due to a type conversion errorand didn't add 1 record(s) to the table due to key violations,0 records due to lock violations, and 0 records due to validation rule violations..... ....

                        Huh?

                        That confuses me, make no mistake....

                        Comment

                        • bloukopkoggelmander
                          New Member
                          • Oct 2006
                          • 184

                          #13
                          I was thinking, could this be because I created my forms manually rather than using Access wizards?

                          Propable a dumb question, but hey not hurt asking.

                          Comment

                          • FishVal
                            Recognized Expert Specialist
                            • Jun 2007
                            • 2656

                            #14
                            Originally posted by bloukopkoggelma nder
                            I was thinking, could this be because I created my forms manually rather than using Access wizards?

                            Propable a dumb question, but hey not hurt asking.
                            You really want to work hard.
                            As far as I've got it you have a completely unbound form.
                            All your present code is devoted to updating underlying table(s) ?
                            If so I can tell you that in a properly bound form you need to call Me.Refresh method or normally don't need any code at all.

                            If you want to proceed this way, the code below is quite close to what Form object do with controls when new record inserted.

                            [CODE=vb]

                            Private Sub btnClear_Click( )

                            Dim ctl As Control

                            On Error Resume Next

                            For Each ctl In Me.Controls
                            ctl.Value = ctl.DefaultValu e
                            Next

                            Set ctl = Nothing

                            End Sub

                            [/CODE]

                            Next time will learn how to emulate Navigation buttons. :-)

                            Good new - you will get out this much more experienced than those working through wizards. I'm serious. This is the best way to learn.

                            Comment

                            • bloukopkoggelmander
                              New Member
                              • Oct 2006
                              • 184

                              #15
                              Howzit Fishval

                              Thanks a lot for your response. I will have a go at that code and see if it does the jobs.

                              Well, like they say , if your dumb then you gotta suffer.

                              I can only use Access wizard for very simple things like forms for maintaining lookup tables.

                              The reason I am doing everything manually is because I am working on a very complex company project and the Access wizards cannot emulate the requirements of this project. So this means creating the forms and controls manually and writing all the underlying code. ( This is my first project and first time using Access and VBA and AS400.

                              But you are also right in that I am learning a hell of a lot. My progress has been huge in the last few months. It is very slow developing though and due to the complexity and my lack of knowledge in the tools I use it is sometimes hard to get certain funtions to work like this seamably simple problem I am trying to solve now.

                              But hey, just keep on swimming!!! :-)

                              Comment

                              Working...