Results 1 to 5 of 5
  1. #1
    jaryszek is offline Expert
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2016
    Posts
    568

    Add incremental number in sql for specific values

    hi guys,

    i have table like here:

    ID Name Function
    1 Topo1 data
    2 Topo1 data
    3 Topo1 data
    4 Topo1 Diff
    5 Topo2 Some1




    and what i want to get for specific Name,in this example for Name = "Topo1" and Function = "data" i result like here in Query:
    ID Name Function IncrementalNumber
    1 Topo1 data 1
    2 Topo1 data 2
    3 Topo1 data 3
    4 Topo1 Diff 1
    5 Topo2 Some1 1

    so i added IncrementalNumnber column and thinking how to do it in Access SQL ?
    Maybe Dcount?
    (and if dcount will work in Excel adodb recordset after?)

    Best,
    Jacek
    Attached Files Attached Files

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,429
    this is one way to do it

    Code:
    SELECT TopoList.ID, TopoList.Name, TopoList.Function, DCount("*","topolist","ID<=" & [ID] & " AND [Function]='" & [function] & "' AND [Name]='" & [Name] & "'") AS Expr1
    FROM TopoList
    ORDER BY TopoList.ID;
    Note that Name and Function are both reserved words and should not be used as field names

    don't know about ADO, you will have to try it

  3. #3
    jaryszek is offline Expert
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2016
    Posts
    568
    hi,

    thank you.
    can you please explain how this is working? Especially criteria for DcOunt?

    Best,
    Jacek

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,429
    with your example data dcount would still work if the name was excluded from the criteria, but you said 'this example for Name = "Topo1" and Function = "data" '
    which is what you see in the criteria

    [Function]='" & [function] & "' AND [Name]='" & [Name] & "'"

    Then you only want to count the ID's that are the same or less than the ID for each record so we have "ID<=" & [ID]
    google dcount to find out more

    If topo1....data appears in a later record (e.g. ID 10), it will have a count of 4.

    If your real data does not match your example, then you will have a problem and would no doubt need a completely different solution

  5. #5
    jaryszek is offline Expert
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2016
    Posts
    568
    thank you !!

    Jacek

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 08-24-2021, 10:18 PM
  2. Auto Number Custome incremental
    By RustyRick in forum Access
    Replies: 25
    Last Post: 03-23-2020, 02:28 PM
  3. ID autonumber or incremental number in VBA
    By MadTom in forum Database Design
    Replies: 3
    Last Post: 12-02-2019, 04:45 PM
  4. Replies: 6
    Last Post: 11-27-2014, 03:21 AM
  5. Incremental Number in a Query
    By jmauldin01 in forum Access
    Replies: 5
    Last Post: 01-01-2014, 01:23 AM

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