我不想使用msgBox,而是想通过表单"frmMsg"创建我的msgBox。"frmMsg"表单有两个底部(Ok和No(和一个用于显示消息的标签(lblMsg(。"frmMsg"属性:弹出=是,模式=是。
我打开表单的功能是MsgInfo:
Public Function MsgInfo(Optional msg As String = "Are You Ok?", _
Optional msgCaption As String = "Warning" ) As Boolean
MsgInfo = False
DoCmd.OpenForm "frmMsg"
Form_frmMsg.Caption = msgCaption ' Set Caption of Form
Form_frmMsg.lblMsg.Caption = msg ' Set Message of Form
MsgInfo = MsgInfoResult ' MsgInfoResult is Public Variable to store MsgInfo Result (Ok Bottom(True) or No Bottom(False) )
End Function
我在其他表格中使用了这个,例如删除客户列表中的客户(删除底部(:
Private Sub btnDelete_Click()
DoCmd.SetWarnings False
If MsgInfo("Are You Sure Delete Customer?", , "Delete Customer!") = True Then
' Run SQL for Delete Customer
Dim sqlDelete As String
sqlDelete = "DELETE tblCustomer.*, tblCustomer.RowId " & _
"FROM tblCustomer " & _
"WHERE tblCustomer.RowId=[Forms]![frmCustomerList]![frmCustomerListSub]![RowId]"
DoCmd.RunSQL sqlDelete
Form_frmCustomerList.frmCustomerListSub.Requery
End If
DoCmd.SetWarnings True
End Sub
我的问题是在打开MsgInfo窗体之后在用户回答此问题之前,将执行Next命令(Sql(。
为了解决这个问题,我在函数MsgInfo:中更改了AcWindowsMode
DoCmd.OpenForm "frmMsg"
至
DoCmd.OpenForm "frmMsg", , , , , acDialog
问题解决了,但还有另一个问题。以下命令不执行:
Form_frmMsg.Caption = msgCaption ' Set Caption of Form
Form_frmMsg.lblMsg.Caption = msg ' Set Message of Form
MsgInfo = MsgInfoResult ' MsgInfoResult is Public Variable
请帮帮我。
我不是VBA或任何编程语言的专家,也不是程序员。但是,作为一种爱好,我对处理编程语言有相当的兴趣。
在VBA中,在关闭窗体之前,在、、、acDialog之后的任何代码都无法运行,因此,您需要找到一种方法将消息传递到某个位置,并让窗体检索它。
创建一个模块,将消息从功能传递到表单
'a Module named Module_gVar
Public MessageHeader as String 'Optional A Separate Text Box For a Header
Public MessageBody as String 'Main Body
Public MessageTitle as String 'Caption of the Form
Public MessageReturn as Boolean
这是调用消息框并获得简单的True或False Return 的功能
'Function To Call the MessageBox
Public Function CallMessageBox ( _
Optional msgHeader as string , _
Optional msgBody as string , _
Optional msgTitle as string)
Module_gVar.MessageTitle = msgTitle
Module_gVar.MessageHeader = msgHeader
Module_gVar.MessageBody = msgBody
DoCmd.OpenForm "frmMessage",,,,,acDialog
CallMessageBox = Module_gVar.MessageReturn
'You can have the CleanUp on a Separate Function
'Since it's not a Procedure Variable it Isn't cleaned when it goes out of scope
Module_gVar.MessageTitle = ""
Module_gVar.MessageBody = ""
Module_gVar.MessageHeader = ""
Module_gVar.Return = False
End Function
现在是表单本身。
'Retrieve the Strings
Private Sub Form_Current()
Me.[YourHeaderTextBox] = Module_gVar.MessageHeader
Me.[YourBodyTextBox] = Module_gVar.MessageBody
Me.Caption = Module_gVar.MessageTitle
End Sub
'A Button to Return a Value
Private Sub cmdYes_Click() ' Yes button
Module_gVar.MessageReturn = True
DoCmd.Close acForm,"frmMessage",acSaveNo
End Sub
Private Sub cmdNo_Click() ' No Button
Module_gVar.MessageReturn = False
Docmd.Close acForm,"frmMessage",acSaveNo
End Sub
你可以从这里开始优化代码,但这是基本结构。我建议创建一个类模块,在其中可以检索字符串、输入字符串和调用Forms。