UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Create A Row Number Id, Office 2010    
 
   
MrFormula
post Apr 1 2012, 08:43 AM
Post #1

UtterAccess Veteran
Posts: 461



I am desiging a query and I would like to add a column that counts up the row numbers. I don't want the total rows I want the first row to have 1 and the second row to have 2 ect... I am currently using a funciton that I found searching online

Function rownum(dummy) As Integer
Static firstdummy, row&
If IsNull(firstdummy) Or IsEmpty(firstdummy) Then firstdummy = dummy
If dummy = firstdummy Then row = 0
row = row + 1
rownum = row
End Function

and then I put this in my query

bnID: rownum([Id])

The first time it works but when I requery for a different group it does not start at 1 on the count. It starts at a much higher number.

I want to always have my bnID start at 1

any help would be appreciated.
Go to the top of the page
 
+
arnelgp
post Apr 1 2012, 09:06 AM
Post #2

UtterAccess Ruler
Posts: 1,090



Add this to your query fields:

Row NUmber : DCount("id","myTable","id <= " & [id])

This post has been edited by arnelgp: Apr 1 2012, 09:07 AM
Go to the top of the page
 
+
Alan_G
post Apr 1 2012, 09:08 AM
Post #3

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,895
From: Devon UK



Hi

Have a look at Stephen Lebans method
Go to the top of the page
 
+
arnelgp
post Apr 1 2012, 09:11 AM
Post #4

UtterAccess Ruler
Posts: 1,090



That was was not Lebans, it was in Pro Access 2007 by Martin Reid (moderator of DatabaseAdvisors).
Go to the top of the page
 
+
MrFormula
post Apr 1 2012, 09:46 AM
Post #5

UtterAccess Veteran
Posts: 461



I don't want the actual row number I want the New row number to start at 1
Go to the top of the page
 
+
Alan_G
post Apr 1 2012, 09:48 AM
Post #6

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,895
From: Devon UK



Hi

QUOTE
That was was not Lebans


The link is to Stephen Lebans site, from where it seems to be something of a combined effort of Stephen, Allen Browne and Peter Schroeder. It may very well be available on other sites also.

Whoever was the original author though doesn't really matter, it's freely available and the important point is that it will do exactly what MrFormula wants
Go to the top of the page
 
+
arnelgp
post Apr 1 2012, 09:58 AM
Post #7

UtterAccess Ruler
Posts: 1,090



Ok, well just rephrase it:

bnID : DCount("id","myTable","id <= " & [id])

You should try all possibilities, especially Mr.Alan G post.

Hello Mr. Alan G.
Go to the top of the page
 
+
Gustav
post Apr 1 2012, 03:17 PM
Post #8

UtterAccess VIP
Posts: 1,851



Here is a method using a collection and it works really fast:
CODE
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));
'
' 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.

  Static col      As New Collection
  Static strGroup As String
  
  On Error GoTo Err_RowCounter
  
  If booReset = True Or strGroup <> strGroupKey Then
    Set col = Nothing
    strGroup = strGroupKey
  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

/gustav
Go to the top of the page
 
+
MrFormula
post Apr 1 2012, 09:39 PM
Post #9

UtterAccess Veteran
Posts: 461



I am trying to impliment Alan's suggestion but I don't see how to call the funciton from the query. I know that the funciton is Serialize but what is the pramiters that I need to make this work. I put this in the query bnID: Serialize(id).

Sorry for my lack of experience.
Go to the top of the page
 
+
theDBguy
post Apr 1 2012, 09:43 PM
Post #10

Access Wiki and Forums Moderator
Posts: 48,599
From: SoCal, USA



Hi,

I think it's probably something like this:

RowNum: Serialize("QueryName", "ID", [ID])

Just my 2 cents... (IMG:style_emoticons/default/2cents.gif)
Go to the top of the page
 
+
MrFormula
post Apr 1 2012, 09:44 PM
Post #11

UtterAccess Veteran
Posts: 461



Gustav

Will this permantely insert the value into the table? I just want to have the value inserted while the query is run. Then when I select something different in the combo box I want it to recount the query.
Go to the top of the page
 
+
Gustav
post Apr 2 2012, 01:54 AM
Post #12

UtterAccess VIP
Posts: 1,851



> Will this permantely insert the value into the table?

That depends. You can use it safely in both select, append, and create-table queries.
Please study the in-line comments and examples.

/gustav
Go to the top of the page
 
+
MrFormula
post Apr 3 2012, 11:14 PM
Post #13

UtterAccess Veteran
Posts: 461



I have struggled with this and tried some different solutions. I could not get your suggestions to work. I knew it had to be very easy so I looke a different way. This seems to work for now. If i need to upgrade it I will come back and try again to impliment your solutions.

I found this code to work for me.

Thanks for all your help..

CODE
SELECT PosButtons.ID, PosButtons.Type, PosButtons.Desc, PosButtons.ButtonDesc, PosButtons.BN, PosButtons.SKU, PosButtons.Group, PosButtons.Qty, PosButtons.ForeColor, PosButtons.BackColor, ([bn]+1)-DMin("bn","posbuttons") AS BNid
FROM PosButtons
GROUP BY PosButtons.ID, PosButtons.Type, PosButtons.Desc, PosButtons.ButtonDesc, PosButtons.BN, PosButtons.SKU, PosButtons.Group, PosButtons.Qty, PosButtons.ForeColor, PosButtons.BackColor, ([bn]+1)-DMin("bn","posbuttons")
HAVING (((PosButtons.Group)=[forms]![PosButtons]![PosPanel]))
ORDER BY PosButtons.BN;
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 19th June 2013 - 03:47 AM