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

When i created my clear button on my form, it is clearing the db, not just the form

P: 48
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdClear_Click()
  2.  
  3. Me.txtSearchSku = Null
  4. Me.txtNMFCCode = Null
  5. Me.txtSearchDesc = Null
  6.  
  7. Me.Repaint
  8.  
  9. End Sub
  10.  
i have this right now, because if i pick anymore of the fields that are in the database, then it takes those records out of it completely. i need for it just clear the form
Jun 12 '15 #1
Share this Question
Share on Google+
12 Replies


NeoPa
Expert Mod 15k+
P: 31,769
From your posted code I would guess that you're looking to show an empty record on the form. I also guess that the form you're dealing with is a bound form. IE. It has a Record Source and the controls on the form have Control Sources.

What you need to understand is that bound forms are like a window onto the data behind the form, of the recordset that the form is bound to. If it's pointing to a valid record when you set the control values to Null then that will be passed on to the fields of the record itself.

What you are probably looking for is the idea of moving the form (window) on to a separate record. When you move records in a bound form you see the different data in each of those records. To clear the form what you really need to do is to move to the New record. This is a special record found in all recordsets that allow additions.

Does this sound like it's what you're after?
Jun 13 '15 #2

Expert 100+
P: 636
Hi

Are you trying to cancel all alterations to the form without saving then?

If so, then perhaps Me.Undo (?) or similar, as moving to a new record will save any alterations.

Just a thought.


MTB
Jun 15 '15 #3

P: 48
I am trying to use the undo but it is not working.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdClear_Click()
  2. Me!sku.Undo
  3. Me!description.Undo
  4. Me!Hazardous.Undo
  5.  
  6.  
  7. End Sub
i was reading something about adding the beforeupdate, but i am not really sure how that would work. i am new to vba. please help!!
Jun 15 '15 #4

P: 48
when i click on my clear button after adding the above code, it is not doing anything.
Jun 15 '15 #5

Expert 100+
P: 636
I some time use this
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCancel_Click()
  2. On Error GoTo Err_cmdCancel_Click
  3.  
  4.     If Me.Dirty Then Me.Undo
  5.  
  6. Exit_cmdCancel_Click:
  7.     Exit Sub
  8.  
  9. Err_cmdCancel_Click:
  10.     MsgBox Err.Description
  11.     Resume Exit_cmdCancel_Click
  12.  
  13. End Sub
If it is not a bound form Me.Dirty will cause an error.

MTB
Jun 15 '15 #6

P: 48
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdClear_Click()
  2. On Error GoTo err_cmdClear_click
  3. If Me.Dirty Then Me.Undo
  4.  
  5. Exit_cmdClear_click:
  6. err_cmdClear_click:
  7. msgBox: Err.description
  8. Resume Exit_cmdClear_click
  9.  
  10. end sub
this error says invalid use of property and highlights this and goes to the word description. i am so confused right now
Jun 15 '15 #7

P: 48
i figured it out!! thanks so much!!!
Jun 15 '15 #8

Expert 100+
P: 636
There seem to be two things need correcting ie.

You need an Exit Sub statement after the Exit_cmdClear_click: label otherwise you will never exit the sub!!
You should not have a colon after the msgBox statement

Like this

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdClear_Click()
  2. On Error GoTo err_cmdClear_click
  3.     If Me.Dirty Then Me.Undo
  4.  
  5. Exit_cmdClear_click:
  6.     Exit Sub
  7.  
  8. err_cmdClear_click:
  9.     msgBox Err.description
  10.     Resume Exit_cmdClear_click
  11.  
  12. End Sub
??

MTB
Jun 15 '15 #9

NeoPa
Expert Mod 15k+
P: 31,769
Hi Lilly.

Just a quick note to draw your attention to two warning PMs that you've received. I'm guessing you haven't even noticed that they're there which is the reason you haven't done anything about them yet. If so there is a link at the top of every page that says Inbox(#), where # is the number of PMs sent to you that haven't yet been read.

The PMs are telling you that you must use the [CODE] tags whenever you post code on here so that people can read it in a form that makes sense. Once you have sorted out using the PM system feel free to send one to me asking for assistance using the site if you struggle any time. Some areas are easier than others, and all can be complicated if you're not used to using similar systems anywhere.

@Mike.
Well done for catching this and realising what the question was about.
Jun 15 '15 #10

P: 48
Yes I just figured it out about the coding part of it. So sorry for the confusion
Jun 15 '15 #11

NeoPa
Expert Mod 15k+
P: 31,769
Don't worry about making honest mistakes. You aren't the first and certainly won't be the last.

No-one here will worry about rules broken in ignorance. Do keep an eye out in case you get any further PMs though. They won't necessarily be about rules of course.
Jun 15 '15 #12

P: 48
Okay!! Thanks for the advice!!
Jun 15 '15 #13

Post your reply

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