By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,630 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,630 IT Pros & Developers. It's quick & easy.

Problem using update query and Function ResetCounter()

P: 1
Dear all,



I did a query which update a sequence number (column NR_SEQUENZA) in a table using a nice code (from Trevor !).


1) Given that I'm not a programmer I can't understand why numbering doesn't start always, running more times the update query, from the same starting parameter assigned (1000000000).
It seems to me that it takes memory last number calculated and running prox update it starts from last table row updated. I need to start always with 100000000 just to feed a 10 char field sequence reference. Is it possible to reset .... it to have this behavior each time I run the upt query ?



2) The update query has only 1 column of table with the update to set to:
GetNextCounter([NR_SEQUENZA])

The other colum is populated by : ResetCounter().

When you save and close it you loose the ResetCounter() column becouse no parameters are defined and so I can't run the query within a macro or from a button. That's a big problem becouse the query is within a weekly basis application and the end user can't go to edit the query and insert manually ResetCounter(). Is there a trick or a workaround ?? (I hope easy one...) to save and run the query each time with no edit it before ?



Any suggestion will be very appreciated.
Thanks in advance
Giovanni

==============
Option Explicit
Dim mlngCounter As Long

Function ResetCounter()
mlngCounter = 1000000000
End Function

Function GetNextCounter(pvar As Variant) As Long
mlngCounter = mlngCounter + 1
GetNextCounter = mlngCounter
End Function



In the Query:
Select [feilds...], ResetCounter(), GetNextCounter([a valid column]) From table....

How this works:
ResetCounter() takes no parameters so Access (being as efficient as ever:) only calls the function once before returning any rows as it thinks the return value will never change, hence the counter only gets reset once.
GetNextCounter() must be sent a valid column as a parameter, Access will realise that it must call this function for every row as each value sent can be different, so it increases the counter on each row.

Please note: This won't work very well for queries & forms where you intend to update data or scroll up and down as the calculated fields gets calculated again and the sequence number will keep going up and up.
Attached Images
File Type: jpg upd_query.jpg (16.1 KB, 231 views)
Dec 20 '09 #1
Share this Question
Share on Google+
1 Reply


ADezii
Expert 5K+
P: 8,638
I am very confused as to the nature of your question, but it seems as though you are attempting to Increment a Counter at the same time that you are Resetting it. Assuming Table1 consists of a single Field named [NR_SEQUENZA], which contains a single Value to be used as a starting point to be incremented; you can change this Value (Reset), then Run your Query by executing the single line of code below:
Expand|Select|Wrap|Line Numbers
  1. CurrentDb.Execute "UPDATE Table1 SET [NR_SEQUENZA] = 100000000;", dbFailOnError
Dec 21 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.