Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    bftsg is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    11

    Creating a new Max() value for a new record

    I am trying to create a new maximum number for a new record I am creating. It consists of three numbers plus the unique new identifier.
    The first number can be one of 15 values, as can the second and third numbers. Once the user has selected the first three options, I need to create a new unique fourth number to prevent duplicates.
    The code I use is working in SQL
    SELECT max(a.Req_No)
    FROM tbl_Requirements a
    where a.Req_Type = forms!frm_Requirements!Req_Type and
    a.Req_Area = forms!frm_Requirements!Req_Area and
    a.Req_SubArea = forms!frm_Requirements!Req_SubArea;

    How can I call that from a form using the "After Update" on the third number? My brain hurts...

    I have set it up as a query from the form but does not show the new value in the field box on the form. DoCmd RunSQL cannot be used.





  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,235
    Wouldn't a dMax work (as the control source of the text box of your frm_Requirements)?

    Dmax("[a.Req_No]","[tbl_Requirements]", "[a.Req_Type] = " & [Req_Type] & " And [a.Req_Area] = " & [Req_Area] & " And [a.Req_SubArea] = " & [Req_SubArea])

    The above assumes Reg_Type,Reg_Area and Reg_Subarea are all numbers. You might need to wrap it in some IIF statements to check if the three fields are populated:
    =IIf(Isnull(
    [Req_Type]),Null,IIf(isnull([Req_Area]),Null,IIf(Isnull([Req_SubArea]),Null,Dmax("[a.Req_No]","[tbl_Requirements]", "[a.Req_Type] = " & [Req_Type] & " And [a.Req_Area] = " & [Req_Area] & " And [a.Req_SubArea] = " & [Req_SubArea]))))
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    bftsg is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    11
    Thanks Vlad - much appreciated. I have tried lots and lots of ways to try and get it to work but without success, mostly down to my misunderstand the syntax of the cDMax command. The latest non-working version is

    Req_No = DMax(["Req_No"], "tbl_Requirements",( "Req_Type = " & Forms!frm_Requirements!Req_Type & " AND 'Req_Area' = " & Forms!frm_Requirements!Req_Area & "AND 'Req_SubArea = '" & Forms!frm_Requirements!Req_SubArea))



    I get an error "Run Time Error 2465 Database can't find the field '|1' referred to in your expression."

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    bftsg,
    Can you tell us a little about the environment? What do Req_Type, Req_Area and Req_SubArea represent?
    May also be helpful if you show readers your tables and relationships.

    Since you're dealing with numbers, can you show us some representative data?

  5. #5
    bftsg is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    11
    The fields are all in one table called Requirements.
    Within the table there is a field calld Area, one called SubArea and one called Type.
    Each field holds an id_
    number from a
    look-up table holding text descriptions

    The user picks an Area, a SubArea and a Type to create a requirement and the system is supposed to create a unique number, one higher than the last requirement for that set of numbers.

    Running an SQL query I can do it easily, but I cannot get the DMax process to work. I tried running the DoCmd.RunSQL to create it but that didn't work either.

    I understand that it is my lack of knowledge of setting the three criteria with DMax that is stopping me but I cannot find any clear instructions on the syntax anywhere. All help is very appreciated.

  6. #6
    bftsg is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    11
    All the id_numbers are two digit but the req_no cqan be 5 digits long.

  7. #7
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,706
    If the DMax code is on form frmRequirements try this:

    Code:
    Dim nNextReg as long
    
    
    nNextReg = DMax("Req_No", "tbl_Requirements", "Req_Type = " & Me.Req_Type & " AND Req_Area = " & Me.Req_Area & " AND Req_SubArea = " & Me.Req_SubArea) + 1

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    I agree with davegri that his code sample will get you a new unique number. However, I'm still curious what the application is about.

  9. #9
    bftsg is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    11
    Thanks Guys - I will give it a try and let you know.

    The app is for sme businesses going through a new or changed application or system process. Allows them to identify what they need to prove before they can build, test and accept that they are getting what they asked for. Makes it easier for both users, developers and testers during the whole SDLC.

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Sounds like a good idea. My only concern is the single table.

  11. #11
    bftsg is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    11
    Hi Orange. What is your concern about a single table? There are a number of tables which are linked by primary keys. The sub-tables contain an auo-number id and a text field. The main table links to these tables using the id key, as normal.

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850

  13. #13
    bftsg is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    11
    Ok - now I see. Thanks for your help

  14. #14
    bftsg is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    11

    Oh flipping heck!

    Apologies for my lack of understanding of the DMax command but the statement below is not returning anything for nNextReq. Any ideas would be appreciated

    There was nothing wrong with the command - apart from a stupid spelling mistake on the name! Only spotted using the larger text in this form. Specsavers here I come...

  15. #15
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Suggest you show us some real data values for the fields in this
    Dmax("Req_No", "tbl_Requirements", "Req_Type = " & Me.Req_Type & " AND Req_Area = " & Me.Req_Area & " AND Req_SubArea = " & Me.Req_SubArea)

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

Similar Threads

  1. Replies: 1
    Last Post: 04-23-2019, 12:44 PM
  2. Replies: 2
    Last Post: 08-10-2018, 04:28 PM
  3. Replies: 5
    Last Post: 08-24-2017, 09:03 PM
  4. Replies: 1
    Last Post: 04-24-2012, 02:36 PM
  5. Creating unique record from record and field data
    By arthurpenske in forum Access
    Replies: 3
    Last Post: 08-24-2011, 06:11 PM

Tags for this Thread

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