Primary Key auto generated from fields of same record

A

AnotherNewbie

In Access 2003 I have a Table [tblLocations]. I want the Primary Key in field
[LocId] to be auto generated by combining [BldgId],[VtyId] and [RmNumId] of
the same Record. I am assuming that I have to write this in code but don't
know how.
Here's what I have so far

Database is 'SRL.mdb' I have several tables that have PKs that lead to the
table [tblLocations].
In that table have these fields:

LocId BldgId VtyId RmNumId
(datatype text) (datatype text) (datatype text) (datatype text)
(Blank right now) 1 3 078

I want the Primarykey to equal 13078
I don't think I can go to tblLocations.LocId propertys DefaultValue and
write it there.
I think I have to go to the Database window and click 'Modules' and then the
'New' icon which brings up Visual Basic. I would type:
PrivateSub
(the code)
EndSub
Don't know how to Procede.
 
D

Dirk Goldgar

AnotherNewbie said:
In Access 2003 I have a Table [tblLocations]. I want the Primary Key in
field
[LocId] to be auto generated by combining [BldgId],[VtyId] and [RmNumId]
of
the same Record. I am assuming that I have to write this in code but don't
know how.
Here's what I have so far

Database is 'SRL.mdb' I have several tables that have PKs that lead to the
table [tblLocations].
In that table have these fields:

LocId BldgId VtyId RmNumId
(datatype text) (datatype text) (datatype text) (datatype text)
(Blank right now) 1 3 078

I want the Primarykey to equal 13078
I don't think I can go to tblLocations.LocId propertys DefaultValue and
write it there.
I think I have to go to the Database window and click 'Modules' and then
the
'New' icon which brings up Visual Basic. I would type:
PrivateSub
(the code)
EndSub
Don't know how to Procede.


Are you aware that a table's primary key can consist of multiple fields?
You don't need to create a separate field for the purpose -- you can select
the three fields in table design view, and then click on the "primary key"
button to define that combination of fields as the primary key. I think
that would be simpler and more reliable than trying to use code to generate
your own separate field. If you need to, for display purposes, you could
create a calculated field in a query or controlsource expression that would
concatenate the three fields into a single text string.
 
H

Hans Up

AnotherNewbie said:
LocId BldgId VtyId RmNumId
(datatype text) (datatype text) (datatype text) (datatype text)
(Blank right now) 1 3 078

I want the Primarykey to equal 13078

The primary key must uniquely identify each row. How does your plan
address the following combinations of BldgId, VtyId, and RmNumId?

LocId BldgId VtyId RmNumId
111015 1 11 015
111015 11 1 015
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top