从1970年至今的访问表的年份下拉列表

时间:2018-11-26 15:21:12

标签: ms-access access-vba access

在Access中,我试图使用查找向导创建一个名为“ Signed Year”的表字段,该字段必须从1970年至今(当前为1970-2018年)。但是,我意识到这并不是最佳选择,因为我不得不不时地手动添加一年。

是否存在某种代码可以自动生成此类范围?

非常感谢!

1 个答案:

答案 0 :(得分:1)

您可以为此使用回调列表。这是15年前的ultimo清单代码:

Public Function ListUltimoYears( _
  ctl As Control, _
  lngId As Long, _
  lngRow As Long, _
  lngCol As Long, _
  intCode As Integer) _
  As Variant

  ' Period for listing dates.
  Const cintYears               As Integer = 15

  ' 2014-09-24. Cactus Data ApS, CPH.

  Static datFirstDate   As Date
  Static strFormat      As String
  Static intRows        As Integer

  Dim datDate           As Date
  Dim varValue          As Variant

  Select Case intCode
    Case acLBInitialize
      datDate = Date
      datFirstDate = DateSerial(Year(datDate), 12, 31)
      intRows = 1 + cintYears
      strFormat = ctl.Format
      varValue = True               ' True to initialize.
    Case acLBOpen
      varValue = Timer              ' Autogenerated unique ID.
    Case acLBGetRowCount            ' Get rows.
      varValue = intRows            ' Set number of rows.
    Case acLBGetColumnCount         ' Get columns.
      varValue = 1                  ' Set number of columns.
    Case acLBGetColumnWidth         ' Get column width.
      varValue = -1                 ' Use default width.
    Case acLBGetValue               ' Get the data for each row.
      varValue = DateAdd("yyyy", lngRow, datFirstDate)
    Case acLBGetFormat              ' Format the data.
      varValue = strFormat          ' Use format of control.
    Case acLBEnd
      ' Do something when form with listbox closes or
      ' listbox is requeried.
  End Select

  ' Return Value.
  ListUltimoYears = varValue

End Function

使用以下命令对其进行修改:

Const cintYears               As Integer = 15

使用变量:

Dim intYears = DateDiff("yyyy", #1/1/1970#, Date)

要在表单中使用它,请设置组合框的属性 RowSourceType :ListUltimoYears