vba 访问VBA在代码中查找记录然后设置为表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25248035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:11:58  来源:igfitidea点击:

Access VBA find record in code and then set to form

vbams-access

提问by Benjamin E.

The following code produces this error runtime error '2455' you entered an expression that has an invalid reference to the property Form/Report.

以下代码产生此错误 runtime error '2455' you entered an expression that has an invalid reference to the property Form/Report.

The RecordSource behind the subforms is an actual table name.

子窗体后面的 RecordSource 是一个实际的表名。

When debugging, it stops on Set rs = ...or the next line.

调试时,停在Set rs = ...或下一行。

Private Sub Form_Current()
    Dim rs As Recordset

    Set rs = CurrentDb.OpenRecordset("SELECT * FROM " & Forms!frmTrafficAndLogistics!sfrTnLPO.Form.RecordSource)
    If Not (rs.EOF And rs.BOF) Then rs.MoveFirst

    rs.FindFirst "PONum = " & Me.PONum

    If Not rs.NoMatch Then
        Forms!frmTrafficAndLogistics!sfrTnLPO.Form.Bookmark = rs.Bookmark
    End If

    rs.Close
    Set rs = Nothing
End Sub

回答by PaulFrancis

You will only need to assign the Form's recordsource to the Recordset object, like

您只需要将表单的记录源分配给 Recordset 对象,例如

Private Sub Form_Current()
    Dim rs As Recordset

    Set rs = Forms!frmTrafficAndLogistics!sfrTnLPO.Form.RecordSetClone

    If Not (rs.EOF And rs.BOF) Then rs.MoveFirst

    rs.FindFirst "PONum = " & Me.PONum

    If Not rs.NoMatch Then
        Forms!frmTrafficAndLogistics!sfrTnLPO.Form.Bookmark = rs.Bookmark
    End If

    rs.Close
    Set rs = Nothing
End Sub