Results 1 to 7 of 7

Thread: Cancel button

  1. #1
    Join Date
    Mar 2004
    Posts
    38

    Smile Unanswered: Cancel button

    How do I code a cancel button, which would cancel the adition of a new record, even if there was already some data in that record?

    Thanx ahead

  2. #2
    Join Date
    Nov 2003
    Posts
    1,487
    Just delete the record....

    or

    in the On Click event of a Command button enter this code.

    On Error Resume Next
    DoCmd.Runcommand acCmdDeleteRecord

    If you dont want the "Are You Sure...." message then do it this way...

    On Error Resume Next
    DoCmd.SetWarnings False
    DoCmd.Runcommand acCmdDeleteRecord
    DoCmd.SetWarnings True


  3. #3
    Join Date
    Mar 2004
    Posts
    38

    Hmm, this doesnt seem to work

    Well - I set it as you said, and it doesnt sem to do the trick. I enabled the warnnings to check if its trying to delete the record - I get the warnning, but even if I choose "Yes" it doesnt delete the record....
    Why?

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Hi
    I'm not familiar with this command, but I suspect it would delete whichever record the focus was on, rather than the most recently inserted record. What about intercepting all this before the record is added to the table? There is a BeforeInsert event for the form - using this event, you can use a msgbox to ask the user if they really want to insert a new record e.g.

    If msgbox ("You sure you want to add a new record?", vbYesNo) = vbNo then

    cancel = true

    end if
    Testimonial:
    pootle flump
    ur codings are working excelent.

  5. #5
    Join Date
    Mar 2004
    Posts
    38

    Still not working

    Quote Originally Posted by dd_plus
    Well - I set it as you said, and it doesnt sem to do the trick. I enabled the warnnings to check if its trying to delete the record - I get the warnning, but even if I choose "Yes" it doesnt delete the record....
    Why?
    Well - I have added that code to the "After Update" event of the form and added a toggle to see if any changes were made - still NOT working, and this event is being called per controller and not only per record.

    Any way of having the form decide on saving or not saving the record ?
    Last edited by dd_plus; 05-15-04 at 22:37.

  6. #6
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Hi

    Sorry to hear it's not getting there. However, you wouldn't want it in the AfterUpdate - that would be fired everytime there is a change, and this occurs after the change has been saved to the table so there is nothing to cancel. It should have been in the Form_BeforeInsert event. However, my suggestion is flawed anyway - it asks the person if they want to add a record before they've actually added any data. You want to delete a record after it has been inserted.



    CyberLynx's suggestion was spot on and I suspect it was working, just that it was deleting records with the focus not just the last one (that’s what happens when I try it anyway). You could stick with it and send out a message box stating that the CURRENT record will be deleted. Or, on the command button being pressed you could move to the last record and delete that.

    Private Sub Command4_Click()
    On Error GoTo Err_Command4_Click

    Dim rst As DAO.Recordset

    If MsgBox("You sure you want to delete the last record added?", vbYesNo) = vbYes Then

    Set rst = Me.RecordsetClone

    rst.MoveLast



    Me.Bookmark = rst.Bookmark



    rst.Close



    On Error Resume Next

    DoCmd.SetWarnings False

    DoCmd.RunCommand acCmdDeleteRecord

    DoCmd.SetWarnings True



    End If

    Exit_Command4_Click:

    Exit Sub

    Err_Command4_Click:



    MsgBox Err.Description

    Resume Exit_Command4_Click



    End Sub

    You'd need a reference setting to DAO 3.6 in the VBE references section. The above is still far from perfect - it will delete the last record irrespective of whether or not it is a "new" record. There is a get around, but I'm starting to think CyberLynx's idea is better afterall. Just put in a message box letting the user know exactly what they are about to do and let them work it out for themselves. At least they can see what they are deleting. Apologies for taking you round the houses as I thought it through.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  7. #7
    Join Date
    Apr 2004
    Location
    Sydney Australia
    Posts
    369
    Just another thought

    When the person "clicks" an entry is made in a field and both an Append and Delete query run on the basis of that entry being the critieria. This way the record is removed from your "using" table and appended to a storage table. When the "click" is done a Now() is also placed in a field the record in question.

    A simple macro with two SetValue actions (the tag and the Now()) and two OpenQuery actions will do the job.

    To go one step further the user can open a form with the "deleted" records sorted on the field that has Now() inserted and the sort is in Descending order and they can "click" and run the reverse of the above....which will transfer the record back to your "using" tabe and remove it from the "storage" table. You could then call up all the records form the "using" table that had entries for the "tag" and Now() and they would be the records where the user changed their mind about deleting.

    Why someone would go to the expense and trouble of buying computer games when they can jnust fiddle with Access is beyond me

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •