"Paul T. Rong" <et***@hotmail.com> wrote
Who can tell me how to write vba, if I want
to open the last record of "order" form
where the customer name is Larry King,
in other words, records with the customer
name "Larry King" are many, but the last
record is wanted?
Create a Query -- you can do it in the Query Builder: use the table(s) of
interest as data sources, under the customer name specify the criteria of
"Larry King", sort DESC on the date, and in the query properties choose TOP
1. You can look at the SQL View to see the pattern for your SQL that you'll
need to build (I'm assuming you are going to pick up the actual customer
name from a form). Then, in the Click event of a Command Button, write VBA
(this assumes a query with the name qryA, in which you specified "Larry
King" as the name, assumes that the user selected name is in cboNam.
Dim strSQL as String
Dim db as DAO.Database
Dim qd as DAO.QueryDef
Dim rs as DAO.Recordset
Set db = CurrentDB
Set qd = db.QueryDefs("qryA")
strSQL = Replace(qd.SQL, "Larry King", Me.cboNam)
Set rs = db.OpenRecordset(strSQL)
If Not (rs.EOF And rs.BOF) Then
'the record was found, do whatever
Else
'the record was not found, do whatever other
End If
rs.Close
Set rs = Nothing
Set qd = Nothing
Set db = Nothing
Larry Linson
Microsoft Access MVP