Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

ms access - Create sequential ID value based on the year that a record is added

I am working on an Access 2013 database that will have different utility poles entered into the database and linked with other attributes. Each pole will have a unique global ID, and to simplify working I would like to add another unique ID that is more simple. I would like this field auto populated when a new pole in imported into the database. The ID would go as follows:

SAC(year)-(Escalating number, cannot be a duplicate)

ex. SAC16-20 (This would be the 20th pole entered into the database in 2016)

ex. SAC15-2536 (Would be the 2536th pole entered in 2015)

If anyone could help me generate some code to make this auto populate ID field work I would greatly appreciate it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With Access versions 2010 and later you can use an event-driven data macro to generate the sequential ID. For example, say you have a table named [poledata]. Open it in Design View and add two fields:

alternate_id_seq  –  Numeric (Long Integer)
alternate_id  –  Text(20)

Save the changes to your table and then switch to Datasheet View.

In the Access ribbon, switch to the "Table Tools > Table" tab and click "Before Change"

Ribbon.png

then enter the following macro ...

BeforeChange.png

... or paste the following XML into the macro editor window

<?xml version="1.0" encoding="utf-16" standalone="no"?>
<DataMacros xmlns="http://schemas.microsoft.com/office/accessservices/2009/11/application">
    <DataMacro Event="BeforeChange">
        <Statements>
            <ConditionalBlock>
                <If>
                    <Condition>[IsInsert]</Condition>
                    <Statements>
                        <Action Name="SetLocalVar">
                            <Argument Name="Name">next_seq</Argument>
                            <Argument Name="Value">1</Argument>
                        </Action>
                        <Action Name="SetLocalVar">
                            <Argument Name="Name">prefix</Argument>
                            <Argument Name="Value">&quot;SAC&quot; &amp; Year(Date()) Mod 100 &amp; &quot;-&quot;</Argument>
                        </Action>
                        <LookUpRecord>
                            <Data Alias="pd">
                                <Query>
                                    <References>
                                        <Reference Source="poledata" Alias="pd" />
                                    </References>
                                    <Results>
                                        <Property Source="pd" Name="alternate_id_seq" />
                                    </Results>
                                    <Ordering>
                                        <Order Direction="Descending" Source="pd" Name="alternate_id_seq" />
                                    </Ordering>
                                </Query>
                                <WhereCondition>[pd].[alternate_id] Like [prefix] &amp; &quot;*&quot;</WhereCondition>
                            </Data>
                            <Statements>
                                <Action Name="SetLocalVar">
                                    <Argument Name="Name">next_seq</Argument>
                                    <Argument Name="Value">[pd].[alternate_id_seq]+1</Argument>
                                </Action>
                            </Statements>
                        </LookUpRecord>
                        <Action Name="SetField">
                            <Argument Name="Field">alternate_id_seq</Argument>
                            <Argument Name="Value">[next_seq]</Argument>
                        </Action>
                        <Action Name="SetField">
                            <Argument Name="Field">alternate_id</Argument>
                            <Argument Name="Value">[prefix] &amp; [next_seq]</Argument>
                        </Action>
                    </Statements>
                </If>
            </ConditionalBlock>
        </Statements>
    </DataMacro>
</DataMacros>

Now when new rows are added to the table the [alternate_id_seq] and [alternate_id] columns will be populated automatically.

Datasheet.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...