There is a number of ways to do this.
The first way is to use sql. In code, this would look like:
dim strSql as string
strSql = "update [customer list] set FirstName = 'Joe' where " & _
"LastName = 'Smith' and FirstName = 'John'"
currentdb.Execute strSql
The above will do as you ask. So, often, data changes are thus made using
sql. Combing your knowledge of sql and code can results is very powerful
data management.
Of course, we could also use a old fashioned approach like in the early days
of computing, where we process record by record (we tend NOT to do this
anymore, since using sql can be sent across the country and EXECUTED on the
target machine. Thus, sql is often preferred since it lets us use a client
to server type setup (that sql statement can be executed BY the server if
you are using a database engine like sql-server)..
However, lets write the code to do the above in the old code way. Here is a
code loop that would do the above:
dim rstRecords as dao.RecordSet
set rstRecords = currentdb.OpenRecordSet("[customer list]")
do while rstRecords.Eof = false
' process records while end of file = false
if rstRecords!FirstName = "John" and rstRecords!LastName = "Smith" then
rstRecords.Edit
rstrecords!FirstName = "Joe"
rstRecords.Update
end if
rstRecords.MoveNext
loop
rstRecords.Close
set rstRecords = nothing.
So, the above the does the same thing as our first code..but as you can see,
the first example is far easier to write.
Of course, we can combine both sql, and data processing together. The above
code loop is wasteful, since it has to test ALL records in the table for a
match. A better approach would be to query ONLY the John Smith. Thus, we
should, and could re-write the above as:
dim rstRecords as dao.RecordSet
dim strSql as string
strSql = "select FirstName, LastName from [Customer List] where " & _
"FirstName = 'John" and LastName = 'Smith'"
set rstRecords = currentdb.OpenRecordSet(strSql)
do while rstRecords.Eof = false
' process records while end of file = false
rstRecords.Edit
rstrecords!FirstName = "Joe"
rstRecords.Update
end if
rstRecords.MoveNext
loop
rstRecords.Close
set rstRecords = nothing.
Once again, you can see that using sql generally saves us coding...