I have a button on my form to clear several fields. When people click it, I want it to ask: "Are you sure you want to clear data - Yes/No". If the user says yes, it runs the code. If the user says no, then nothing happens
I have a button on my form to clear several fields. When people click it, I want it to ask: "Are you sure you want to clear data - Yes/No". If the user says yes, it runs the code. If the user says no, then nothing happens
NOTE: vbDefaultButton2 sets the default button to 'No'.Code:Private Sub MyButton_Click If MsgBox("Are you sure you want to clear data - Yes/No", vbQuestion+vbYesNo+vbDefaultButton2, "Clear Data?") = vbNo Then Exit Sub 'YOUR CODE GOES HERE End Sub
If you prefer it to be Yes, change to vbDefaultButton1 or omit that part
Thank you! Also, thanks for mentioned the default button thing.
You should learn how to assign the message box function result to a variable in case you ever need more than 2 buttons (like Yes/No/Cancel). While you could write code that repeats the entire message box construct 3 times, this would be better IMHO:
You can use vb constants or integers - the effect is the same even though result is dim'd as an integer. Note that parentheses for the function must not be used unless the function is used to return a result (you can tell by the fact that it is set to = something)Code:Dim result As Integer result = Msgbox ("Some message requiring 3 choices",vbYesNoCancel) If result = 6 Then do yes stuff ElseIf result = 7 Then do no stuff Else do cancel stuff End If
Here's more on the options https://www.techonthenet.com/access/...msgbox_ret.php
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
Hi micron
In this case there was no need for alternative outcomes hence just the one line of code.
I know that:Is there something wrong with trying to teach somebody something?in case you ever need more than 2 buttons
Sorry ....
By chance, I'd just written an answer at StackOverflow where I did much the same thing.
The moderators at SO deleted my post as according to them itwasn't answering the question in hand