One way to do this would be to use something like the following code. All the
following code is UNTESTED and may contain errors.
Public Sub fUpdateTable()
Dim dbAny as DAO.Database
Dim strSQL as String
On Error GoTo Proc_Error
strSQL = "UPDATE Table1 " & _
" SET Table1.Num2 = 222" & _
" WHERE Table1.Num1=2"
Set dbAny = Currentdb()
dbAny.Execute strSQL,dbFailOnError
'Optional lines follow
MsgBox "Updated " & dbAny.RecordsAffected & " records"
Exit Sub
Proc_Error:
MsgBox Err.Number & " : " & Err.Description,,"fUpdateTable Error"
End Sub
If you wanted to step through the records one at a time and edit and save each
record, you would need to build a recordset and do that. That code would look
something like the following.
Public Sub fUpdateTable()
Dim rs As DAO.Recordset
Dim strSQL As String
strSQL = "SELECT Table1.Num1, Table1.Num2" & _
" FROM Table1 " & _
" WHERE Table1.Num1=2"
Set dbAny = CurrentDb()
Set rs = dbAny.OpenRecordset(strSQL)
If rs.RecordCount > 0 Then
With rs
While Not .EOF
.Edit
.Fields("Num1") = 22
.Update
.MoveNext
Wend
End With
End If
End Sub
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County