Ms access VBA - Select Case with MsgBox from module function

Ms access VBA - Select Case with MsgBox from module function

由于人们经常在 MS access 的多个表单上使用同一条消息(即错误提示),我考虑将所有消息组织在 table 中并使用 public 函数调用它们:

Public Function message(msg_name As String, Optional msg_type As VbMsgBoxStyle)

output =   MsgBox DLookup("Msg_Text", "vba_msg", "Msg_Name='" & msg_name & "'"), _
           msg_type, _
           DLookup("Msg_Caption", "vba_msg", "Msg_Name='" & msg_name & "'")

简单明了。非常适合简单的通知(当然,"ouput = " 和括号不是必需的)。但是,我无法在 Select 案例上下文中使用这些消息:

Select Case message("test_message_name", vbYesNo)
  Case vbYes
    MsgBox "Yes"
  Case vbNo
    MsgBox "No"
End Select

按 "Yes" 或 "No" 都不会被识别。如果我也定义 "Case Else" ,我只会得到回应。 如果我在同一个 Sub 中执行相同的过程,一切正常:

output =   MsgBox DLookup("Msg_Text", "vba_msg", "Msg_Name='" & "test_message_name" & "'"), _
           msg_type, _
           DLookup("Msg_Caption", "vba_msg", "Msg_Name='" & "test_message_name" & "'")

Select Case output
  Case vbYes
    MsgBox "Yes"
  Case vbNo
    MsgBox "No"
End Select

我是不是漏掉了什么?

您刚刚忘记为函数分配 return 参数类型,并为函数分配 output

Public Function message(msg_name As String, Optional ms_type As VbMsgBoxStyle) As MsgBoxResult

message = output

或者只是

message =  MsgBox DLookup("Msg_Text", "vba_msg", "Msg_Name='" & msg_name & "'"), _
           msg_type, _
           DLookup("Msg_Caption", "vba_msg", "Msg_Name='" & msg_name & "'")