Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS Access  last number used +1

Posted on 2009-06-27
7
Medium Priority
?
407 Views
Last Modified: 2013-11-28
When creating a record I need to assign a unique number. I do not want to use the auto number. I would like to use the last number +1. This is a multi user database and I can't have duplicate numbers. The front end is MS Access and back end in MS SQL. How would I do this?
0
Comment
Question by:seanlhall
[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
7 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24728347
Since you like to live dangerously do it this way:

BEGIN TRANSACTION

INSERT YourTable (UniqueNumber, Col1, Col2, ...)
SELECT MAX(UniqueNumber) + 1, 'Value1', 'Value2', ...
FROM    YourTable

COMMIT
0
 
LVL 39

Accepted Solution

by:
thenelson earned 1500 total points
ID: 24728350
DMax("YourField", "YourTable") + 1
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 24731389
Do you want a date serialized number. Who is keying it in? The acct exec id. A composite off the customer.

There are many ways of doing this.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:seanlhall
ID: 24731449
The starting number will be keyed in by me starting at 100. The number will be use for the case number. Example Year-100 and 09-101... I have been using the ID number of the table but if a record is deleted I lose that number.
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 24731490
The problem, as I see it, is trying to keep everything in a series. The total insistence on keeping everything in series (1,2,3,4,5) without having missing numbers (1,2,3,5...) is just overwrought. It doesn't buy you anything.

In addition, are you sure you will have less than 899 cases a year? Your range is 09-100 to 09-999. What happens then.

I'm bringing up the design points now.
0
 

Author Comment

by:seanlhall
ID: 24762537
I agree that each number in series 1,2,3,4 is most likely not going to happen. The range will start at 100 and continue from there. There is not ending number. At the begin of the new year the number will return to 100.  Example 10-100.
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 24762694
This function should be able to do what you want.
Public Function GetIDNum()
 
Dim DB As Database
Dim RS As Recordset
Dim SQL As String
 
 
'This is based on having a table "IdxNbrTbl" with the _
 columns "Id_Num" as a number field and "Year_Val" as _
 string/text field.
 
SQL = "SELECT Id_Num, Year_Val " & _
    "FROM IdxNbrTbl"
 
'Determine Current FY
 
Set DB = CurrentDb()
Set RS = DB.OpenRecordset(SQL)
 
With RS
    .MoveFirst
    'This will reset the value if the ID_Num (Record number) _
     back to 100 if its a new Year.
    If !FY_Val <> Year(Now()) Then
       .Edit
       !ID_Num = 100
       !FY_Val = Year(Now())
       .Update
    End If
    'If the Year hasn't changed, then this just gets the new _
     ID_Num and passes it back.
    GetIDNum = Right(!FY_Val, 2) & "-" & Format(!Idx_num, "000")
    .Edit
    !ID_Num = !ID_Num + 1
    .Update
End With
 
Set RS = Nothing
Set DB = Nothing
 
End Function

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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