A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Database Design
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Incrementing a key value



 
 
Thread Tools Display Modes
  #1  
Old January 8th, 2009, 05:46 PM posted to microsoft.public.access.tablesdbdesign
Eric Johnson[_2_]
external usenet poster
 
Posts: 2
Default Incrementing a key value

I need to increment a key value by "1" with each new record and the "1" needs
to be a text field so that the format of it can be "L0001", "L0002",.....

Using Access 2003.


  #2  
Old January 8th, 2009, 06:23 PM posted to microsoft.public.access.tablesdbdesign
Dorian
external usenet poster
 
Posts: 542
Default Incrementing a key value

I'd use a regular autonumber column and just add the 'L' to the front when
you need to display the key value.

-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".


"Eric Johnson" wrote:

I need to increment a key value by "1" with each new record and the "1" needs
to be a text field so that the format of it can be "L0001", "L0002",.....

Using Access 2003.


  #4  
Old January 8th, 2009, 07:18 PM posted to microsoft.public.access.tablesdbdesign
Eric Johnson[_3_]
external usenet poster
 
Posts: 4
Default Incrementing a key value

"John W. Vinson" wrote:
Private Sub Form_BeforeInsert(Cancel as Integer)
Dim iNext As Integer
iNext = NZ(DMax("[ID]", "[tablename]")) + 1
If iNext = 10000 Then
Cancel = True
MsgBox "All ID's have been used, shut off the PC and go home", vbOKOnly
Else
Me![ID] = iNext
End If
End Sub


I was afraid it was going to get complex. How do I do the incrementing in a
table...no form? If I can't do this, I'll have to look for another solution
as there are no forms...embedded tables and reports only.

Eric Johnson
  #5  
Old January 8th, 2009, 08:24 PM posted to microsoft.public.access.tablesdbdesign
Gina Whipp
external usenet poster
 
Posts: 3,500
Default Incrementing a key value

"...MsgBox "All ID's have been used, shut off the PC and go home",
vbOKOnly..."

I like that message, not sure I'll use it anywhere, could get me 'fired'!

--
Gina Whipp

"I feel I have been denied critical, need to know, information!" - Tremors
II
"John W. Vinson" wrote in message
...
On Thu, 8 Jan 2009 09:46:43 -0800, Eric Johnson Eric
wrote:

I need to increment a key value by "1" with each new record and the "1"
needs
to be a text field so that the format of it can be "L0001", "L0002",.....

Using Access 2003.


If the L is constant and never varies, I'd suggest not storing it in the
field
at all. You can instead use an Integer or Long Integer number field with a
Format property of

"\L0000"

to just display the letter and the leading zeroes.

You can increment the ID in various ways. If this is a single user
database
then the simplest would be to use a form for data entry, and in its
Beforeinsert event put code like

Private Sub Form_BeforeInsert(Cancel as Integer)
Dim iNext As Integer
iNext = NZ(DMax("[ID]", "[tablename]")) + 1
If iNext = 10000 Then
Cancel = True
MsgBox "All ID's have been used, shut off the PC and go home", vbOKOnly
Else
Me![ID] = iNext
End If
End Sub

For multiuser databases you need to take precautions against having two
users
create new records at the same time - post back with details if you need
this.
--

John W. Vinson [MVP]



  #6  
Old January 8th, 2009, 09:41 PM posted to microsoft.public.access.tablesdbdesign
John W. Vinson
external usenet poster
 
Posts: 18,261
Default Incrementing a key value

On Thu, 8 Jan 2009 11:18:09 -0800, Eric Johnson
wrote:

"John W. Vinson" wrote:
Private Sub Form_BeforeInsert(Cancel as Integer)
Dim iNext As Integer
iNext = NZ(DMax("[ID]", "[tablename]")) + 1
If iNext = 10000 Then
Cancel = True
MsgBox "All ID's have been used, shut off the PC and go home", vbOKOnly
Else
Me![ID] = iNext
End If
End Sub


I was afraid it was going to get complex. How do I do the incrementing in a
table...no form? If I can't do this, I'll have to look for another solution
as there are no forms...embedded tables and reports only.

Eric Johnson


If you're doing data entry in tables... DON'T.

That is not their function, and they are of very, very limited utility. In
particular tables have no events, so you cannot program things like
increments.

What do you mean by "embedded tables"? How do you do your data entry? What's
your aversion to forms: they are *essential* to any production Access
application?
--

John W. Vinson [MVP]
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 08:11 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.