How to make a form not save to a table if left blank?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slenish
    Contributor
    • Feb 2010
    • 283

    #1

    How to make a form not save to a table if left blank?

    Well i have been playing with this off and on all day and its driving me up a wall.

    I have a small form that has 5 text boxes. Now if I were to type nothing in to the pop up box before closeing it nothing is saved to the table on the backend but if i type something and then change my mind and erase it and then just close the form it will save a blank record.

    How can i have the form check to see if all of the boxes are blank then erase the info that could have been typed in and then close with out saving?

    I was trying to use the OnDirty and AfterUpDate function but was getting no where.
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    Using afterUpdate is too late (which kinda seems obvious, no offense :O)

    In the form's beforeUpdate, have code like this:
    Code:
    If Me.TextBoxIWantToCheck & ""="" then
      Me.Undo
      Cancel=true
    end If
    You can then expand this to include several criteria if you have more then 1 textbox, and you can add a confirmation dialog if needed.

    Edit: remembered this
    Another way of doing it without code, is to go to the table design and specify the fields as required. That way you cannot save a record with those fields left blank.

    Comment

    • slenish
      Contributor
      • Feb 2010
      • 283

      #3
      Hi TSO,

      I appreciate the help. Well i tried what you said but its still not working. Here is what I have so far.

      I have placed this in the form BeforeUpdate function
      Code:
      If Me.Text10 & Text2 & Text4 & Text6 & Text8 = "" Then
      Me.Undo
      Cancel = True
      
      End If
      What's happening is it wont let me close the form if its left blank but i want it to close still just not save anything. Any ideas?
      Thanks again!

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        Check post #2 again. There's already another idea there waiting.

        Comment

        • slenish
          Contributor
          • Feb 2010
          • 283

          #5
          Hi NeoPa,

          I saw the extra part at the bottom where TSO updated his post, but still doesnt help me. I'm trying to make it where none of the fields are required, it just wont save to the table if left blank.

          The way its working right now is that it wont save which is good but it also wont close which is not good. I need it to do both, close and not save if the form is left blank.

          You got any ideas? I keep trying all kinds of variations. I was trying to set user input to a value so that way if someone typed something in and then erased it it would clear the form before closeing a saving but was having no luck with that either.

          This is not exact code just kind of sudo coding my idea

          Code:
          dim value as string
          
          userInput = value
          if userInput = value then 
          text1 = value
          
          else 
          if userInput = "" then
          value = me.undo
          end if
          is there any way to do Me.Erase?? Me.Undo does not seem to work like it should.

          Appreciate the help. good to see you again :)

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            Originally posted by slenish
            I saw the extra part at the bottom where TSO updated his post, but still doesnt help me. I'm trying to make it where none of the fields are required, it just wont save to the table if left blank.
            Can you explain what you see as the difference between these two situations? I would have thought setting a field or control to required would have just that effect.

            Comment

            • slenish
              Contributor
              • Feb 2010
              • 283

              #7
              Hi NeoPa,

              Well the difference is that you are talking about setting a field or control to REQUIRED but that is not what i want.

              Maybe you can make a test form and try this to see what the problem is. What I have is a form with 5 text boxes. Now if I leave the boxes blank and dont type anything in the boxes and hit the save record/close form button the form closes and nothing is saved which is great!!

              But If i type something in one box and then delete what i typed and then close/save the form(remember the form is now blank because i erased what i typed) the form will now save a blank record which is what I dont want. So its not that i want the form to tell the user you have to type something in here I just want the form to tell the form if the text boxes are left blank go ahead and close just dont save anything.

              Its weird that it will close and not save as long as I dont type anything but as soon as I type something, then erase it, then close the form it saves a blank record.

              The whole reason behind this is because the form is a pop up form and if the user opens the form on accident and then realizes they dont need it i dont want it to save any blank records that i have to go erase on the table later.

              I hope that is a little more detailed explanation.

              apprecitate the help :D

              Comment

              • robjens
                New Member
                • Apr 2010
                • 37

                #8
                You should use the .Value and .Text properties of a TextBox. Value is what's already in the table (as in the field bound to your control) and .Text is what you just typed but not yet stored.

                Comment

                • slenish
                  Contributor
                  • Feb 2010
                  • 283

                  #9
                  I finally got this to work. Really appreciate all the help everyone.

                  I had to change TSO orginal answer just a little bit and I had to put it in the Forms OnClose function.

                  Only thing im trying to get rid of now is a little pop up box that asks if you want to delete the record. Which is fine just wish it would delete it and close with out asking.

                  Thanks again everyone.

                  Code:
                  If Me.Text2 = "" Then
                  DoCmd.RunCommand acCmdUndo
                  Cancel = True
                  End If

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32669

                    #10
                    Originally posted by slenish
                    Only thing im trying to get rid of now is a little pop up box that asks if you want to delete the record. Which is fine just wish it would delete it and close without asking.
                    I'm a little confused here. Is this related to the current question (where does deleting come in when updating a record)? Is it possibly a similar, but deletion related, question?

                    Comment

                    • hngtwn
                      New Member
                      • Apr 2010
                      • 3

                      #11
                      I've got exactly the same issue - frustrating. I think I have it beat though. I put a "Done" button on the form that runs this macro:

                      Criteria / Action / Arguments
                      / SetWarnings / No
                      Forms!MyForm!Te xt1 Is Null / RunCommand / Undo
                      / Close / Form, MyForm, No
                      / SetWarnings / Yes


                      BTW - the SetWarnings action isn't available in the macro builder unless you click "Show all actions" up top. It'll take care of your pop-up.

                      Comment

                      • hngtwn
                        New Member
                        • Apr 2010
                        • 3

                        #12
                        Never mind - sorry. My solution works if you type something into the text box then erase it, but it causes an error if nothing has been entered.

                        Comment

                        • hngtwn
                          New Member
                          • Apr 2010
                          • 3

                          #13
                          One more step - I put an afterupdate event on the text box in question that sets a temporary variable to 1, then added that to the criteria for running the undo command. Finally, I put a RemoveTempVar in the macro that closes the form. Works like a charm.

                          Comment

                          Working...