Table of contents
XML
TOC
折叠目录
展开目录

修改 DAO 记录集中的现有记录Modify an Existing Record in a DAO Recordset

office 365 dev account|上次更新日期: 2017/9/25
|
1 参与者

您可以通过编辑更新方法修改表类型或动态集类型记录集对象中的现有记录。You can modify existing records in a table-type or dynaset-type Recordset object by using the Edit and Update methods.

若要修改表类型或动态集类型记录集对象中的现有记录:To modify an existing record in a table-type or dynaset-type Recordset object:

  1. 转到要更改的记录。Go to the record that you want to change.

  2. 使用Edit方法,对当前记录进行编辑前准备。Use the Edit method to prepare the current record for editing.

  3. 对该记录进行所需的更改。Make the necessary changes to the record.

  4. 使用Update方法保存对当前记录的更改。Use the Update method to save the changes to the current record.

下面的代码示例演示如何更改为一个名为雇员表中的所有销售代表的职务。The following code example shows how to change the job titles for all sales representatives in a table called Employees.

Dim dbsNorthwind As DAO.Database 
Dim rstEmployees As DAO.Recordset 

   Set dbsNorthwind = CurrentDb 
   Set rstEmployees = dbsNorthwind.OpenRecordset("Employees") 

   rstEmployees.MoveFirst 
   Do Until rstEmployees.EOF 
      If rstEmployees!Title = "Sales Representative" Then 
         rstEmployees.Edit 
         rstEmployees!Title = "Account Executive" 
         rstEmployees.Update 
      End If 
      rstEmployees.MoveNext 
   Loop 

请注意 如果您尝试更改当前记录中的值之前没有使用Edit方法,则发生运行时错误。如果编辑当前记录然后移至其他记录或关闭记录集对象而不是第一个使用Update方法,则更改将会丢失且不发出警告。例如,省略从前面的示例将导致对雇员表进行任何更改的更新方法。Note If you do not use the Edit method before you try to change a value in the current record, a run-time error occurs. If you edit the current record and then move to another record or close the Recordset object without first using the Update method, your changes are lost without warning. For example, omitting the Update method from the preceding example results in no changes being made to the Employees table.

不使用正在执行方法保存更改,也可以终止Edit方法,任何挂起的事务。尽管可以仅通过离开当前记录来终止Edit方法,这并不可行时的当前记录是记录集中第一个或最后一个记录或新记录时。它是通常使用的正在执行的方法更简单。You can also terminate the Edit method and any pending transactions without saving changes by using the CancelUpdate method. While you can terminate the Edit method just by moving off the current record, this is not practical when the current record is the first or last record in the Recordset, or when it is a new record. It is generally simpler to use the CancelUpdate method.

© 2018 Microsoft