Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clearing All Controls in a Form

Status
Not open for further replies.

rana

Technical User
Jul 5, 2000
20
0
0
US
I am trying to make a button that when clicked first clears all the controls in the form and then closes the form.  I need it to clear all the controls because I have set the properties for all the fields to be required so if the person tries to exit with only half the controls filled out then error messages keep popping up.  I'm trying to avoid this.  Is this possible?
 
Try this:<br><br>Private Sub YourButton_Click()<br>Me.Undo<br>DoCmd.Close acForm, Me.Name<br>End Sub<br><br>Change &quot;YourButton&quot; to the actual name of the cmdbutton.<br><br>HTH<br>RDH <p>Ricky Hicks<br><a href=mailto: rdhicks@mindspring.com> rdhicks@mindspring.com</a><br><a href= > </a><br>
 
Try this, I picked it up in another forum, it only clears textboxes, so if you need to clear comboboxes or listboxes than you need to adjust it accordingly.&nbsp;&nbsp;The above code will error out if there are no changes to undo, and may also undo a record that you want to save.<br><br><br>&nbsp;Private Sub ClearText()<br>&nbsp;Dim ctl as Control<br>&nbsp;For Each ctl in Me.Controls<br>&nbsp;If Typeof ctl is TextBox then<br>&nbsp;ctl.Value = vbNullString<br>&nbsp;End If<br>&nbsp;Next ctl<br>&nbsp;End Sub<br><br><br>PaulF
 
Paul, I've never had Me.Undo error because there where no changes made. And as for &quot;Undo a record you may wish to save&quot;, looping through the controls and setting their values to null does basically the same thing as undoing them. This will also clear the values of all of the controls in the form, not just the text boxes.<br><br>I reallty don't see much difference.<br><br>RDH <p>Ricky Hicks<br><a href=mailto: rdhicks@mindspring.com> rdhicks@mindspring.com</a><br><a href= > </a><br>
 
Yes clearing the textboxes without using some code to ensure that you don't clear the data from a good record will cause the same problem as an undo command would. <br><br>&nbsp;I have several forms with&quot;Undo&quot; command buttons that were made using the Access wizard, and using the DoCmd.RunCommand accmdUndo,&nbsp;&nbsp;and if you click this button when no changes were made you get an error (2046).&nbsp;&nbsp;I have encounter that more than once, and have had to use the Me.Dirty property to prevent it from occurring.&nbsp;&nbsp;<br><br>What I've done to prevent the problem the original post was about, is to check and see if there where changes made to the record, using Me.Dirty, then if changes were made, to check each of the critical fields to ensure accuracy. If a discrepancy is found I&nbsp;&nbsp;display a message box and then cancel whatever event was to occur.&nbsp;&nbsp;I also use my own navigation buttons, so I can assign the code to do the checking regardless of how the user moves off of the record.<br><br>My intent was not to &quot;Blast&quot; your suggestion, only pass on another method, and some past experience.<br><br>PaulF<br>
 
John, I too were not intending to &quot;fuss&quot; on the subject. I have not used the DoCmd.RunCommand acCmdUndo. So I have not encountered the error you described, so I built a quick test db the try it out. You were correct, using the DoCmd.RunCommand accmdUndo, I did indeed received an error, but using Me.Undo did not error. Strange..........<br><br>I guess this is just another one of those &quot;Access Things&quot;.<br><br>RDH<br><br> <p>Ricky Hicks<br><a href=mailto: rdhicks@mindspring.com> rdhicks@mindspring.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top