首页 > 解决方案 > 有没有办法在打开时更改 Ms Access 对话框窗体的属性?

问题描述

我想通过表单“frmMsg”创建我的 msgBox,而不是使用 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 表单之后在用户回答这个问题之前,执行下一个命令 (Sql)。

为了解决这个问题,我在 Function MsgInfo 中更改了 AcWindowsMo​​de:

    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 

请帮我。

标签: vbams-accessmodal-dialogms-access-forms

解决方案


我不是 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 返回的函数

'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

您可以从这里开始优化代码,但这是基本结构。我建议创建一个类模块,您可以在其中检索字符串、输入字符串和调用表单。


推荐阅读