Create autonumber in a query

  • Thread starter Thread starter Esperanza
  • Start date Start date
E

Esperanza

Hello VBA Expert !
I want an auto increment number in my query.
Even if I declare a public variable : intTransacNoEDI
the result of my query always return 1 for each record.
Thanks in advance !!

'==================================
Public intTransacNoEDI As Integer

Public Function AutoIncrementeNo() as integer
intTransacNoEDI = intTransacNoEDI + 1
AutoIncrementeNo = intTransacNoEDI
End Function



Query Field
EdiTransacNumber: AutoIncrementeNo()
 
Hi,


The query optimizer knows that AutoincrementNo(), where it appears in your
query, does NOT depends on any FIELD of any TABLES implied in the query.
THAT IS THUS A CONSTANT, in its opinion, and a constant expression needs to
be evaluated only once, exactly as if you have

SELECT f1+3+4 FROM myTable


will likely become, internally

SELECT f1+7 FROM myTable


That is the kind of thing an Optimizer looks for. To "fool" the optimizer,
send an argument, a field name:


SELECT AutoIncrementeNo(fieldNameHere) FROM myTable


the Optimizer will then 'think' the function has to be evaluated for each
record.


Have your function like:

---------------------------
Public Function AutoIncrementNo( Optional arg As Variant) As Long
Static intTransacNoEDI As Long
If IsMissing(arg) then
intTransacNoEDI =0
Else
intTransacNoEDI =1+intTransacNoEDI
End If

AutoIncrementNo=intTransacNoEDI
End Function
-----------------------------


thus,


SELECT AutoIncrementNo(), AutoIncrementNo( fieldName)
FROM myTable


will automatically initialize your counter to 0, because of the 'constant'
expression evaluated once, through AutoIncrementNo(), and also, you will get
1, 2, 3, ... through AutoIncrementNo( fieldName) . So, no need to
'remember' to reset the global variable to 0, before launching the query!
is it not nice?

Only problem, Jet also optimized the computation of functions (in recent
versions of Jet). As example, if you look at the result, the first page will
look fine, make a page down, again, fine, but then, make a page up to go to
the start, and oops, the sequence continue to fire, rather that being 1, 2,
3 ... as initially.


So, as elegant as it can be, I suggest you append the data to a new table
that would have an autonumber (autoincrement by 1), and read that
(temporary) table once the data (other than the autonumber) had been
appended.

If you are using MS SQL Server 2005, you can use the new function RANK to
get a continuous sequencing.

Hoping it may help,
Vanderghast, Access MVP
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You have to put a "primer" in the call to the function. The primer
ensures that the function is called for each new record:

AutoIncrementNo(<column_name>)

The column_name can be any column in the query/table. It isn't
important. It is just used to ensure the function is run for each
record.

The function definition would be like this:

Public Function AutoIncrementeNo(varNothing) as integer
' varNothing just acts as a primer
' - it is not use for anyting else
intTransacNoEDI = intTransacNoEDI + 1
AutoIncrementeNo = intTransacNoEDI
End Function
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ9fyGYechKqOuFEgEQKu3QCfU0tlC7SsBtdoncp7K/TLkckUL0IAoIGK
7ewTuodWDOGo/V18YaMB9DZ2
=cBsO
-----END PGP SIGNATURE-----
 
Back
Top