Solved

Microsoft Access - Increment Number Using Update Query

Posted on 2010-11-29
5
2,497 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:apollo7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:VTKegan
ID: 34229463
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

0
 
LVL 1

Author Comment

by:apollo7
ID: 34229539
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.
0
 
LVL 10

Accepted Solution

by:
VTKegan earned 500 total points
ID: 34229560
Click on the create tab at the top of access.  On the far righ under other there will be a split button that says macro.  Click on the drop down and select module.

This will open up the VBA editor.

Type in the first line of code like this Public Sub [YOURSUBROUTINENAMEHERE] ()

Then paste the code from above.  The editor should automatically include End Sub at the end of the routine after typing in the first line.

Press ctrl + G to open the immediate window.

In that window you can simply type the name of the subroutine you just created and it will run the code.
0
 
LVL 19

Expert Comment

by:MINDSUPERB
ID: 34229578
Is your CallID field has an AutoNumber format?

Ed
0
 
LVL 1

Author Closing Comment

by:apollo7
ID: 34308124
Thanks very much for the help
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question