?
Solved

Microsoft Access - Increment Number Using Update Query

Posted on 2010-11-29
5
Medium Priority
?
2,692 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

762 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