Me.OpenArgs 尝试在访问中打开新表单时总是返回空值
Me.OpenArgs is always returning null when trying to open a new form in access
好的,所以我在 Access 中有一个数据库,当我打开一个表单(用于工作申请)并单击一个按钮时,它会运行以下代码:
Private Sub cmdAddDemo_Click()
DoCmd.OpenForm "sfrmAppDemographics", , , , , , txtApplID.Value
End Sub
其中 txtApplID 是一个文本框,其中填充了应用程序 table 中 "application ID" 编号的数值。我希望将此值传递给 "sfrmAppDemographics" 表单,以便为应用程序的前面提到的表单中显示的用户打开正确的人口统计信息。
所以,我在人口统计表格的代码中这样做了:
Private Sub Form_Open(Cancel As Integer)
Dim rs As DAO.Recordset
Set rs = Me.Recordset
If rs.RecordCount = 0 Then
rs.AddNew
rs!ApplID = Me.OpenArgs
rs.Update
Else
MsgBox Me.OpenArgs
End If
Me.Requery
结束子
所以,这个想法是,如果没有人口统计信息,它将使用来自传递的 openargs 的 ApplID 创建一个新的,如果有该用户的人口统计数据,它会弹出一个带有 openargs 的消息框(作为测试看它是否工作)。我总是在 MsgBox 的行上收到错误 "Run-time error '94': Invalid use of Null"。 (因为数据库确实有人口统计记录)。当我取出 Else MsgBox Me.OpenArgs 时,它只显示数据库中的第一个人口统计信息,ApplID 为 1。奇怪。看来我无法通过 Form_Open 代码中的 OpenArgs 功能传递或访问 ApplID。有帮助吗?
OpenArgs
是传递给表单的字符串,仅此而已。如果要过滤被调用的表单,使用WhereCondition
参数:
Private Sub cmdAddDemo_Click()
DoCmd.OpenForm "sfrmAppDemographics", WhereCondition:="ApplID = " & txtApplID.Value
End Sub
如果记录不存在而要创建记录,将 ID 作为 OpenArgs 额外传递是有意义的:
DoCmd.OpenForm "sfrmAppDemographics", WhereCondition:="ApplID = " & txtApplID.Value, _
OpenArgs:=txtApplID.Value
然后
Private Sub Form_Open(Cancel As Integer)
' No need for a separate recordset here
If Me.RecordsetClone.EOF Then
' We are already in a new record, just set the bound ID control to create it
Me!ApplID = Me.OpenArgs
' if you want to save the new record immediately
Me.Dirty = False
End If
End Sub
P.S。如果 Me.OpenArgs
在您的代码中为 Null,则 txtApplID
为 Null。我看不出其他原因。
只需将 DoCmd.GoToRecord , , acNewRec 添加到 Andre 的代码中,它就可以顺利运行...
Private Sub Form_Open(Cancel As Integer)
If Me.RecordsetClone.EOF Then
DoCmd.GoToRecord , , acNewRec
Me!ApplID.Value = Me.OpenArgs
Me.Dirty = False
End If
End Sub
好的,所以我在 Access 中有一个数据库,当我打开一个表单(用于工作申请)并单击一个按钮时,它会运行以下代码:
Private Sub cmdAddDemo_Click()
DoCmd.OpenForm "sfrmAppDemographics", , , , , , txtApplID.Value
End Sub
其中 txtApplID 是一个文本框,其中填充了应用程序 table 中 "application ID" 编号的数值。我希望将此值传递给 "sfrmAppDemographics" 表单,以便为应用程序的前面提到的表单中显示的用户打开正确的人口统计信息。
所以,我在人口统计表格的代码中这样做了:
Private Sub Form_Open(Cancel As Integer)
Dim rs As DAO.Recordset
Set rs = Me.Recordset
If rs.RecordCount = 0 Then
rs.AddNew
rs!ApplID = Me.OpenArgs
rs.Update
Else
MsgBox Me.OpenArgs
End If
Me.Requery
结束子
所以,这个想法是,如果没有人口统计信息,它将使用来自传递的 openargs 的 ApplID 创建一个新的,如果有该用户的人口统计数据,它会弹出一个带有 openargs 的消息框(作为测试看它是否工作)。我总是在 MsgBox 的行上收到错误 "Run-time error '94': Invalid use of Null"。 (因为数据库确实有人口统计记录)。当我取出 Else MsgBox Me.OpenArgs 时,它只显示数据库中的第一个人口统计信息,ApplID 为 1。奇怪。看来我无法通过 Form_Open 代码中的 OpenArgs 功能传递或访问 ApplID。有帮助吗?
OpenArgs
是传递给表单的字符串,仅此而已。如果要过滤被调用的表单,使用WhereCondition
参数:
Private Sub cmdAddDemo_Click()
DoCmd.OpenForm "sfrmAppDemographics", WhereCondition:="ApplID = " & txtApplID.Value
End Sub
如果记录不存在而要创建记录,将 ID 作为 OpenArgs 额外传递是有意义的:
DoCmd.OpenForm "sfrmAppDemographics", WhereCondition:="ApplID = " & txtApplID.Value, _
OpenArgs:=txtApplID.Value
然后
Private Sub Form_Open(Cancel As Integer)
' No need for a separate recordset here
If Me.RecordsetClone.EOF Then
' We are already in a new record, just set the bound ID control to create it
Me!ApplID = Me.OpenArgs
' if you want to save the new record immediately
Me.Dirty = False
End If
End Sub
P.S。如果 Me.OpenArgs
在您的代码中为 Null,则 txtApplID
为 Null。我看不出其他原因。
只需将 DoCmd.GoToRecord , , acNewRec 添加到 Andre 的代码中,它就可以顺利运行...
Private Sub Form_Open(Cancel As Integer)
If Me.RecordsetClone.EOF Then
DoCmd.GoToRecord , , acNewRec
Me!ApplID.Value = Me.OpenArgs
Me.Dirty = False
End If
End Sub