| | |
Unique field/primary key for Access
![]() |
•
•
Join Date: Oct 2005
Posts: 1
Reputation:
Solved Threads: 0
I need to create a field in Access that included both text and the autonumber feature. We are a non-profit hospital organization and we have multiple locations so we want to create an ID field that is unique by location and still uses the auto numbering feature to generate the ID field. I think we need to create this field on the form versus on the table, and we'd make a change in the preceding letters for each location form.
While I'm certain one can accomplish this using a VB formula, but I don't know how to do this. I need the results of the field to look something like the following:
SJ000001
SJ000002
SJ000003 ...
or
SJ000099
SJ000100
SJ000101 ...
etc. etc.
If someone could please assist in how to do this within Access...I'd me most grateful.
While I'm certain one can accomplish this using a VB formula, but I don't know how to do this. I need the results of the field to look something like the following:
SJ000001
SJ000002
SJ000003 ...
or
SJ000099
SJ000100
SJ000101 ...
etc. etc.
If someone could please assist in how to do this within Access...I'd me most grateful.
This can be accomplioshed with VBA. Basically, if the numbers are numerical, you can use VBA to count the records, add one to the new record, and concantinate (connect) the text string. If you need more help, I'd be glad to assit a non-profit orginization. www.gabrielvilla.com I do pro-bono
•
•
Join Date: Nov 2005
Posts: 134
Reputation:
Solved Threads: 10
There is another way of doing this without code but it assumes one thing:
- The ID numbers cannot be repeated between locations, i.e. SJ000001 and LA000001 cannot exist.
What you do is use the Access AutoNumber to create the unique ID for each record and for display purposes only you concatenate the location with the ID. The code in the Control source property of the field would look simething like this:
=[Location] & Format([ID], "000000")
However, if you need to have similar numbers across locations, SJ000001 AND LA000001, then you need to run a query to find out how many records there are for the specified location before using the technique posted above to add one to the last number.
If, on top of that, you can delete records for a location and therefore have gaps in the sequence you need to find out what the value of the last number stored is. If that is the case I can send you some code but it will only take up space here if that's not the case.
Hope this helps
Yomet
- The ID numbers cannot be repeated between locations, i.e. SJ000001 and LA000001 cannot exist.
What you do is use the Access AutoNumber to create the unique ID for each record and for display purposes only you concatenate the location with the ID. The code in the Control source property of the field would look simething like this:
=[Location] & Format([ID], "000000")
However, if you need to have similar numbers across locations, SJ000001 AND LA000001, then you need to run a query to find out how many records there are for the specified location before using the technique posted above to add one to the last number.
If, on top of that, you can delete records for a location and therefore have gaps in the sequence you need to find out what the value of the last number stored is. If that is the case I can send you some code but it will only take up space here if that's not the case.
Hope this helps
Yomet
![]() |
Similar Threads
- find primary key of post (MySQL)
- Random unique primary key - is this possible? (MySQL)
- generate unique primary key (MySQL)
- Need Help Accessing a Primary key MS Access in SQL to print out informatio. (MS Access and FileMaker Pro)
- HELP! Need someone that Knows SQL to tell Me How to Access a Primary Key (C)
- Program Problem with a select statement to access Data base (C)
- Need to make program access a data base primary key number entered by user (C)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: So close.....................................................................
- Next Thread: VB query
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





