The ms-access environment does include a full VB development environment.
Actually, the programming language is called VBA (Visual Basic For
Applications), but for all purposes, it is the same programming language
used in VB.
So, can you manipulate records with code? Why of course you can, and likely
any application that really does anything of value will most certainly has
to "process" data.
The basic processing loop for a table names tblCustomers is as follows:
Dim rstRecords As DAO.Recordset
Set rstRecords = CurrentDb.OpenRecordset("tblCustomers")
Do While rstRecords.EOF = False
Debug.Print "last name is " & rstRecords!LastName
rstRecords.MoveNext
Loop
rstRecords.Close
Set rstRecords = Nothing
So, the above piece of code is the basic means to traverse a set of records
in a table. The above would display each last name in the debug window. Not
much use the above is, but it does show the basic processing loop in VB. Of
course, you can use sql in place of the table name to restrict record. So,
to change all the City Names from NY to New York, we could use:
Dim rstRecords As DAO.Recordset
Set rstRecords = CurrentDb.OpenRecordset("tblCustomers")
Do While rstRecords.EOF = False
if rstRecords!City = "NY" then
rstRecords.Edit
rstRecords!City = "New York"
rstRecords.Update
end if
rstRecords.MoveNext
Loop
rstRecords.Close
Set rstRecords = Nothing
Of course, since we can use sql to restrict what records will be in the
record set, then we can actually REMOVE the "if statement" and use the
following:
Dim rstRecords As DAO.Recordset
Set rstRecords = CurrentDb.OpenRecordset("select City from tblCustomers "
& _
" where City = 'NY'")
Do While rstRecords.EOF = False
rstRecords.Edit
rstRecords!City = "New York"
rstRecords.Update
rstRecords.MoveNext
Loop
rstRecords.Close
Set rstRecords = Nothing
Since all records will be city = 'NY', then we don't need to test in the
code if the city is = to 'NY'. Hence, using (and learning!) sql allows use
to save a lot of coding.
Of course, the above whole processing loop in VB is a real waste, and not
the best example since we have what is called sql (sql = structured query
language). Sql is really the key to un-locking the power of a database
system. We can replace all of the above code with ONE line of sql. It is
very power full. So, the VB code using sql could be:
currentdb.Execute "update tblCustomers set City = 'New York' Where City =
'NY'"
That is it!...one line of code!. So, when possible, you will most certainly
use code to process data, and actually make your application "do things".
However, when possible, you do want to use sql since is it less code,
generally runs faster, and is easer to maintain then complex processing VB
code.
You should get your self a book or two if you going to jump into this stuff.
Here is a good one to get your started, and it is free:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/bapp2000/ht
ml/acbatitle.asp
and
http://www.microsoft.com/accessdev/articles/bapp97/toc.htm