VBA SQL Max Function

Littlemalky

Board Regular
Joined
Jan 14, 2011
Messages
223
Hi, I'm trying to write an update query in which it filters for a specific employee ID, but also takes the Max of the primary key field. I'm not sure how to do this because it doesn't seem like a WHERE clause. I think I need to do a GROUP BY, but I'm not sure how to write it exactly in an UPDATE query?

I'm updating table "[tbl_TERRITORY_EMPLOYEE_MAP]", setting the [TERRITORY_START_DATE] field equal to a field on my form; inserting a MODIFIED BY name and a MODIFIED DATE stamp where the [EMPLOYEE_ID] field equals a value on my form. The problem is that the [EMPLOYEE_ID] field can have duplicates so to limit it further, I'd like to take the Max of the [ID] field, which is my primary key in the table.

Here is my current statement:
Code:
strSQL_TSDC = "UPDATE [tbl_TERRITORY_EMPLOYEE_MAP] SET [TERRITORY_START_DATE] = #" & Forms![frm_SALES_EMPLOYEE_MANAGEMENT]!txtTERRITORY_START_DATE_CHANGE & "#" & _
",[MODIFIED_BY] = '" & UserName() & "', [MODIFIED_DATE] = #" & Now() & "# " & _
"WHERE [EMPLOYEE_ID] = '" & Forms![frm_SALES_EMPLOYEE_MANAGEMENT]!cmbEMPLOYEE_ID & "',"
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You could probably use a DMax() function in the where clause. The "where" part of the DMax function will have the same employee id you are talking about (Max id where employee id = blah blah blah).
 
Upvote 0
Where exactly do you want to use the max of the ID in the query?
 
Upvote 0
I'm not sure how or where to put it. I'd just like to narrow this down to isolating one row, but taking the Max of [ID] where the [EMPLOYEE_ID] equals the value on the form.
 
Upvote 0
Are you trying to find the latest record in the table for the [EMPLOYEE_ID] that's been entered on the form?

For example, if there are 5 records with [EMPLOYEE_ID] equal to 57 and they have the IDs 3, 12, 45, 67 and 69 you would want to return, and update, the record with the ID equal to 69?
 
Upvote 0
Guessing but something like this should be added to the where clause:
Code:
 AND [ID] = DMax("[ID]", "[tbl_TERRITORY_EMPLOYEE_MAP]", "[EMPLOYEE_ID] = '" & Forms![frm_SALES_EMPLOYEE_MANAGEMENT]!cmbEMPLOYEE_ID & "'")
 
Last edited:
Upvote 0
xenou

Will DMax work if the OP was using something like ADO (or DAO) to run the query?
 
Upvote 0
Good question. Probably not, unless using DoCmd.RunSql()
 
Upvote 0

Forum statistics

Threads
1,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top