Solved

Microsoft Access - Increment Number Using Update Query

Posted on 2010-11-29
5
2,332 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
  • 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
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. …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

912 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now