Consulting

Results 1 to 14 of 14

Thread: Solved: Message Box closing issues

  1. #1
    VBAX Regular FF Ethan's Avatar
    Joined
    May 2010
    Location
    Boardman, OR
    Posts
    28
    Location

    Solved: Message Box closing issues

    I have a MsgBox that will close even though I click "No" can someone help?
    [vba]On Error Resume Next
    YesNo = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.")
    If No Then
    DoCmd.Close_Click = False
    End If

    DoCmd.Close
    Exit_Close_Click:
    Exit Sub
    Err_Close_Click:
    MsgBox Err.Description
    Resume Exit_Close_Click

    End Sub
    [/vba]
    Ethan Ellis
    Before VBA Express After VBA Express.

  2. #2
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Hard to tell from the code you posted. Can you post the whole sub or at least the part before On Error Resume Next

    Might try to change your msgbox and if statement from this:

    [VBA]YesNo = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.")
    If No Then
    DoCmd.Close_Click = False
    End If
    [/VBA]

    To this:

    [VBA]MsgBox ("Are you sure you want to close? " & "This will close the form."), vbYesNo + vbCritical
    If vbNo Then
    DoCmd.Click_Close = False
    End If[/VBA]
    Peace of mind is found in some of the strangest places.

  3. #3
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    I use

    MsgBox ("Are you sure you want to close? " & "This will close the form."), vbYesNo + vbCritical
    If YesNo = vbNo Then Exit Sub

  4. #4
    VBAX Regular FF Ethan's Avatar
    Joined
    May 2010
    Location
    Boardman, OR
    Posts
    28
    Location
    [VBA]Private Sub Close_Click()
    On Error Resume Next
    YesNo = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.")
    If vbNo Then
    DoCmd.Close_Click = False
    End If






    DoCmd.Close
    Exit_Close_Click:
    Exit Sub
    Err_Close_Click:
    MsgBox Err.Description
    Resume Exit_Close_Click

    End Sub
    [/VBA]
    Sorry I thought i had all of it if you need more let me know.
    Ethan Ellis
    Before VBA Express After VBA Express.

  5. #5
    VBAX Regular FF Ethan's Avatar
    Joined
    May 2010
    Location
    Boardman, OR
    Posts
    28
    Location
    OBP I tried yours but it says End if Without Block If?
    I dont know what I am doing wrong.
    Ethan Ellis
    Before VBA Express After VBA Express.

  6. #6
    VBAX Regular FF Ethan's Avatar
    Joined
    May 2010
    Location
    Boardman, OR
    Posts
    28
    Location
    austenr your code says Method or data Member not found?
    Ethan Ellis
    Before VBA Express After VBA Express.

  7. #7
    VBAX Regular FF Ethan's Avatar
    Joined
    May 2010
    Location
    Boardman, OR
    Posts
    28
    Location
    [VBA]Option Compare Database

    Private Sub New_Click()
    On Error Resume Next
    OkOnly = MsgBox("Please enter a value first!", vbOKOnly = vbCritical, "Needs Data!")


    DoCmd.GoToRecord , , acNewRec
    Exit_New_Click:
    Exit Sub
    Err_New_Click:
    MsgBox Err.Description
    Resume Exit_New_Click

    End Sub
    Private Sub Save_Click()
    On Error Resume Next
    OkOnly = MsgBox("Are you sure you want to save these values?", vbOKCancel + vbCritical, "Your results will be entered.")



    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    Exit_Save_Click:
    Exit Sub
    Err_Save_Click:
    MsgBox Err.Description
    Resume Exit_Save_Click

    End Sub
    Private Sub Close_Click()
    On Error Resume Next
    YesNo = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.")
    If vbNo Then
    DoCmd.Click_Close = False
    End If





    DoCmd.Close
    Exit_Close_Click:
    Exit Sub
    Err_Close_Click:
    MsgBox Err.Description
    Resume Exit_Close_Click

    End Sub
    [/VBA]

    Here is the whole code just in case.
    Ethan Ellis
    Before VBA Express After VBA Express.

  8. #8
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    In my code it has to be on One Line, did you put it on 1 line?
    as in
    If YesNo = vbNo Then Exit Sub

  9. #9
    VBAX Regular FF Ethan's Avatar
    Joined
    May 2010
    Location
    Boardman, OR
    Posts
    28
    Location
    Right after Sub Close_Click?
    Ethan Ellis
    Before VBA Express After VBA Express.

  10. #10
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    Like this
    YesNo = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.")
    If YesNo = vbNo Then Exit Sub
    DoCmd.Close
    Exit_Close_Click:
    Exit Sub

  11. #11
    VBAX Regular FF Ethan's Avatar
    Joined
    May 2010
    Location
    Boardman, OR
    Posts
    28
    Location
    Perfect thank you!
    Ethan Ellis
    Before VBA Express After VBA Express.

  12. #12
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    There's no need to have a variable to store the result of the msgbox unless you want to use it multiple times.

    [VBA]Private Sub Close_Click()
    If vbYes = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.") Then
    DoCmd.Close
    End If
    End Sub
    [/VBA]

    Also I'm not sure what your other events are supposed to be doing - you're asking the user a question, then doing the same thing regardless of the answer.

  13. #13
    VBAX Regular FF Ethan's Avatar
    Joined
    May 2010
    Location
    Boardman, OR
    Posts
    28
    Location
    geekgirlau,
    Your absolutely right I didnt notice that before thank you for pointing that out! The ways that I've used work but will they work the same way for the other MsgBox's?
    Ethan Ellis
    Before VBA Express After VBA Express.

  14. #14
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    I'm not following the logic:

    [VBA]
    Private Sub Close_Click()
    On Error Resume Next
    YesNo = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.")
    ' this is NOT checking the result of your message box
    If vbNo Then
    DoCmd.Click_Close = False
    End If
    ' regardless of the answer to the question, the form will be closed
    DoCmd.Close
    Exit_Close_Click:
    Exit Sub
    Err_Close_Click:
    MsgBox Err.Description
    Resume Exit_Close_Click
    End Sub

    [/VBA]

Posting Permissions

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