DAO 3.6 Library

Editing Records

Now all that's left to provide full functionality in our small library is the ability to change a field's value. To do this we'll use a Function called ChangeField. It'll accept four parameters. The recordset of the record that you wish to update, the id of the record, the name of the field that you wish to change and the value that you wish to change it to.

Like the DeleteRecord function, the ChangeField function returns the ID that you passed to it, if it's successful and -1 if it's not.

Here's the code:

Public Function ChangeField(rstTable As Recordset, intID As Integer, strFieldName As String, varValue As Variant)As Integer
   With rstTable
      .FindFirst "ID = " & intID
      If .NoMatch = True Then
         ChangeField = -1
         .Fields(strFieldName).Value = varValue
         ChangeField = intID
      End If
   End With
End Function

The code requeries the table as usually, moves to the record that we want to change and uses the .Fields structure to alter the field that you specified to include the value that you passed to it. The special .Update method ensures that the changes are recorded by Access. To use this function use the following code.. (This is assuming that we want to change field FirstName of record 4 of recordset rstPeople to "Louis"):

Dim intReturn as Integer
intReturn = ChangeField(rstPeople, 4, "FirstName", "Louis")

You might also like...



Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The generation of random numbers is too important to be left to chance.” - Robert R. Coveyou