Solved

Microsoft Access - Increment Number Using Update Query

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

705 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

16 Experts available now in Live!

Get 1:1 Help Now