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 & "',"
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,793
Office Version
  1. 2019
Platform
  1. Windows
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).
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
Where exactly do you want to use the max of the ID in the query?
 

Littlemalky

Board Regular
Joined
Jan 14, 2011
Messages
223
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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
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?
 

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,793
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

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:

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
xenou

Will DMax work if the OP was using something like ADO (or DAO) to run the query?
 

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,793
Office Version
  1. 2019
Platform
  1. Windows
Good question. Probably not, unless using DoCmd.RunSql()
 

Watch MrExcel Video

Forum statistics

Threads
1,122,802
Messages
5,598,122
Members
414,214
Latest member
marketingnumbersguy

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
Top