【问题标题】:Automatically generating field contents based on Year根据年份自动生成字段内容
【发布时间】:2015-11-30 20:40:02
【问题描述】:

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

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

我一直在尝试使用 AutoNumber 字段的 Format 属性,但在使用 TREQ-"yy"-"0000 掩码时出现了奇怪的行为。我最终得到了 TREQ-1899-01、TREQ-1900-02、TREQ-1900-03 之类的值。

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

【问题讨论】:

    标签: ms-access ms-access-2013


    【解决方案1】:

    您可以使用更改前data macro 来自动将键值分配给新记录,而不是使用自动编号字段(它不会在新年开始时将自身重置为data macro)。桌子。宏看起来像这样:

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

    【讨论】:

    • 谢谢!直到现在才有机会对此进行测试,但看起来这会奏效!
    【解决方案2】:

    实际上,您不应该为此使用自动编号字段 - 它们旨在由系统生成一个唯一编号,而无需用户干预(只是为了确保每一行都有一个唯一的要引用的 ID;它就像 GUID 的前身),通常不会发布。通常,他们会简单地“编号”,并且没有概念随着年份的变化而重新设置。 (这就是为什么 1900 从 2 开始)。

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

    【讨论】:

      【解决方案3】:

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

      ="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;
      

      希望这会有所帮助

      【讨论】:

        猜你喜欢
        • 2020-03-30
        • 2016-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-11
        • 1970-01-01
        相关资源
        最近更新 更多