Results 1 to 8 of 8
  1. #1
    Tuckejam is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Jan 2020
    Posts
    168

    Simple vbYesNo problem

    It appears I have lost my mind, or at lease what little coding knowledge I thought I had learned.



    I have a form with records and want a simple warning when deleting a record.

    when using the code below, the no button deletes a record, and the yes button appears to do nothing except if you click yes then no, it deletes 2 records ??

    Code:
     Private Sub Form_Delete(Cancel As Integer)   If MsgBox(" Are you sure you want to delete this record? ", vbYesNo) = vbYes Then
             Me.AllowDeletions = True
             DoCmd.RunCommand acCmdDeleteRecord
             Me.AllowDeletions = False
          Else
             Exit Sub
        End If
    End Sub
    thanks for the help

  2. #2
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    Add line
    Me.Requery
    just before the Else

  3. #3
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Is the OnDelete event the event to use?
    There's a reason for the cancel arguement.
    Its not clear how you are invoking the delete.

    I would use an OnClick event of a command button with your code.

    I've never used the OnDelete event but I would imagine you would do something like

    Code:
    Private Sub Form_Delete(Cancel As Integer) 
    
    If MsgBox(" Are you sure you want to delete this record? ", vbYesNo) = vbNo Then 
    
             cancel = true
    
        End If
    
    End Sub
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    The code is wrong for the event IMO. User deletes, record moves into a buffer, event runs and if you click no you've already deleted anyway. Yes should cause you run a delete command on the next record (which has now become the current record) because of the way you've coded it. If using this event, I believe you simply cancel or not, which restores the record, or not.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914

  6. #6
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Thats another event I've never used or had a use for, or even looked at before.
    now I've noticed a "before screen tip" event. I think I'll be googling for a while.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by moke123 View Post
    Thats another event I've never used or had a use for, or even looked at before.
    now I've noticed a "before screen tip" event. I think I'll be googling for a while.
    I think I might have used it once.

    Found it
    Code:
    Private Sub Form_AfterDelConfirm(Status As Integer)
     Select Case Status
        Case acDeleteOK
            MsgBox "Record deleted!", , "Delete Result"
        Case acDeleteCancel
            MsgBox "Delete record cancelled!", , "Delete Result"
        Case acDeleteUserCancel
            MsgBox "Delete record cancelled!", , "Delete Result"
     End Select
    End Sub
    
    Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
    Response = acDataErrContinue
     ' Display custom dialog box.
    If MsgBox("Are you sure you want to delete this record?", vbYesNo, "Delete Confirmation") = vbNo Then
        Cancel = True
    End If
    End Sub
    Last edited by Welshgasman; 11-23-2020 at 07:24 AM.

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by Tuckejam View Post

    Code:
     Private Sub Form_Delete(Cancel As Integer)   
       If MsgBox(" Are you sure you want to delete this record? ", vbYesNo) = vbYes Then
             Me.AllowDeletions = True
             DoCmd.RunCommand acCmdDeleteRecord
             Me.AllowDeletions = False
       Else
             Exit Sub
       End If
    End Sub
    I agree that another event should be used here...but what I'm really confused about is your code and scenario...as presented.

    The line

    Me.AllowDeletions = True

    would indicate that you have AllowDeletions set to No/False elsewhere...either in code or in the Properties Pane. If this is true (is it?) how would you get to the Form_Delete event in the first place? IF this is a false assumption, why have Me.AllowDeletions = True at all?

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Simple If/Then Problem
    By nnaxor in forum Modules
    Replies: 5
    Last Post: 08-02-2016, 03:44 PM
  2. Help With What Is (Probably) A Simple Problem
    By shoelesscraig in forum Access
    Replies: 23
    Last Post: 08-25-2015, 11:45 AM
  3. Help with vbYesNo msgbox
    By focosi in forum Access
    Replies: 10
    Last Post: 08-13-2011, 01:48 PM
  4. Very simple problem
    By alexc333 in forum Queries
    Replies: 8
    Last Post: 07-21-2011, 07:35 AM
  5. simple form problem
    By kcsun in forum Forms
    Replies: 0
    Last Post: 08-12-2010, 12:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums