By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,510 Members | 1,445 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,510 IT Pros & Developers. It's quick & easy.

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

100+
P: 283
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.
Apr 21 '10 #1
Share this Question
Share on Google+
12 Replies


TheSmileyCoder
Expert Mod 100+
P: 2,321
Using afterUpdate is too late (which kinda seems obvious, no offense :O)

In the form's beforeUpdate, have code like this:
Expand|Select|Wrap|Line Numbers
  1. If Me.TextBoxIWantToCheck & ""="" then
  2.   Me.Undo
  3.   Cancel=true
  4. end If
  5.  
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.
Apr 21 '10 #2

100+
P: 283
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. If Me.Text10 & Text2 & Text4 & Text6 & Text8 = "" Then
  3. Me.Undo
  4. Cancel = True
  5.  
  6. End If
  7.  
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!
Apr 21 '10 #3

NeoPa
Expert Mod 15k+
P: 31,186
Check post #2 again. There's already another idea there waiting.
Apr 22 '10 #4

100+
P: 283
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

Expand|Select|Wrap|Line Numbers
  1. dim value as string
  2.  
  3. userInput = value
  4. if userInput = value then 
  5. text1 = value
  6.  
  7. else 
  8. if userInput = "" then
  9. value = me.undo
  10. end if
  11.  
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 :)
Apr 22 '10 #5

NeoPa
Expert Mod 15k+
P: 31,186
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.
Apr 22 '10 #6

100+
P: 283
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
Apr 23 '10 #7

P: 37
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.
Apr 23 '10 #8

100+
P: 283
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.

Expand|Select|Wrap|Line Numbers
  1. If Me.Text2 = "" Then
  2. DoCmd.RunCommand acCmdUndo
  3. Cancel = True
  4. End If
Apr 23 '10 #9

NeoPa
Expert Mod 15k+
P: 31,186
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?
Apr 26 '10 #10

P: 3
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!Text1 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.
Apr 26 '10 #11

P: 3
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.
Apr 26 '10 #12

P: 3
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.
Apr 26 '10 #13

Post your reply

Sign in to post your reply or Sign up for a free account.