[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Microsoft Access - Increment Number Using Update Query

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

Industry Leaders: 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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

656 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