Link to home
Start Free TrialLog in
Avatar of apollo7
apollo7Flag for United States of America

asked on

Microsoft Access - Increment Number Using Update Query

I need to run an update to increment an ID number, starting at a certain value and adding 1 for each record.

Currently I have a separate table (named fCallID) that contains the starting number in a field called newCallID.  The current value is 131200

I need to update the CallID field in the fCalls table.  The CallID field is blank for all 50000 records. I want to start with the newCallID value of 131200 and add 1 for every CallID in the fCalls table.

I want to run this one time from a query. Note: the primary key of fCalls is mid.

Thanks
Avatar of VTKegan
VTKegan
Flag of United States of America image

From VBA you could do something like this.


You could place this code in a module as a public Sub and run it from the immediate window.
Dim rst as dao.recordset
Dim NextID as Integer
NextID = DLookup("NewCallID","fCallID")

Set rst = CurrentDb.OpenRecordset ("Select * From fCalls")

rst.movefirst
Wile Not rst.eof
     If IsNull(rst!CallID) Then
       rst.edit     
       rst!CallID = NextID
       rst.Update
       rst.MoveNext
       NextID = NextID + 1
     Else
       rst.moveNext
     End If
Wend

Set rst=Nothing

Open in new window

Avatar of apollo7

ASKER

Thanks for the quick response.

Can you explain how to do this (from your post):

<<You could place this code in a module as a public Sub and run it from the immediate window>>.

I am familar with creating a function in a module and calling it from a query but I am not sure what you mean by the above.  Can you give me instructions on how to do this?

Thanks again.
ASKER CERTIFIED SOLUTION
Avatar of VTKegan
VTKegan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is your CallID field has an AutoNumber format?

Ed
Avatar of apollo7

ASKER

Thanks very much for the help