VBA Msgbox - No Default Button - vba

I would like to know if it's possile to show a Msgbox in VBA (Excel 2007), without any button being set as default?
I have a yes\no button and I don't want the user to hit return or the space bar and accidentally trigger a default button.
Anyone know if this is possible so that neither button on my form is set to default, forcing the user to click yes or no?
If MsgBox("Message goes here", vbYesNo + vbQuestion ) = vbYes Then
'Something
Else
'Else
End If

Another workaround (other than a UserForm, or trapping Enter & Space etc) is to
Add a third button option that is an invalid choice
Make this button the default
Continue to show the message box if this option is picked
code
Dim lngMsg As Long
Do
lngMsg = MsgBox("Message goes here (Yes or No)", vbQuestion + vbYesNoCancel + vbDefaultButton3)
Loop While lngMsg = vbCancel
If lngMsg = vbYes Then
' route a
Else
' route b
End If

No this is not possible unfortunately.
A (not particularly nice) workaround would be to code the message box using a UserForm. Probably not worth doing given the benefit over the extra code you'd have to maintain.

If MsgBox("Message goes here", vbYesNo + vbQuestion **+ vbDefaultButton2**) = vbYes Then
'Something
Else
'Else
End If

Related

Alert Text Before Save

I'm still new in this language. I want to create alert text when users click certain button. However, I don't know how to do it. Anyone can help me on this. I provide some pictures for the idea.
Here is the example of the alert that I want to make
You can try below codes for alert message before doing any action.
Private Sub cmdSaveRecord_Click()
If MsgBox("Are you sure want to save?", vbYesNo + vbQuestion, "Confirmation") = vbYes Then
'Write code here to save your data
RunCommand acCmdSaveRecord
Else
'Write code here for cancallation
MsgBox "Cancelled by user"
End If
End Sub

Command Button in msgbox vba possible?

si it possible to add a command button in the msgbox window in vba?
For example, i want to add a cancel button that stops the code rather than continuing it. I could create a new userform, but it would be nice if i save some space and use the msgbox that is already here.
VBA has several different types of MessageBoxes with built in command buttons for this very purpose. The type of buttons included in the message box is declared as the second parameter - MsgBox(Prompt, Buttons as)
The types you are probably interested in are:
vbYesNo
vbYesNoCancel
vbAbortRetryIgnore
vbOkCancel
vbRetryCancel
These Buttons return integer values that need to either be stored or used for comparison.
VBA has these integer answers stored as constants (e.g. vbOK = 1, VbCancel = 2, etc.) See Microsoft Developer Network MsgBox Function for more details on that.
Sub mySub()
Dim answer as Integer
answer = MsgBox("Would you like to choose yes?", vbYesNoCancel)
If answer = vbYes Then
'Do whatever you want
ElseIf answer = vbNo Then
'Do whatever
Else
Exit Sub
End If
End Sub
Try this:
If MsgBox("Cancel or Continue, are you sure?", vbOKCancel) = vbOK Then
'continue whatever you want to do.
End if

Message Box on Record Change

I have a form that queries records that the user may want to edit. I want the user to only be able to save the record if they've clicked the 'Save' button. Hitting the 'Close' button will prompt the user if they haven't saved yet, and may ask if they want to save.
I'm encountering a problem when the user changes through the records: I want to have a Y/N message box prompt the user for saving the changes they made to the previous record, otherwise their changes will be discarded. I have the following code set up:
Private Sub CmdCloseForm_Click()
If Me.Dirty Then
'checks that needed fields are completed
If IsFormValidated = False Then
If MsgBox("Required fields aren't filled." & vbCrLf & "Would you like to close this form without saving?", vbYesNo + vbQuestion + vbDefaultButton2, "Warning") = vbNo Then
Exit Sub
End If
Else
'checks if form has been saved already
If mSaved = False Then
Select Case MsgBox("Form hasn't been saved. Do you want to save and close?" & vbCrLf & "If you click 'No' the form will close without saving.", vbQuestion + vbYesNoCancel, "Save As")
'selecting yes will save and close form
Case vbYes:
mSaved = True
'selecting no will close the form w/o saving
Case vbNo:
mSaved = False
'selecting cancel will cancel out of the prompt
Case vbCancel:
Exit Sub
End Select
ElseIf mSaved = True Then
'if form has been previously saved, will finally close the form
If MsgBox("Would you like to close this form?", vbYesNo + vbQuestion + vbDefaultButton2, "Close Form") = vbNo Then
Exit Sub
End If
End If
End If
End If
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
'won't save automatically unless mSaved is true
Private Sub Form_BeforeUpdate(Cancel As Integer)
'if mSaved = False then the record won't save
If mSaved = False Then
Cancel = True
Me.Undo
Cancel = False
End If
End Sub
I pretty much want the 'CmdCloseForm' Message Boxes to run when the user moves on to the next record. Is there any way to do this?
If you really want to ask user about saving changes for each row, ask in Form_BeforeUpdate and in Form_BeforeDelConfirm. Those events will be fired each time when user changes edited record, or when subform with edited records loses focus, or user closes form with data. But this is not good solution because messageboxes will be too annoying. The better way is to copy edited data to temporary table, allow user edit the data and copy back to source table changed data when user clicks "Save". It's quite simple if you don't need multi-user data editing, in this case you will need some additional code for avoiding collisions.

Excel "Workbook_BeforeClose" event not firing again after canceled

Update: After more research, I found this duplicate question: Excel 2016 Workbook.BeforeClose event firing every other time bug. It seems I was using the wrong keywords, and that this is a bug, not a problem with my code. However, I cannot seem to download the version mentioned in the solution. I am running Windows 7 and using Microsoft Office 365 Pro Plus, and Office is stating that the most up to date version available is 16.0.6965.2105
I am trying to use the Workbook_BeforeClose event to test whether a checkbox is checked or not. If not, the user is prompted on whether they want to continue closing with out checking the box. If they choose "Yes," the sheet is cleared and the workbook is saved. If they choose, "No," the box is checked and, "Cancel" is set to true.
This works fine the first time the Workbook_BeforeClose event runs. However, the second time the sheet is closed, the standard Excel "Want to save your changes to..." dialogue box comes up and the Workbook_BeforeClose event does not fire. If I click cancel on the dialogue box and close the workbook a third time, the event fires. Something is being reset when, "Cancel," is clicked in the dialogue box, but I can't figure out what it is. My code is below:
Public Closing as Boolean
Sub Workbook_BeforeClose(Cancel As Boolean)
Debug.Print "Workbook_BeforeClose"
If Closing = True Then Exit Sub 'Closing is used as a switch to stop the event from looping on Application.ThisWorkbook.Close below
Closing = True
If Sheets(1).DraftCheckBox = False Then
If MsgBox("This file is not being saved as a draft. This workbook will be cleared if the draft box is not checked." & vbCr & vbCr & "Would you like to continue?", vbYesNo, "Warning") = vbYes Then
'If "Yes" selected
'Stuff happens here
Application.ThisWorkbook.Close savechanges:=True
Else
'If "No" selected
Sheets(1).DraftCheckBox = True
Cancel = True
End If
End If
Closing = False
End Sub
I know for a fact that Application.EnableEvents is set to True. I also know that the event itself is not firing because there is a stop on the very first line at "Debug.Print" This stop is activated after the first close and I can step through the code. It does not activate at all after the second close, either before or after the dialogue box. Is there anything that I should be doing to prevent the dialogue box from coming up and to force the Workbook_BeforeClose event every time the workbook is closed?
You are already closing the workbook you don't want to call close again, use Save instead.
You also don't need the extra variable if you use the code below. One big question remains...how is your user imitating the Close event? If by the red X then the code below should work. If by a button on your form what does that button code look like?
Note: This is untested code as I don't have your workbook.
Sub Workbook_BeforeClose(Cancel As Boolean)
If Sheets(1).DraftCheckBox = False Then
If MsgBox("This file is not being saved as a draft. " + _
"This workbook will be cleared if the draft box is not checked." _
& vbCr & vbCr & "Would you like to continue?", _
vbYesNo, "Warning") = vbYes Then
'If "Yes" selected
'Stuff happens here
Application.ThisWorkbook.Save
Cancel = False 'Just to be sure.
Else
'If "No" selected
Sheets(1).DraftCheckBox = True
Cancel = True
End If
End If
End Sub
HTH

VBA : vbYesNo displays a button with OK and nothing else

Title speaks for itself.
Here is an illustration
and here is the line of code I'm using.
If MsgBox("Are you sure?", vbYesNo) = Yes Then Exit Sub
I'm also using EXTRA! X-treme
I'd have to guess that EXTRA! X-treme (wow! hyperbole much?!) is messing with the vb constants, or at least not respecting them if it is intercepting calls to the MsgBox function.
vbYesNo should be a constant numeric value of 4.
Also, instead of comparing the result of MsgBox to Yes, you should probably be comparing it to vbYes (numeric value of 6).
I think you're using VBA correctly (except for the vbYes part), so this might be an EXTRA! X-treme bug (or under-documented feature).
Solution
If MsgBox("Are you sure?", 4) = 6 Then Exit Sub
If DialogResult.OK = Windows.Forms.MessageBox.Show("Are You Sure ? ", "Choose Folder",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Question) Then
' Do Code
End If