Create SQL statement

  • Thread starter Thread starter Melinda
  • Start date Start date
M

Melinda

Hello,
Access 97 problem here. I'm trying to create an SQL
statement to pick out the most recent date/time for each
JobCode in my table and then delete all of the records in
my table that don't have that date/time. Here is my code:

Dim dbs As Database, qdf As QueryDef, strSQL As String
Set dbs = CurrentDb
strSQL = "SELECT * FROM audWorkOrder2 WHERE Max
(audWorkOrder2.audDate)"
Set qdf = dbs.CreateQueryDef("MostRecent", strSQL)
Delete * FROM audWorkOrder2 WHERE
audWorkOrder2.audDate<>MostRecent

The problem is, I'm not sure how to tell VB to group my
Max audDate by JobCode. Also, the Delete * gives me a
compile error. Any ideas?
Thanks!
Melinda
 
Give this a try
DoCmd.RunSQL "DELETE FROM audWorkOrder2 T1 WHERE audDate <
(SELECT Max(audDate) FROM audWorkOrder2 T2 WHERE T1.jobCode
= T2.jobCode)"

You will need to change jobCode if that is not the name of
the column in the table.

Hope This Helps
Gerald Stanley MCSD
 
Thank you! That worked perfectly! And it's much classier
than my code!
Melinda
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top