I have a query in access and I need it to also have an auto incremented field. I can’t add it physically to the table. I tried to create a function to the filed inc : incfun(1)
EDIT:
In my saved query I created a column in design mode called INC : incfun(1) <–This is how I call my function. It outputs the data to the INC column.
Here is my function in module1 script file.
Dim inc As Integer
Option Compare Database
Function incfun(num As Integer) '1 is first passed to function as 1
inc = num + 1 '1 added to global variable inc
incfun = inc 'inc value passed back to output of function.
End Function
EDIT: Every time I refresh the datasheet it goes up by 1, but doesn’t auto increment threw out all the rows. Anyway to make this work?
Are you doing something like this?
Both of these calls sets inc equal to the parameter (value = 1) + 1. No matter how many times you call it
inc = incfun(1)will always setincto 2.Either use
inc = inc + 1which I would have thought was the easier solution.or replace the body of the function with: