Alert Text Before Save - vba

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

Related

Is it possible to have VBA MsgBox as type vbYesNo and vbExclamation simultaneously?

I am trying to get a message box in vba to display as a regular yes no box:
But with the Exclamation icon as well:
I can do either easily by vba. Like the vbExclamation version code below:
MsgBox "Are you sure you want to continue?", vbExclamation, "Warning!"
or the version for the vbYesNo:
Dim msgboxResult As VbMsgBoxResult
msgboxResult = MsgBox("Are you sure you want to continue?", vbYesNo, "Warning!")
If msgboxResult = vbNo Then Exit Sub
But am not sure if the two can be combined?
On the MSDN vba refrence for the MsgBox function, https://msdn.microsoft.com/en-us/library/aa445082(v=vs.60).aspx it does say at the very bottom the following:
"Note: To specify more than the first named argument, you must use MsgBox in an expression. To omit some positional arguments, you must include the corresponding comma delimiter."
Is this referring to what I need, and if so, how would I implement this please?
I think
msgboxResult = MsgBox("Are you sure you want to continue?", vbExclamation + vbYesNo, "Warning!")
would do it the way you want to have.

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.

Wrong Result after Yes/No Button

I'm currently adding a relatively simple user decision Box to my excel file, but there seems to be some issue that I can't find.
The basic idea is that if a user Clicks Yes everything is ok and a message box pops up telling him that and if he/she clicks no he should input a calendar date which should be added to a sheet.
I already have issues with my current code where the date-adding part isn't even integrated yet.
Private Sub CommandButton1_Click()
MsgBox "Volume already in rolling forecast?", vbYesNo + vbQuestion, "Rolling Forecast Integration"
If Answer = vbYes Then
MsgBox "O.K", vbOKOnly, "O.K"
Else
MsgBox "Please specify", vbQuestion, "Contact Me"
End If
End Sub
When I click Yes it should the OK Message box, right? Well it always displays the "Please specify" MsgBox and I don't see why.
You need to set-up Answer with the result coming from the MsgBox.
Modify your first line of :
MsgBox("Volume already in rolling forecast?", vbYesNo + vbQuestion, "Rolling Forecast Integration")
To:
Answer = MsgBox("Volume already in rolling forecast?", vbYesNo + vbQuestion, "Rolling Forecast Integration")
you can also shorten down your code:
Private Sub CommandButton1_Click()
If MsgBox("Volume already in rolling forecast?", vbYesNo + vbQuestion, "Rolling Forecast Integration") = vbYes Then
MsgBox "O.K", vbOKOnly, "O.K"
Else
MsgBox "Please specify", vbQuestion, "Contact Me"
End If
End Sub

VBA Msgbox - No Default Button

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