Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How add a field with sequential numbers to a form

Posted on 2016-11-28
7
57 Views
Last Modified: 2016-12-01
I have a form that is a continuous form.  I want to add a field to the form that just shows a number that is not bound to the record source.

The first record would show "1", the second record would show "2", etc.  Is there a way to do this?
0
Comment
Question by:SteveL13
7 Comments
 
LVL 36

Expert Comment

by:PatHartman
ID: 41904423
What is the point of the sequence number?  If you sort the recordset, would the sequence number now refer to a different row?  You can do something using DCount("*", "PK < " & PK) +1 which will count the number of rows with primary key values (autonumber is assumed here0 less than that of the current record.  If the data is grouped some way, the where argument of the DCount() would need to include the grouping field to count over the group.  This count would work ONLY if the rows were sorted in PK order.

This is quite easy to do with a report because the recordset isn't "live" and can't be sorted without rerunning the entire report.
0
 

Author Comment

by:SteveL13
ID: 41904444
The purpose is just to show the records as 1, 2, 3, etc.  No relationship to the PK.  So DCount("*", "PK < " & PK) +1 won't work.  The PK numbers are not sequential.
0
 
LVL 36

Expert Comment

by:PatHartman
ID: 41904479
Unless you can utilize the actual sort sequence AND it is UNIQUE, then you won't be able to assign a sequential number.  How are the rows sorted?  Try concatenating the sort fields and using that to determine the count of earlier records.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 19
ID: 41904801
the key is "that just shows a number that is not bound to the record source."

CAN you modify the structure? If so you could add:
TempRecNum, Long Integer, Default Value =Null

This temporary field could be updated as necessary.

While there is a way to use code to add a number to show as a "record number", it could recalculate and throw values off as the user moves if there is no way to bind it to values in the table.
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 250 total points
ID: 41904839
You can expand the query that you use as source with a field holding a rownumber obtained from this function:
Public Function RowCounter( _
  ByVal strKey As String, _
  ByVal booReset As Boolean, _
  Optional ByVal strGroupKey As String) _
  As Long
  
' Builds consecutive RowIDs in select, append or create query
' with the possibility of automatic reset.
' Optionally a grouping key can be passed to reset the row count
' for every group key.
'
' Usage (typical select query):
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' Usage (with group key):
'   SELECT RowCounter(CStr([ID]),False,CStr[GroupID])) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' The Where statement resets the counter when the query is run
' and is needed for browsing a select query.
'
' Usage (typical append query, manual reset):
' 1. Reset counter manually:
'   Call RowCounter(vbNullString, False)
' 2. Run query:
'   INSERT INTO tblTemp ( RowID )
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable;
'
' Usage (typical append query, automatic reset):
'   INSERT INTO tblTemp ( RowID )
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter("",True)=0);
'
' 2002-04-13. Cactus Data ApS. CPH
' 2002-09-09. Str() sometimes fails. Replaced with CStr().
' 2005-10-21. Str(col.Count + 1) reduced to col.Count + 1.
' 2008-02-27. Optional group parameter added.
' 2010-08-04. Corrected that group key missed first row in group.

  Static col      As New Collection
  Static strGroup As String
  
  On Error GoTo Err_RowCounter
  
  If booReset = True Then
    Set col = Nothing
  ElseIf strGroup <> strGroupKey Then
    Set col = Nothing
    strGroup = strGroupKey
    col.Add 1, strKey
  Else
    col.Add col.Count + 1, strKey
  End If
  
  RowCounter = col(strKey)
  
Exit_RowCounter:
  Exit Function
  
Err_RowCounter:
  Select Case Err
    Case 457
      ' Key is present.
      Resume Next
    Case Else
      ' Some other error.
      Resume Exit_RowCounter
  End Select

End Function

Open in new window

Typical usage for a select query is shown in the in-line notes.
Note please, that this function is very fast - no DCount or the like - thus impact on the display will be minimal during load, and none during browsing.

/gustav
0
 
LVL 36

Assisted Solution

by:PatHartman
PatHartman earned 250 total points
ID: 41904843
The solution suggested by Gus uses a function that makes use of the concatenated sortKey I mentioned a few posts ago.  So, you can use DCount() with the concatenated sortkey or you can implement Gus' function.

Personally, I generally stick to built-in SQL functions and the few VBA functions that the ODBC drivers can translate directly to T-SQL since my databases are almost always SQL Server and so I cannot use VBA or UDF functions.  That allows me to upsize applications at the drop of a hat rather than having to take days to ferret out all the non-portable code.
0
 
LVL 19
ID: 41904861
Gustav often has great solutions -- this, however, was already suggested by Pat.  But he gave code with usage examples  ...

bottom line is this:
if there is a sort key that is unique, code can be used that ties the sort key to values in the table.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question