Results 1 to 4 of 4
  1. #1
    lowesthertz is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    19

    Closing a form and discarding Changes

    Good morning,
    I have a form which has a button on it which is meant to close the form without saving any changes to the form. The caviat is that I want to check to see if the form is empty, and if so, delete the record (I am really only looking at 4 key fields).

    So what I am looking at are three scenarions:

    1) when the user clicks the button, all four fields are null. In this case, the record gets deleted.
    2) when the user clicks the close button, the four fields are filled in. In this case I want to undo the changes to the form.
    2A) once the changes have been undone, if the 4 fields are null, delete the record, close the form
    2B) once the changes have been undone, if the 4 fields are not null, keep the record (without changes), close the form

    The code currently looks like this. I can get scenario 1 and 2B to work, but not 2A. It gets as far as undoing the changes and then closes the form. It skips the second if statement. I am not a programmer, and I bet this is something really stupid that is tripping me up.


    Private Sub Command366_Click()
    Dim canVal As Integer
    Dim SOAValue As Boolean
    Dim VSValue As Boolean
    Dim PDValue As Boolean
    Dim InitValue As Boolean

    SOAValue = IsNull(Combo326)
    VSValue = IsNull(Combo354)
    PDValue = IsNull(Text127)
    InitValue = IsNull(Text257)

    canVal = MsgBox("You have elected to close the form without saving." & vbCr & vbCr & "Are you sure you want to continue?", vbYesNo + vbExclamation, "Close Form")
    If canVal = vbNo Then
    DoCmd.CancelEvent
    ElseIf canVal = vbYes Then

    MsgBox "SOA = " & SOAValue & " & VS = " & VSValue & " & PD = " & PDValue & " & Init = " & InitValue, vbInformation + vbOKOnly


    If (SOAValue = False) And (VSValue = False) And (PDValue = False) And (InitValue = False) Then
    MsgBox "4 field filled in", vbOKOnly + vbInformation, "Returning to Main"


    Me.Undo
    MsgBox "removed changes", vbOKOnly + vbInformation, "Returning to Main"
    End If


    If (SOAValue = True) Or (VSValue = True) Or (PDValue = True) Or (InitValue = True) Then
    MsgBox "This is not a complete record, it will be deleted", vbOKOnly + vbInformation, "Record to be Deleted"
    Me.Undo
    DoCmd.SetWarnings False
    RunCommand acCmdDeleteRecord
    DoCmd.SetWarnings True
    MsgBox "Record Deleted", vbOKOnly + vbInformation, "Delete"
    End If


    DoCmd.Close acForm, "CARForm"
    End If


    End Sub

    Your help would be appreciated.
    thanks
    Chris

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Have you step-debugged? Follow the code as it executes one line at a time. Check that variables are populated as expected.

    Review link at bottom of my post for debugging guidelines.

    I caution against over-using message boxes - users get annoyed with all those extra clicks - sometimes just need to do things without notifying user.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    lowesthertz is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    19
    June7, message boxes are solely for me at this point to see what is going on, they will be removed once it is working.

    The following are defined as Boolean variables, once the changes are un-done, do the variables need to be refreshed? Another words, initially they would be false, then the undo happens and they become true. If I pop a message box after the undo, I still see them as false. Just a hunch.

    Dim SOAValue As Boolean
    Dim VSValue As Boolean
    Dim PDValue As Boolean
    Dim InitValue As Boolean

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Yes. Would have to retest the values of the listboxes.

    Personally, I would not use the Booleans. I would just use the IsNull() functions in the If conditionals.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 6
    Last Post: 01-06-2015, 12:37 PM
  2. closing form
    By joshynaresh in forum Access
    Replies: 6
    Last Post: 02-05-2014, 09:06 AM
  3. Don't save when closing form
    By thefairman in forum Forms
    Replies: 4
    Last Post: 02-21-2012, 09:00 PM
  4. Replies: 5
    Last Post: 12-22-2011, 01:12 PM
  5. Closing a form through VBA
    By ghostmachine in forum Forms
    Replies: 4
    Last Post: 12-13-2010, 01:57 PM

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