2016-03-11 40 views
0

我正在使用Access 2013數據庫,該數據庫將不同的電線杆輸入數據庫並與其他屬性鏈接。每個杆將有一個唯一的全球ID,爲了簡化工作,我想添加另一個更簡單的唯一ID。我希望這個字段在導入到數據庫中的新極點時自動填充。該ID會去如下:根據添加記錄的年份創建順序ID值

SAC(年) - (不斷升級的數字,不能是複印件)

前。 SAC16-20(這將是2016年進入數據庫的第20極)

ex。 SAC15-2536(將在2015年進入第2536杆)

如果任何人都可以幫助我生成一些代碼,使此自動填充ID字段的工作,我將不勝感激。

回答

1

使用Access版本2010和更高版本,您可以使用事件驅動的data macro生成順序標識。例如,假設你有一個名爲[poledata]的表。在設計視圖中打開它,並添加兩個字段:

alternate_id_seq   –  數字(長整型)
alternate_id   –  文本(20)

更改保存到您的表,然後切換到數據表視圖。

在訪問功能區中,切換到 「表格工具>表」 選項卡,然後單擊 「更改之前」

Ribbon.png

然後輸入下面的宏...

BeforeChange.png

...或將以下XML粘貼到宏編輯器窗口中

<?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> 

現在,當新行添加到表中時,[alternate_id_seq]和[alternate_id]列將自動填充。

Datasheet.png