Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93

    Assigning the result of a Max() Function to a variable

    Given: A table with a unique Integer field [ApplID]

    From an unbound form, I want to determine the highest number in [ApplID], increment that value by 1 and assign it to a variable "NextApplID".



    Pseudocode: NextAppID = ("SELECT MAX(ApplID) FROM tblApplicant") + 1

    How can i do that? It's Friday, I'm foggier than usual

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Can't use SQL statement like that.
    Use DMax() domain aggregate function.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Don't forget to add code to increment the value in your table also.

  4. #4
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    to the question, maybe this?

    NextApplID = DMax("ApplID","tblApplicant") + 1
    https://learn.microsoft.com/en-us/of...plication.dmax
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    I do hope your app is not multi-user - two users determining NextAppID at the same time will get the same result.

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Quote Originally Posted by CJ_London View Post
    I do hope your app is not multi-user - two users determining NextAppID at the same time will get the same result.
    Agreed. That's why when I had to do this sort of thing the value got created when the form was saved (as long as user does not really need to know what it would be up front). If user does need to know but bails on the process at the end, then that number could be created but flagged somehow; e.g. archived - as long as consecutive values are not required. Often they are deemed to be required, but really aren't. Gaps in numbers play mind games with some people.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Join Date
    Apr 2017
    Posts
    1,792
    Quote Originally Posted by pledbetter View Post
    Pseudocode: NextAppID = ("SELECT MAX(ApplID) FROM tblApplicant") + 1
    Try
    Code:
    NextAppID = (SELECT TOP 1 ApplID FROM tblApplicant ORDER BY ApplID DESC)+1

  8. #8
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Quote Originally Posted by CJ_London View Post
    I do hope your app is not multi-user - two users determining NextAppID at the same time will get the same result.
    Only one user will be using this at a time.

  9. #9
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Quote Originally Posted by ArviLaanemets View Post
    Try
    Code:
    NextAppID = (SELECT TOP 1 ApplID FROM tblApplicant ORDER BY ApplID DESC)+1

    "Type Mismatch". I changed the variable type from Integer to String, and same error.

  10. #10
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    The computed number will eventually be the very number presented in a future INSERT INTO action.

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by ArviLaanemets View Post
    Try
    Code:
    NextAppID = (SELECT TOP 1 ApplID FROM tblApplicant ORDER BY ApplID DESC)+1
    Can you really use SQL like that?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by pledbetter View Post
    The computed number will eventually be the very number presented in a future INSERT INTO action.
    Not using bound form then?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Quote Originally Posted by Micron View Post
    to the question, maybe this?

    NextApplID = DMax("ApplID","tblApplicant") + 1
    https://learn.microsoft.com/en-us/of...plication.dmax
    That's what I get for not reading the Syntax rules carefully. I didn't think the Criteria switch was optional. Thanks!

  14. #14
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Quote Originally Posted by pledbetter View Post
    That's what I get for not reading the Syntax rules carefully. I didn't think the Criteria switch was optional. Thanks!
    I take it that solved your issue?

    Maybe you were looking at the wrong documentation for the application. AFAIK, it is required for Excel. Got my feet wet with Access vba. When I see how different common things are between these apps I wonder if the Excel guys were invited to the same meetings. That comment doesn't necessarily pertain to DMax, just in general.

    If you have a link where it says it is required for Access vba (not just someone's misinformed post), I'd like to check it out if you'd care to post it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  15. #15
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    Only one user will be using this at a time.
    I have a client who tells me the same thing. So far they have messed the data up three times as they 'forgot'

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query not giving required result using max function
    By mithinxavier in forum Queries
    Replies: 6
    Last Post: 01-15-2016, 09:04 AM
  2. Assigning a variable
    By tguitard in forum Programming
    Replies: 1
    Last Post: 05-02-2013, 12:22 PM
  3. Replies: 2
    Last Post: 03-15-2013, 12:49 PM
  4. using ADO, assigning a date variable
    By crowegreg in forum Programming
    Replies: 3
    Last Post: 08-23-2011, 12:00 AM
  5. assigning a variable
    By crowegreg in forum Programming
    Replies: 1
    Last Post: 08-22-2011, 05:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums