根据年份自动生成字段内容

Automatically generating field contents based on Year

我正在尝试创建一个遵循以下格式的自动生成的字段:

TREQ-YY-NNNN

其中 YY 是提交的年份,NNNN 是当年提交的第 nth 个表单。例如:

TREQ-15-0001
TREQ-15-0002
TREQ-15-0003
TREQ-15-0004
TREQ-15-0005
TREQ-16-0001
TREQ-16-0002

我一直在尝试使用自动编号字段的格式 属性,但在使用 TREQ-"yy"-"0000 掩码时出现奇怪的行为。我最终得到像 TREQ-1899-01、TREQ-1900-02、TREQ-1900-03 这样的值。

有没有办法让我获得我正在寻找的编号格式或类似的东西?我是 Access 的新手,我仍在努力学习公式的正确语法。

在您的表单中插入一个新的 Text Box,并在 Data 选项卡的 Control Source 字段 属性 中,插入以下表达式:

="TREQ-" & Right(Year([DateSubmitted]),2) & "-" & Format([ID],"0000")

其中 [DateSubmitted] 是您的日期字段,[ID] 是您的自动编号字段。

或者,您可以像这样创建一个本地查询:

SELECT Table1.ID, Table1.DateSubmitted, "TREQ-" & Right(Year([DateSubmitted]),2) & "-" & Format([ID],"0000") AS TreqNum
FROM Table1;

希望这会有所帮助

您可以使用 Before Change data macro 自动将键值分配给新的,而不是使用自动编号字段(它不会在新的一年开始时将其自身重置为 1)记录插入 table。宏看起来像这样:

<?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">yy</Argument>
              <Argument Name="Value">Right(Year(Date()),2)</Argument>
            </Action>
            <Comment>Set default value in case no records found:</Comment>
            <Action Name="SetLocalVar">
              <Argument Name="Name">newSeq</Argument>
              <Argument Name="Value">1</Argument>
            </Action>
            <LookUpRecord>
              <Data Alias="z">
                <Query>
                  <References>
                    <Reference Source="tblTREQ" />
                  </References>
                  <Results>
                    <Property Source="tblTREQ" Name="KeyField" />
                  </Results>
                  <Ordering>
                    <Order Direction="Descending" Source="tblTREQ" Name="KeyField" />
                  </Ordering>
                </Query>
                <WhereCondition>[KeyField] Like &quot;TREQ-&quot; &amp; [yy] &amp; &quot;-*&quot;</WhereCondition>
              </Data>
              <Statements>
                <Action Name="SetLocalVar">
                  <Argument Name="Name">newSeq</Argument>
                  <Argument Name="Value">Val(Right([z].[KeyField],4))+1</Argument>
                </Action>
              </Statements>
            </LookUpRecord>
            <Action Name="SetField">
              <Argument Name="Field">KeyField</Argument>
              <Argument Name="Value">&quot;TREQ-&quot; &amp; [yy] &amp; &quot;-&quot; &amp; Right(&quot;0000&quot; &amp;
              [newSeq],4)</Argument>
            </Action>
          </Statements>
        </If>
      </ConditionalBlock>
    </Statements>
  </DataMacro>
</DataMacros>

实际上,您不应该为此使用自动编号字段 - 它们旨在让系统生成一个唯一编号,而无需用户干预(只是为了确保每行都有一个唯一的 ID 来引用;它就像 GUID 的前身),通常它们不会发布。通常,他们只会 "number up" 并且不知道随着年份的变化而重新设置。 (这就是 1900 从 2 开始的原因)。

要生成您自己的(字符串!)密钥,您需要编写在 post 上生成密钥的代码。