Updating Records

The code for updating records in a recordset is almost the same as adding them, including the need to specify the dbOpenDynaset Type. You may also need to find the correct record to update using FindFirst. If you find it successfully, you can update it.

Set rec = db.OpenRecordset("tblMyTable", dbOpenDynaset) With rec

.FindFirst "CustomerKey = " & Me!CustomerKey If Not .NoMatch Then 'we found the record .Edit

!CustomerName = "ABC Construction" !CustomerStatus = 1 .Update End If End With

The With statement is purely a programming convenience. Instead of typing the name of the object every single time, you can use With <objectname>. After that, and until you use End With, any references with no object name, just a dot (.) or bang (!), will be assumed to belong to the With object. You may want to improve the clarity of your code by not using it when you are trying to keep track of multiple recordsets, as in the next example.

0 0

Post a comment