全部显示

RowSourceType 属性(用户定义函数) - 代码参数值

   

创建的 Visual Basic 函数必须接受五个参数。第一个参数必须声明为控件,其他参数声明为 Variant 类型。函数本身的返回值必须是 Variant 值。

functionname (fld As Control, id As Variant, row As Variant, col As Variant, code As Variant) As Variant

Function 过程有下列五个必选参数:

参数 说明
fld 控件变量,用于引用要填充的列表框或者组合框
id 唯一值,用于标识要填充的控件。如果要在多个列表框或者组合框中使用同一个用户定义函数,并且要正确区分这些函数时可以使用该参数。(示例中将变量值设为 Timer 函数)
row 要填充的行(从 0 开始计算)
col 要填充的列(从 0 开始计算)
code 一个固有常量,用于指定必需的信息类型。

注意   由于要将数据项插入到列表中,Microsoft Access 需要多次调用用户定义函数才能完成,因此,常常需要保留每次调用的信息以供下次调用时使用。所以要进行这种操作,最好是使用 Static 变量。

Microsoft Access 调用用户定义函数的过程是在 code 参数中指定所需的不同值,然后重复调用该自定义函数。code 参数可以使用下列固有常量:

常量 含义 函数返回值
acLBInitialize 初始化 如果该函数可以对列表进行填充则返回非零值;否则返回 False (0) 或 Null
acLBOpen 打开 如果该函数可以对列表进行填充则返回非零 ID 值;否则返回 FalseNull
acLBGetRowCount 行数 列表中的行数(可以为 0);如果未知则为 -1 。
acLBGetColumnCount 列数 列表中的列数(不能为 0);必须与属性表中的值匹配。
acLBGetColumnWidth 列宽度 col 参数指定的列宽(以为单位);如果指定为 -1 则使用默认宽度。
acLBGetValue 列表输入项 在由 row 参数和 col 参数指定的行和列中显示的列表输入项。
acLBGetFormat 格式设置字符串 指定行和列的列表输入项中显示内容使用的格式,行数和列数由 row 参数和 col 参数指定;如果指定为 -1 则使用默认格式。
acLBEnd 结束(对用户定义函数的最后一次调用,通常使用该参数)。 无。
acLBClose (不用) 不用。

对于 acLBInitializeacLBOpenacLBGetRowCountacLBGetColumnCount 参数,Microsoft Access 只调用一次该用户定义函数,为用户定义函数进行初始化,打开所需的查询,然后决定列表中的行数和列数。

对于 acLBGetColumnWidth 参数,Microsoft Access 调用该用户定义函数两次,一次是决定列表框或者组合框的总宽度,第二次是对列宽进行设置。

对于 acLBGetValueacLBGetFormat 参数,Microsoft Access 为获取列表输入项和字符串变量的格式设置,而调用用户定义函数的次数,取决于输入项的个数、用户对滚动条的设置和其他的一些相关因素。

在关闭窗体或每次需要对列表框或者组合框进行查询时,Microsoft Access 对 acLBEnd 参数调用该用户定义函数。

每次需要使用特定值时(例如列数),如果返回的是 Null 或者其他任意无效值,Microsoft Access 则会中止对包含该参数的用户定义函数的调用。

提示   可以将示例中的 Select Case 代码结构作为模板,并在应用程序的 RowSourceType 属性(用户定义函数)中使用。

示例

下列用户定义函数将返回从今天的日期开始往后四个星期一的列表。若要从列表框控件中调用该用户定义函数,可以将 RowSourceType 属性设为 ListMondays,并将 RowSource 属性留空。

Function ListMondays(fld As Control,id As Variant, _
    row As Variant,col As Variant,code As Variant) _
     As Variant
    Dim intOffset As Integer
    Select Case code
        Case acLBInitialize            ' Initialize.
            ListMondays = True
        Case acLBOpen                    ' Open.
            ListMondays = Timer        ' Unique ID.
        Case acLBGetRowCount            ' Get rows.
            ListMondays = 4
        Case acLBGetColumnCount    ' Get columns.
            ListMondays = 1
        Case acLBGetColumnWidth    ' Get column width.
            ListMondays = -1            ' Use default width.
        Case acLBGetValue                ' Get the data.
            intOffset = Abs((9 - Weekday(Now))Mod 7)
            ListMondays = Format(Now() + _
             intOffset + 7 * row,"mmmm d")
    End Select
End Function

下面的示例将使用静态数组来存储当前目录中所有数据库的名称。如果要调用该用户定义函数,可以将 RowSourceType 属性设为 ListMDBs,并将 RowSource 属性留空。

Function ListMDBs(fld As Control, id As Variant, _
     row As Variant, col As Variant, _
     code As Variant) As Variant
    Static dbs(127) As String, Entries As Integer
    Dim ReturnVal As Variant
    ReturnVal = Null
    Select Case code
        Case acLBInitialize                ' Initialize.
            Entries = 0
            dbs(Entries ) = Dir("*.MDB")
            Do Until dbs(Entries) = "" Or Entries >= 127
                Entries = Entries+1
                dbs(Entries) = Dir
            Loop
            ReturnVal = Entries
        Case acLBOpen                        ' Open.
            ' Generate unique ID for control.
            ReturnVal = Timer
        Case acLBGetRowCount            ' Get number of rows.
            ReturnVal = Entries
        Case acLBGetColumnCount    ' Get number of columns.
            ReturnVal = 1
        Case acLBGetColumnWidth    ' Column width.
            ' -1 forces use of default width.
            ReturnVal = -1
        Case acLBGetValue                    ' Get data.
            ReturnVal = dbs(row)
        Case acLBEnd                        ' End.
            Erase dbs
    End Select
    ListMDBs = ReturnVal
End Function