【问题标题】:Docmd.Close not closing the access formDocmd.Close 不关闭访问表单
【发布时间】:2015-10-13 06:46:37
【问题描述】:

我在访问 vb 时遇到问题。有一个主窗体(比如 parentForm),它有两个按钮来触发两个不同的窗体(比如 childForm1 和 childForm2)。两个子窗体都对其 Load 事件执行检查。如果条件失败,则 childForm 必须关闭。问题是在 childForm1 中,代码可以正常工作,而在 childForm2 中则完全出错了。

似乎关闭事件完全被忽略了。在onLoad事件之后,处理到onCurrent事件,这不应该发生!下面是childForm2的onLoad事件代码。

Private Sub Form_Load()
On Error Resume Next

Dim db As Database
Dim rst As Recordset
Dim stDocName As String

stDocName = "childForm2"
closeEvent = False
Set db = CurrentDb

 If a<> 0 And b<> 0 Then
    Set rst = db.OpenRecordset("SELECT * FROM tbl1 WHERE Cust Like '*" & a & "*' AND Cust2 Like '*" & b & "*';")
    If (rst.EOF Or rst.BOF) And IsLoaded(stDocName) Then
       MsgBox ("No record found!")
       rst.Close
       SetWarnings = True
       closeEvent = True
       Me.SetFocus
       DoCmd.Close acForm, stDocName, acSaveNo
    End If
ElseIf a = 0 And b <> 0 Then
    Set rst = db.OpenRecordset("SELECT * FROM tbl1 WHERE Cust2 Like '*" & b & "*';")
    If (rst.EOF Or rst.BOF) Then
       MsgBox ("No record found!")
       rst.Close
       DoCmd.Close acForm, stDocName
    End If
End If
db.Close
End Sub

另外,我尝试使用一个全局布尔变量(代码中的 closeEvent),当窗体必须关闭时,该变量初始化为 False 并变为 True。在 onCurrent 事件中检查此变量以关闭表单。但是,当我调试代码时,从 onLoad 传递到 OnCurrent 事件时,该变量似乎丢失了它的 (True) 值!

任何建议都非常感谢。

提前致谢, 玛丽亚

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    使用Form_Open 事件而不是Form_Load事件。

    然后,不要关闭表单 (=Docmd.Close),而是使用内置的 Cancel 参数来取消表单的打开。

    Private Sub Form_Open(Cancel As Integer)
    
       If **condition not met** then
          Cancel = True  'this stops the form from opening
       End If
    
    End Sub 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      相关资源
      最近更新 更多