1

有没有办法通过 ByRef 函数更新记录集中的字段?我觉得这个例子应该可以工作,但是 tbl![Field1] 不会被 UpdateTable 子更新。可以这样做,还是我必须先将字段传递给变量?

Private Sub DeclareTable()
    Dim tbl As Recordset
    Set tbl = CurrentDb.OpenRecordset("Table1")
    tbl.MoveFirst
    
    tbl.Edit
    UpdateTable tbl![field1], 5, 2
    tbl.Update
    
End Sub
Private Sub UpdateTable(ByRef tblField, X, Y)
    tblField = X * Y
End Sub
4

1 回答 1

2

您可能需要更具体地使用 DAO:

Private Sub DeclareTable()

    Dim tbl     As DAO.Recordset
    
    Set tbl = CurrentDb.OpenRecordset("Table1")
    tbl.MoveFirst
    
    tbl.Edit
    UpdateTable tbl![Field1], 5, 2
    tbl.Update
    
    tbl.Close
    
End Sub


Private Sub UpdateTable( _
    ByVal tblField As DAO.Field, _
    ByVal X As Long, _
    ByVal Y As Long)

    tblField.Value = X * Y

End Sub

这在这里运行得很好。

于 2021-10-29T10:53:01.417 回答