我使用 ms access vba 來創建我的程式,但我不喜歡默認的 msgbox 設計,所以我創建了一個名為“frm_msg”的表單
看起來像這樣 >>>

我做了一個帶引數的簡單函式來呼叫這個表單
Public Function custom_msg(msg_title, msg_txt, msg_icon, msg_button As String)
DoCmd.OpenForm "frm_msg"
Form_frm_msg.lbl_msg_title.Caption = msg_title
Form_frm_msg.lbl_msg_txt.Caption = msg_txt
If msg_icon = "success_icon" Then
Form_frm_msg.success_icon.Visible = 1
Form_frm_msg.error_icon.Visible = 0
Form_frm_msg.warning_icon.Visible = 0
Form_frm_msg.question_icon.Visible = 0
ElseIf msg_icon = "error_icon" Then
Form_frm_msg.success_icon.Visible = 0
Form_frm_msg.error_icon.Visible = 1
Form_frm_msg.warning_icon.Visible = 0
Form_frm_msg.question_icon.Visible = 0
ElseIf msg_icon = "warning_icon" Then
Form_frm_msg.success_icon.Visible = 0
Form_frm_msg.error_icon.Visible = 0
Form_frm_msg.warning_icon.Visible = 1
Form_frm_msg.question_icon.Visible = 0
ElseIf msg_icon = "question_icon" Then
Form_frm_msg.success_icon.Visible = 0
Form_frm_msg.error_icon.Visible = 0
Form_frm_msg.warning_icon.Visible = 0
Form_frm_msg.question_icon.Visible = 1
End If
If msg_button = "ok" Then
Form_frm_msg.btn_ok.Visible = 1
Form_frm_msg.btn_yes.Visible = 0
Form_frm_msg.btn_no.Visible = 0
ElseIf msg_button = "yes_no" Then
Form_frm_msg.btn_ok.Visible = 0
Form_frm_msg.btn_yes.Visible = 1
Form_frm_msg.btn_no.Visible = 1
End If
End Function
現在我的問題是如何讓它根據我點擊的按鈕回傳一個值?
并在 if 陳述句中像默認 msgbox 一樣使用它
if msgbox("Hallo world",vbInformation vbYesNo ,"Hallo") = vbYes Then
'do something ...
end if
uj5u.com熱心網友回復:
我發現可靠地做到這一點的最簡單方法是在表單上放置一個隱藏的文本框來保存您的“回傳”值。(我發現這更可靠,因為您可以控制默認值、型別等并從使用 TempVar 無法從 NULL 等中得出結論)
現在為自己撰寫一個簡單的包裝函式,它以對話模式打開表單。在按鈕的 OnClick 事件中,將隱藏的文本控制元件值設定為您想要的回傳值,然后隱藏(不要關閉)表單。因為它是對話框,它仍然會打開但不可見,并且控制流回傳到您的包裝器函式。
使用對文本控制元件的完全限定參考從表單上的隱藏文本欄位中獲取值,將其存盤在包裝函式中的變數中,并在需要時對其進行處理,或者按原樣回傳(如示例),然后使用 DoCmd.Close 以編程方式關閉表單。
像這樣簡單的事情;
Form_Name 是你的表單的全名 NameOfTextControl 是你的 onClick 事件設定的隱藏控制元件的名稱
Function customBox(msg_title, msg_txt, msg_icon, msg_button) as string
'I would usually pass a delimited string of values in the OpenArgs
'or set TempVars if necessary, then use your function code inside
'the form Open event to configure the form dynamically before displaying
DoCmd.OpenForm "Form_Name", acNormal, , , , acDialog
' When we get back here, the form is invisible, not closed
customBox = Forms!Form_Name!NameOfTextControl
DoCmd.Close acForm, "Form_Name", acSaveNo
End Function
所以你要做的就是用你的 customBox 函式替換 msgbox 并調整 vbYes / vbNo 常量來檢查你在表單中設定的任何值
if customBox(msg_title, msg_txt, msg_icon, msg_button) = "Yes" Then
'do something ...
end if
uj5u.com熱心網友回復:
您可以使用模塊和全域/公共變數與表單互動。
這是我專案中的一個片段:
' Opens a message box, using form ModernBox, similar to VBA.MsgBox.
'
' Syntax. As for MsgBox with an added parameter, TimeOut:
' MsgMox(Prompt, [Buttons As VbMsgBoxStyle = vbOKOnly], [Title], [HelpFile], [Context], [TimeOut]) As VbMsgBoxResult
'
' If TimeOut is negative, zero, or missing:
' MsgMox waits forever as MsgBox.
' If TimeOut is positive:
' MsgMox exits after TimeOut milliseconds, returning the result of the current default button.
'
' 2018-04-26. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function MsgMox( _
Prompt As String, _
Optional Buttons As VbMsgBoxStyle = vbOkOnly, _
Optional Title As Variant = Null, _
Optional HelpFile As String, _
Optional Context As Long, _
Optional TimeOut As Long) _
As VbMsgBoxResult
' Set global variables to be read by form ModernBox.
mbButtons = Buttons
mbPrompt = Prompt
mbTitle = Title
mbHelpFile = HelpFile
mbContext = Context
Call OpenFormDialog(ModernBoxName, TimeOut)
' Return result value set by form ModernBoxName.
MsgMox = mbResult
End Function
完整的代碼太多了,無法在此處發布,因此請隨意瀏覽VBA.ModernBox以了解所有詳細資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/325983.html
上一篇:如何在MicrosoftWord中搜索和突出顯示多個術語?
下一篇:捕獲使用通配符找到的目錄值
