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
.Requery
.FindFirst "ID = " & intID
If .NoMatch = True Then
ChangeField = -1
Else
.Edit
.Fields(strFieldName).Value = varValue
.Update
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")
Comments