而不是使用msgBox,我想通过表单“ frmMsg”创建我的msgBox。“ frmMsg”表单具有两个拖尾(“确定”和“否”),以及用于显示消息的标签(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"
to
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或任何编程语言的专家,我也不是Profession的程序员。但是,我在处理编程语言方面有很多共同的爱好。
在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返回的功能
'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
您可以从此处开始优化代码,但这是基本结构。我建议创建一个类模块,您可以在其中检索字符串,输入字符串并调用表单。