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



在Access中,我试图使用查找向导创建一个名为"签名年份"的表字段,该字段必须从1970年到现在(目前为1970-2018年(。然而,我意识到这不是最佳的,因为我不得不不时地手动添加一年。

有没有某种代码可以自动生成这样的范围?

非常感谢!

您可以为此使用回调列表。以下是15年前最后一次上市的代码:

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

最新更新