gpt4 book ai didi

forms - 从另一个表单打开一个表单并通过命令单击关闭原始表单

转载 作者:行者123 更新时间:2023-12-02 04:23:14 25 4
gpt4 key购买 nike

我创建了一个登录表单,单击“打开数据库”按钮时会打开另一个表单。我试图在单击按钮打开另一个表单时关闭原始表单“UserInformation”。我正在尝试各种 VBA 代码,但似乎没有任何效果。我通过向导创建了按钮,然后将宏转换为 vba 代码。

Private Sub cmdOpenDatabase_Click()

If IsNull(Me.txtFirstName) Then
MsgBox ("Please enter First Name")
Me.txtFirstName.SetFocus
Cancel = True
Exit Sub
End If

If IsNull(Me.txtLastName) Then
MsgBox ("Please enter Last Name")
Me.txtLastName.SetFocus
Cancel = True
Exit Sub
End If

If IsNull(Me.txtEmployee) Then
MsgBox ("Please enter Employee ID")
Me.txtEmployee.SetFocus
Cancel = True
Exit Sub
End If

On Error GoTo cmdOpenDatabase_Click_Err

DoCmd.OpenForm "Standards", acNormal, "", "", , acNormal

cmdOpenDatabase_Click_Exit:
Exit Sub

cmdOpenDatabase_Click_Err:
MsgBox Error$
Resume cmdOpenDatabase_Click_Exit

End Sub

最佳答案

在调用 DoCmd.OpenForm 打开另一个表单后,为当前表单 (Me.Name) 调用 DoCmd.Close。注意 acNormal 不是 OpenForm WindowMode 选项支持的值;我猜你想要的是 acWindowNormal

'DoCmd.OpenForm "Standards", acNormal, "", "", , acNormal
DoCmd.OpenForm "Standards", acNormal, , , , acWindowNormal
DoCmd.Close acForm, Me.Name

还要考虑包含选项名称是否会使您的代码更易于理解:

DoCmd.OpenForm FormName:="Standards", View:=acNormal, WindowMode:=acWindowNormal
DoCmd.Close ObjectType:=acForm, ObjectName:=Me.Name

最后,Cancel = True 不会在命令按钮单击事件过程中完成任何操作。事实上,在表单模块的声明部分中使用 Option Explicit 时,Cancel 将在那里触发编译错误(“变量未定义”)。作为标准做法,您应该在所有代码模块中包含 Option Explicit

这是您的程序的替代版本...

Private Sub cmdOpenDatabase_Click()
Dim blnMissingData As Boolean

If IsNull(Me.txtFirstName) Then
MsgBox "Please enter First Name"
Me.txtFirstName.SetFocus
blnMissingData = True
End If

If IsNull(Me.txtLastName) Then
MsgBox "Please enter Last Name"
Me.txtLastName.SetFocus
blnMissingData = True
End If

If IsNull(Me.txtEmployee) Then
MsgBox "Please enter Employee ID"
Me.txtEmployee.SetFocus
blnMissingData = True
End If

On Error GoTo cmdOpenDatabase_Click_Err

If Not blnMissingData Then
DoCmd.OpenForm FormName:="Standards", View:=acNormal, WindowMode:=acWindowNormal
DoCmd.Close ObjectType:=acForm, ObjectName:=Me.Name
End If

cmdOpenDatabase_Click_Exit:
Exit Sub

cmdOpenDatabase_Click_Err:
MsgBox Error$
Resume cmdOpenDatabase_Click_Exit
End Sub

关于forms - 从另一个表单打开一个表单并通过命令单击关闭原始表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28752279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com