Understanding what response codes come back from MsgBox

三世轮回 提交于 2019-12-04 00:41:18
Bobby

MsgBox does return an Enum(eration) called MsgBoxResult, which is basically nothing else then numeric values with a 'label'. 6 and 7 in this case are members of this enum, which are mapped to the answers Yes and No.

Using so called 'magic numbers' instead of Constants or Enums should avoided whenever possible.

Basically, you could rewrite the code to this:

Dim message As Integer
message = MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login")
If message = MsgBoxResult.Yes Then
    Range("A1").Value = "You may proceed"
    ActiveWorkbook.Activate
ElseIf message = MsgBoxResult.No Then
    ActiveWorkbook.Close
End If

Might be that the Enum is called vbMsgBoxResult or something... I don't have an Office to verify this, just Visual Studio.

While we are on it... this might be easier to understand:

Select Case MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login")
    Case MsgBoxResult.Yes
        Range("A1").Value = "You may proceed"
        ActiveWorkbook.Activate

    Case MsgBoxResult.No
        ActiveWorkbook.Close

    Case MsgBoxResult.Cancel
        ' he clicked cancel '

End Select

It's very poorly written code, "6" and "7" are the values of the constants "vbYes" and "vbNo" where are returned when the user clicks Yes or No on the dialog.

Reference: http://www.techonthenet.com/access/constants/msgbox_ret.php

The code should say

If message = Constants.vbYes 

instead of

If message = 6

So that it is clear what is happening.

Ben McCormack

When I first started with MsgBox answers, I almost always declared the answer as an Integer. However, I learned that the best thing to do is to declare your message variable as VbMsgBoxResult, which is an enumeration that will always show the available answers. In the picture below, the IDE (e.g. the Visual Basic for Application editor) will show you the possible options available in VbMsgBoxResult.

You could store your answer variable as an Integer since all of the variables in the Enumeration (e.g. vbAbort, vbYes, vbOK, etc.) do in fact resolve to integers. However, you have to figure out what the integer values for those variables are every time you want to reference them. In my opinion, it's a better practice to store your answer as VbMsgBoxResult so you can actually see the available answers.

This link is for VBScript, but I think the return codes should be the same: MsgBox Function Reference

The Return Codes tell you which button was clicked:

1   OK
2   Cancel
3   Abort
4   Retry
5   Ignore
6   Yes
7   No

These are return value from MsgBox(). The author should have used their symbolic value instead to make the program more readable:

vbYes   6
vbNo    7

See this MSDN article for more info

6 and 7 are the return codes from the MsgBox method. Basically, when MsgBox is called, it shows a message-box to the user, who clicks either "Yes", "No", or "Cancel". The user's selection is returned from the MsgBox method as a number, where 6 is Yes, and 7 is No.

It is considered best-practice not to use these numbers in your code directly, but instead to use Microsoft supplied constants which represent them. Your code could be re-written as:

Private Sub CommandButton1_Click()
    Dim message As Integer
    message = MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login")
    If message = vbYes Then
        Range("A1").Value = "You may proceed"
        ActiveWorkbook.Activate 
    ElseIf message = vbNo Then
        ActiveWorkbook.Close
    ElseIf message = vbCancel Then
        'Do nothing.
    End If
End Sub

The 6 and 7 are hard coded values that hold a special meaning. The 'MsgBox("Click Yes...")' call will return a number that will let your code determine what the user did with the message box and you can then use conditionals (your IF statements) to decide what to do next.

A full list of these special values can found in the MSDN documentation here:

http://msdn.microsoft.com/en-us/library/139z2azd(VS.80).aspx

Just rewrite to the equivalent:

Private Sub CommandButton1_Click()
  Dim optionSelected As VbMsgBoxResult
  optionSelected = MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login")
  If optionSelected = vbYes Then
    Range("A1").Value = "You may proceed"
    ActiveWorkbook.Activate 
  ElseIf optionSelected = vbNo Then
    ActiveWorkbook.Close
  End If
End Sub

And move on

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!