Cancelling AddNew entry.

During an AddNew procedure, how can I allow the user to cancel what he has typed so it does not get written to the database?  I am using VB4/32.

Thanks,
Don
derossAsked:
Who is Participating?
 
mrmickConnect With a Mentor Commented:
If you do not invoke the .UpDate method, it will be canceled.  Once you perform and .Update, you'll have to just delete the record.
0
 
mrmickCommented:
By the way, this is also true of the .Edit method, if you don't invoke the .Update method, changes are discarded.
0
 
derossAuthor Commented:
But if I move to another record using the data control or even if I close the form that my AddNew procedure is on, the record still gets added.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
mrmickCommented:
Whoops, Until you use the update method, no changes are made, but the record is still added.

To delete the most recently added record use the following (assumes rs = your recordset):

   rs.bookmark = rs.Bookmark = rs.LastModified
   rs.delete

Note: Always keep a backup the database until you're sure your code is working properly when coding any type of delete.

0
 
mrmickCommented:
Make sure your procedure only executes the above code if a record was added and the user decided to cancel.
0
 
mrmickCommented:
A possible method of insuring the added record is used or deleted would be to create a module level variant variable called AddedUnused.   Assign the bookmark of the newly added record to AddedUnused.  Then call a separate procedure that looks like this to cancel:

Sub CheckUnused()
Dim rs As Recordset
 If Not IsEmpty(AddedUnused) Then
  rs.Bookmark = AddedUnused
  rs.Delete
  AddedUnused = Empty
 End If
End Sub

If you choose to use the above sample, make sure you set AddedUnused = YourBookMark directly after you invoke the AddNew method... AND directly after you invoke the .Update method, set AddedUnused = Empty.  In the form unload event, you could call CheckUnused() allowing your user to close the form as another means of canceling the edit OR you could make sure AddedUnused = Empty in the Form_QueryUnload event to decide if you would permit your user to close the form without first canceling.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.