Results 1 to 4 of 4
  1. #1
    sburch032870 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2014
    Posts
    3

    Need a sequential number for each record returned by query in access

    So I have a database that issues transmittal documents for each client.
    Each client has multiple projects that I manage. My DB has a master
    transmittal number (Primary Key) that keeps the records in order for the
    client. What I really need to do is from the single Primary Key (here
    it is the master transmittal number) that no matter what other records
    contain, the query which will pull by project, should return any records
    issued for that project ordered by the Primary Key (here ID) but then
    adding a sequential number to a field based on the records returned in
    the query.



    This way if I issue only 5 transmittals to project 611 out of 1000
    transmittals to other projects it will sequence the query records first
    in order of the Primary Key then insert a sequential number into a
    query-number field which I will use on the report and form side as the
    sequential transmittal number for that project by formatting it in
    either the form or report. That way I have a master sequential number
    for the document type issued for each client, but then when I have to
    answer a subpoena, I can pull the appropriate records with sequential
    numbering and they do not think that I am skipping records because the
    transmittal number is from the master list where only a few records are
    relevant or were issued for this project.



    I have put together a sample database that the has the basics of what
    the main system has in it. The only real issue is that I need this
    code/macro/whatever to be portable so that I can just drop it into the
    next client DB if possible. If not I could build the client DB around a
    template transmittal DB that was working this way. http://www.sburch.com/query/Transmittal%20System.accdb

    I also uploaded a printed transmittal with some notes to better display what I am in the end needed to end up with. http://www.sburch.com/query/Transmit...Page%20001.jpg

    So what I am trying to do it get from here:



    TLogID ProjectNo ToName TRXNoForProject Attn TDate
    TRX.0001 8010 Sample Company 1
    First Last 11/2/2014
    TRX.0002 8017 Sample Company 1
    First Last 11/4/2014
    TRX.0003 8017 Sample Company 1
    First Last 11/4/2014
    TRX.0004 8010 Sample Company 1
    First Last 11/4/2014
    TRX.0005 8017 Sample Company 1
    First Last 11/4/2014

    To here in a query.
    TLogID ProjectNo TRXNoForProject ToName Address
    TRX.0001 8010 1 Sample Company 1 1213 Street
    TRX.0004 8010 2 Sample Company 1 1213 Street

    Then I an

    This is the query with the transmittal number for this project that I am trying to get to in the end. I would then take the number “1” (or the next sequential number for the project) and format it as a transmittal number “8010.TRX.0001” and so on for this project in the form and reports that I am using. I just manipulated the table to create this query as an example, what I am looking for you to do is get this thing to plug the “1” and then “2” and so on in that field based on the order of the TLogID which is the master transmittal number (auto) primary key from the master transmittal log.

    Thank you for any help that you can offer.

  2. #2
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Here is an Example: Taking a Primary Key set to Text. In your Query!
    SerNu: Serialize("MyQueryNameHere","PrimaryKeyHere",[PrimaryKeyHere])
    You will also need a module for this.
    Module,Save As ProdGetLineNumber


    'Copyright Stephen Lebans 1999
    'May not be resold
    'Please include my 1 line Copyright notice
    'in your code if you use these functions


    'I left a bunch of development code in here in case anyone decides to go
    'down the same paths I did.
    'Created by Stephen Lebans with help from Chris Bergmans
    ' Updated by Allen Browne Oct/2002
    'Production version of GetLineNumberForm
    'Works in Form or SubForm mode
    'Set controlsource of unbound Text box to
    '= RowNum([Form])
    'Type exactly as above


    Public Function RowNum(frm As Form) As Variant
    On Error GoTo Err_RowNum
    'Purpose: Numbering the rows on a form.
    'Usage: Text box with ControlSource of: =RowNum([Form])

    With frm.RecordsetClone
    .Bookmark = frm.Bookmark
    RowNum = .AbsolutePosition + 1
    End With

    Exit_RowNum:
    Exit Function

    Err_RowNum:
    If Err.Number <> 3021& Then 'Ignore "No bookmark" at new row.
    Debug.Print "RowNum() error " & Err.Number & " - " & Err.Description
    End If
    RowNum = Null
    Resume Exit_RowNum
    End Function


    '********************************************
    'Only USE GetLineNumberForm and Serialize
    '********************************************




    'Stephen Lebans with help from Chris Bergmans
    ' Updated by Allen
    'Production version of GetLineNumberForm
    'Works in Form or SubForm mode
    'Set controlsource of unbound Text box to
    '= GetLineNumberForm([Form])
    'Type exactly as above


    Function GetLineNumberForm(f As Form)
    Dim rs As Recordset
    Dim frmMain As Form
    Dim frmCur As Form
    Dim strName As String
    Dim IsItSubForm As Boolean
    Dim strFName As String

    'is the form referenced in the parameter currently
    'loaded as a SubForm?
    'Check it parent property to find out.

    On Error Resume Next
    strFName = f.Parent.NAME
    IsItSubForm = (Err = 0)

    'Point to our error handler
    On Error GoTo Err_GetLineNumber

    If IsItSubForm Then
    'its a SubForm
    Set frmMain = f.Parent.Form
    strName = f.NAME
    Set frmCur = frmMain(strName).Form
    Set rs = frmCur.RecordsetClone

    Else
    'It's not a SubForm
    Set rs = f.RecordsetClone
    Set frmCur = f
    End If

    ' Find the current record.
    rs.Bookmark = frmCur.Bookmark
    GetLineNumberForm = rs.AbsolutePosition + 1


    Bye_GetLineNumber:
    Set rs = Nothing
    Set frmMain = Nothing
    Set frmCur = Nothing
    Exit Function


    Err_GetLineNumber:
    Resume Bye_GetLineNumber


    End Function


    'Stephen Lebans April 1999
    'For use directly as a calculated expression in the query
    'For demonstration purposes only
    'See my posting comp.databases.ms-accessfor an adapted version of this function
    'to be used on a form in an unbound text box www.Dejanews.com search Lebans query
    'Blatantly adapted from Microsoft source code
    'ACC: How to Display Line Numbers on Subform Records Article ID: Q120913
    'Sorry, recordset property of Screen.ActiveDatasheet is not available
    'In query design view save any changes to your query to disk before executing your query
    'Only send an indexed field to the function


    Function Serialize(qryname As String, keyname As String, keyvalue) As Long
    Dim dbs As Database
    Dim rs As Recordset


    Set dbs = CurrentDb
    On Error GoTo Err_Serialize
    Set rs = dbs.OpenRecordset(qryname, dbOpenDynaset, dbReadOnly)


    On Error GoTo Err_Serialize


    'Find the current record.
    Select Case rs.Fields(keyname).Type
    ' Find using numeric data type key value?
    Case DB_INTEGER, DB_LONG, DB_CURRENCY, DB_SINGLE, _
    DB_DOUBLE, DB_BYTE
    rs.FindFirst "[" & keyname & "] = " & keyvalue
    ' Find using date data type key value?
    Case DB_DATE
    rs.FindFirst "[" & keyname & "] = #" & keyvalue & "#"
    ' Find using text data type key value?
    Case DB_TEXT
    rs.FindFirst "[" & keyname & "] = '" & keyvalue & "'"
    Case Else
    MsgBox "ERROR: Invalid key field data type!"

    End Select


    Serialize = Nz(rs.AbsolutePosition, 0) + 1




    Err_Serialize:
    'Add your own Error handler
    rs.Close
    dbs.Close
    Set rs = Nothing
    Set dbs = Nothing


    End Function



    This may not be what your looking for but, HTH

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Maybe the RunningSum property of textbox on report will serve?
    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.

  4. #4
    AlexHedley's Avatar
    AlexHedley is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    England
    Posts
    180
    I've written a tip on generating a http://599cd.com/tips/access/140703-row-number/ Row Number in a Query, could you use this

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

Similar Threads

  1. Replies: 1
    Last Post: 11-26-2013, 01:41 AM
  2. Replies: 18
    Last Post: 05-15-2012, 03:44 PM
  3. Adding record with next sequential number
    By stanley721 in forum Forms
    Replies: 3
    Last Post: 02-28-2011, 01:26 PM
  4. Replies: 1
    Last Post: 09-22-2010, 08:03 AM
  5. Replies: 1
    Last Post: 01-31-2009, 10:43 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