473,671 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Auto Incrementing

I am trying to create a field where the primary key field will produce JDP-
001; where the three letters come from the first name, middle intial and
last name of my table. I want to auto increment only if the three letters
are reproduced.

e.g. JDP-001
JDP-002
YPT-001
YRT-001
RPT-001

When you reply, let me know if I can do it in the table design view or the
form design view or both.

--
Message posted via http://www.accessmonster.com
Nov 13 '05 #1
8 3481
I would assume you are trying to do this when inserting new records?

There is a function, Left(firstname, 1), or maybe it is lowercase,
left(), anyhow, it will extract the first letter of a field(the second
parameter tells how many letters).

You could extract the first letter of each field and concactenate them
together.

As far as determining if that Primary key already exists, I would do a
query on the primary key field to find all keys that start with that 3
letter string, and sort them, look at the number on the last one(using
the Right() function to extract just the number), increment the number,
and then concatenate that onto your three letter combo.

I don't think this can be done in the table design view, but I'm not
absolutely sure. The only time I insert new records is in DTS
packages, and I would use a scripted transfoormation link in this case
I think. I'm a noob though, so there are probably better ways to do
this.

Nov 13 '05 #2
Access really isn't up to this job. However, you can do it
by creating a function that stores a "Next Number" for each
Initials combination in an "Initials" table. Then you can
call the function on the form that inserts the data.

Without real database triggers, I'm afraid that's about the
best you can do, if you HAVE to have that as a primary key.

Kevin
JD via AccessMonster.c om<fo***@nospam .AccessMonster. com>
5/9/2005 9:56:21 AM >>>I am trying to create a field where the primary key field
will produce JDP-
001; where the three letters come from the first name,
middle intial and
last name of my table. I want to auto increment only if
the three letters
are reproduced.

e.g. JDP-001
JDP-002
YPT-001
YRT-001
RPT-001

When you reply, let me know if I can do it in the table
design view or the
form design view or both.

--
Message posted via http://www.accessmonster.com

Nov 13 '05 #3
The field that I have created will be used as the primary key, therefore I
need to make each record unique.

I know how to auto increment with numbers, but I was trying to find out how
to do it with letters and numbers combination.

I need someone to add more to Shumaker comment.

--
Message posted via http://www.accessmonster.com
Nov 13 '05 #4
Explain for me. What is the scenario for adding records? Are you
wanting the user to be able to use the Add new record button of a form
and add records individually? Or is this in an insert query where you
are inserting a batch of records?

Nov 13 '05 #5
rkc
JD via AccessMonster.c om wrote:
The field that I have created will be used as the primary key, therefore I
need to make each record unique.

I know how to auto increment with numbers, but I was trying to find out how
to do it with letters and numbers combination.

I need someone to add more to Shumaker comment.


What's the point of a letter number combination being used as an
artificial primary key? Is it a requirement from the client/boss or
something you dreamed up yourself?
Nov 13 '05 #6
I want to have it semi-automated on the form, where the user enter the
three intitials and populate the next number sequence if the member id
three initials are the same.

The reason why I want it like this, because I don't want the user to have
to keep guessing what number is in next number sequence for a particular
three initials pair.

Right now, I have created a input mask as such: >LLL\-000 for the table
field: Member ID. It works fine, but like I explained in the last
paragraph the user will have to keep guessing what's the next number
sequence until they achieve the correct one, when Access prompt them that
the member ID is not unique.

When anyone creates a database we want them to be user friendly.
Kevin:
Explained to me how to write a simple Next function for my application. I
have just begun to learn VBA and I don't know all the bells and whistles of
this powerful program.

--
Message posted via http://www.accessmonster.com
Nov 13 '05 #7
JD,
I keep a one row table with as many columns as I need for each different
sequence in my database. I have code I wrote that retrieves the old
sequence number from this table and increments it to create my new number.
It is designed to fail if the update to the sequence table fails. It works
pretty well. In your case you could concantenate together the text you want
appended to the number with a function that returns the next sequence
number. Sample code for the sequence function is below:

Public Function NextItemNo() As Long
On Error GoTo Err_NextItemNo

Dim cn As New ADODB.Connectio n
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Dim SQL As String
Dim RetVal As Long

Set cn = CodeProject.Con nection

SQL = "SELECT ITEM_NO + 10 AS NEW_ITEM_NO FROM SEQUENCE_TBL;"

With rs
.ActiveConnecti on = cn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.Source = SQL
.Open
If Not .EOF Then
RetVal = !NEW_ITEM_NO
Else
SQL = "SELECT MAX(CLNG(NZ(ITE M_NO,0))) + 10 AS NEW_ITEM_NO FROM
ITEM_MASTER_TBL ;"
.Requery
If Not .EOF Then
RetVal = !NEW_ITEM_NO
End If
End If
.Close
End With

SQL = "UPDATE SEQUENCE_TBL SET ITEM_NO = " & RetVal & ";"

With cmd
.ActiveConnecti on = cn
.CommandType = adCmdText
.CommandText = SQL
.Execute
End With

Done:
On Error Resume Next
NextItemNo = RetVal
Set rs = Nothing
Set cmd = Nothing
cn.Close
Set cn = Nothing

Exit_NextItemNo :
Exit Function

Err_NextItemNo:
Debug.Print "There was an unexpected error in getting a new item number:
" & Err.DESCRIPTION
Resume Done
End Function

The basic algorythm would work as a stored procedure in a buncs of rdbms'
with changes to account for differences in the scripting language of the
rdbms in question.
--
Alan Webb
kn*******@SPAMh otmail.com
"It's not IT, it's IS
"JD via AccessMonster.c om" <fo***@nospam.A ccessMonster.co m> wrote in message
news:de******** *************** *******@AccessM onster.com...
I am trying to create a field where the primary key field will produce JDP-
001; where the three letters come from the first name, middle intial and
last name of my table. I want to auto increment only if the three letters
are reproduced.

e.g. JDP-001
JDP-002
YPT-001
YRT-001
RPT-001

When you reply, let me know if I can do it in the table design view or the
form design view or both.

--
Message posted via http://www.accessmonster.com

Nov 13 '05 #8
I would agree with Alan that this would be the simplest way
to handle the job, as long as you don't need identical
initials to have consecutive numbers.
Like:
ABC-001
XYZ-002
MNO-003
ABC-004

If you DO need them to be consecutive, change the code below
to query a table like INITIALS_SEQ instead of SEQUENCE_TBL,
and insert 1 row per unique set of initials.

Table: InitialsSeq
- Initials Text(3) (Primary Key)
- NextSeq: Integer

You will have to be more careful here, because you will have
to inspect the table for existing initials and insert a new
row if not there.

Public Function MyPrimaryKey(My Initials as string) as
String
' Step 1: Test to see if new initials, retrieving CurrSeq
' Step 2: If New,
' Step 2a - insert a new record with NextSeq = 2
' Step 2b - Set CurrSeq = 1
' Step 3: Else,
' Step 3a: - Update the existing record with NextSeq =
CurrSeq + 1
' Step 4: Format Key with initials and CurrSeq
' Step 5: Return Key
End Function

Kevin
Alan Webb<kn*******@ hotSPAMmail.com > 5/10/2005 9:53:57
AM >>>JD,
I keep a one row table with as many columns as I need for
each different
sequence in my database. I have code I wrote that
retrieves the old
sequence number from this table and increments it to createmy new number.
It is designed to fail if the update to the sequence table
fails. It works
pretty well. In your case you could concantenate together
the text you want
appended to the number with a function that returns the
next sequence
number. Sample code for the sequence function is below:

Public Function NextItemNo() As Long
On Error GoTo Err_NextItemNo

Dim cn As New ADODB.Connectio n
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Dim SQL As String
Dim RetVal As Long

Set cn = CodeProject.Con nection

SQL = "SELECT ITEM_NO + 10 AS NEW_ITEM_NO FROM
SEQUENCE_TBL ;"

With rs
.ActiveConnecti on = cn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.Source = SQL
.Open
If Not .EOF Then
RetVal = !NEW_ITEM_NO
Else
SQL = "SELECT MAX(CLNG(NZ(ITE M_NO,0))) + 10 AS
NEW_ITEM_NO FROM
ITEM_MASTER_TB L;"
.Requery
If Not .EOF Then
RetVal = !NEW_ITEM_NO
End If
End If
.Close
End With

SQL = "UPDATE SEQUENCE_TBL SET ITEM_NO = " & RetVal &
";"

With cmd
.ActiveConnecti on = cn
.CommandType = adCmdText
.CommandText = SQL
.Execute
End With

Done:
On Error Resume Next
NextItemNo = RetVal
Set rs = Nothing
Set cmd = Nothing
cn.Close
Set cn = Nothing

Exit_NextItemN o:
Exit Function

Err_NextItemNo :
Debug.Print "There was an unexpected error in getting anew item number:
" & Err.DESCRIPTION
Resume Done
End Function

The basic algorythm would work as a stored procedure in a
buncs of rdbms'
with changes to account for differences in the scripting
language of the
rdbms in question.
--
Alan Webb
kn*******@SPAM hotmail.com
"It's not IT, it's IS
"JD via AccessMonster.c om" <fo***@nospam.A ccessMonster.co m>wrote in message
news:de******* *************** ********@Access Monster.com...
I am trying to create a field where the primary key field

will produce JDP-
001; where the three letters come from the first name,

middle intial and
last name of my table. I want to auto increment only if

the three letters
are reproduced.

e.g. JDP-001
JDP-002
YPT-001
YRT-001
RPT-001

When you reply, let me know if I can do it in the table

design view or the
form design view or both.

--
Message posted via http://www.accessmonster.com

Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1679
by: TheMechE | last post by:
Has anyone had a table that has lived long enough to wrap the auto incrementing number...? i.e. the complete byte span and back to 0x32. I'm just wondering if mySql will fill in the deleted ID's that don't exist, or if it just halts... --
0
1490
by: oo00oo | last post by:
Hi people, I am trying to make my own auto_increment for some tables as the ID's have a string prefix in front of them. For example: - customer table has the prefix CUS, - employee table has the prefix EMP, - item table has different prefixes like WB, WC, TFB, etc... The difficulty i am having is finding the last inserted row for that table. When I use the following SQL statement I don't get what i would
7
12842
by: Yannick Turgeon | last post by:
Hello all, I'm using SS2K on W2k. I'v got a table say, humm, "Orders" with two fields in the PK: OrderDate and CustomerID. I would like to add an "ID" column which would be auto-increment (and would be the new PK). But, I would really like to have orders with the oldest OrderDate having the smallest ID number and, for a same OrderDate, I'd to have the smallest CustomerID first. So my question is:
3
3576
by: Poul Møller Hansen | last post by:
Hi, I need an auto incrementing field that will contain values like N000001, N000002, N000003 etc. I think the way is to use the value from an identity field in a stored procedure that is triggered at insert. I can't see that it can be made in pure SQL, but Java is not a problem. Any of you that can tell me the way of doing it ?
16
20482
by: John Baker | last post by:
Hi: I know this is a strange question, but I have inherited a system where files are copied and records re auto numbered (as an index field) )frequently, and I am wondering how high the number can go without the system crashing. An ancillary question is how one resets an auto number so that the sequence starts again at 1. In the case of this file, the auto number field serves no useful purpose except as an
1
2097
by: Dave Stone | last post by:
I find with a table in datasheet view, if a Number field is blank, and I fill it in for a series of rows (more than 2) with values incrementing by 1 or 2 (only), then Access 'helpfully' fills in the field in the 3rd and subsequent rows in turn, incrementing by the 'appropriate' amount. So if Product# is filled in in row 1 as 123, down-arrow to Product# in row 2 and type 125, down-arrow to Product# in row 3, Access has filled in 127. Is...
6
5063
by: Alpha | last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key column and the 2nd is a string. When I add a new row to the dataset to be updated back to the database. What should I do with the 1st column ? (Below I have a "1" in place for now). Also, Does the datase.AcceptChanges(); updates the changes to the database? Which command do I use to update the changes in dataset back to the Access database table? Thanks, Alpha...
1
1541
by: John | last post by:
Hi I am using Application.ProductVersion.ToString to get the app build number but the problem is that it does not auto increment. I have even replaced the content of AssemblyInfo.vb file from a new project in case it was corrupt or something but no luck. What elements effect the auto-increment of ProductVersion so I can check those? Thanks
12
28438
by: badvoc | last post by:
Hi, I have had some good fortune on this site so I am back and I must iterate I am a beginer. I am having some problems getting to grips with the right technique to manage variables and adding a unique order number to a customer order form. I have 4 stages to my order form. Stage 1 involves the user selecting a link based on what they want. The link then sets 2 variables. $type and $fault based on the link they have chosen.
13
37558
by: BobLewiston | last post by:
Using Visual C# 2008 Express and SQL Server 2008 Express, I would like to insert new records into database "AdventureWorks", table "Person.Contact". To my surprise, this table's int-value identity column "ContactID" does not appear to be auto-increment. I don't know how to confirm for sure that this is so. (I have installed SQL Server Management Studio, if that helps.) How can I confirm this, and how can I make ContactID auto-increment? ...
0
8481
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8400
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8602
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8672
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6234
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.