Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    banpreet is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2016
    Posts
    117

    Running Query that will return next highest number in table on form

    Hi guys,



    I have a form with one field from a table, an id field named TR_ID . I need to run a query that will have the format "TR-1" then"TR-2" and so on. I suck at queries, any idea how to achieve this.

    Thanks!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,523
    in query design, add the table
    pull down the field TR_ID
    run query

    if you need to specify TR , then in criteria put:

    TR-*

  3. #3
    banpreet is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2016
    Posts
    117
    How would I then implent this on the form? so every time a new record is created it automatically does it or do I need to make a button or jus run the query button on the form?

  4. #4
    banpreet is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2016
    Posts
    117
    sorry forgot to specify but I want it do retrieve the highest value from the table which I think is 54 then continue from there. I guess like the DMax function and then add one

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Or, if you just want to display the ID's with "TR-" as a prefix, use an Expression in the query:

    ID:"TR-" & [TR_ID]

    If you have more than 10, you might want to format with leading zeros, otherwise the sorting won't work if you use that calculated field to sort on.

    ID:"TR-" & Format([TR_ID],"000")

  6. #6
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    You're on the right track with the DMax -

    As the default value for the form field put =nz(Dmax("TR_ID","Tablename"),0) + 1

    Using the Nz means it will work even if there are no records yet, and putting that expression as the default means it will only set the value in new records.

    To display the ID with the TR- on the form, set the format to "TR-000"
    Last edited by John_G; 12-21-2016 at 02:00 PM. Reason: more information

  7. #7
    banpreet is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2016
    Posts
    117
    Where do I Enter this information? I need a step by step because im a noob sorry. Thanks for the reply!

  8. #8
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    You put this in the Properties list for the textbox containing ID on the form. There is a Default Value property and a Format property.


    Note: Setting the format does not affect how the data is stored in the table - it only affects how you see it.

  9. #9
    banpreet is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2016
    Posts
    117
    I put the code in and it went to a new record but the TR_ID was at 10, which is a duplicate, the highest record is 58 in the table so I would assume it would go to 59. Any help is appreciated thanks!

  10. #10
    banpreet is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2016
    Posts
    117
    It seems that the tables last record for the TR_ID was 9 instead of 58. I don't know why its out of order but I deleted the record, lets hope it works.

  11. #11
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,469
    If I read it right, you have a table with 1 numeric field where you want to increment that value to use in your ID for each new record? So basically you have to retrieve that value, use it in your TR-?? value, then increment it to use for the next record. Below might work for you. Put this in the OnCurrent on after you go to a new record. You need to change the names iof the field OrderIDMax and table dbo_tblOrderIDMax to yours.


    Dim vMax Variant
    vMax = DLookup("OrderIDMax", "dbo_tblOrderIDMax")
    me.TR_ID = "TR-" & vMax
    DoCmd.RunSQL "UPDATE dbo_tblOrderIDMax SET OrderIDMax = " & vMax + 1

  12. #12
    banpreet is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2016
    Posts
    117
    Sweet it works thanks, pretty simple I just cant get the format to work, I tried but how do I get "TR-" in the beginning? I went in the table and put "TR-000" as format but it does not show up. How do I get it to show up like this one the form field? Thanks!

  13. #13
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,469
    = "TR-" & Field

    Field is your incremental number wherever you are getting it from. The "&" sign concatenates values in Access so if your value was 55, doing the above would make it TR-55

  14. #14
    banpreet is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2016
    Posts
    117
    I put it the way u said and it puts quotes around & and Field automatically and it comes up on the form like that saying field and &, is there another way?

  15. #15
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,469
    Not sure who's method you went with or where you are trying to put this value, on a control/field or in a table? Maybe = "TR-" [YourMaxField]. Change YourMaxField to your own field that has that max value

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

Similar Threads

  1. Replies: 2
    Last Post: 05-21-2014, 06:16 AM
  2. Replies: 1
    Last Post: 02-13-2012, 04:58 PM
  3. Query to return only the row containing the highest date.
    By eric.opperman1@gmail.com in forum Queries
    Replies: 4
    Last Post: 03-22-2011, 08:42 PM
  4. Replies: 2
    Last Post: 08-09-2010, 08:13 AM
  5. Replies: 1
    Last Post: 02-05-2009, 04:53 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