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

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

Functionfunctionname (fldAs ControlidAs VariantrowAs VariantcolAs VariantcodeAs Variant) As Variant

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

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

注意

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

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

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

Access 为 acLBInitializeacLBOpenacLBGetRowCountacLBGetColumnCount 调用用户定义函数一次。 它初始化用户定义函数,打开查询,并确定行数和列数。

Access 为 acLBGetColumnWidth 调用用户定义函数两次,一次用于确定列表框或组合框的总宽度,另一次用于设置列宽。

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

Access 在窗体关闭或每次查询列表框或组合框时,调用 acLBEnd 的用户定义函数。

每当特定值 ((例如需要) 的列数)时,返回 Null 或任何无效值都会导致 Access 停止使用该代码调用用户定义的函数。

提示

使用示例中的 Select Case 代码结构作为自己的 RowSourceType 属性用户定义函数的模板。

示例

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

Public Function ListMondays(fld As Control, id As Variant, _
    row As Variant, col As Variant, code As Variant) _ 
    As Variant 

    Dim Offset      As Integer
    Dim WeekdayDate As Date 
 
    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. 
            Offset = Abs((9 - Weekday(Date)) Mod 7) 
            WeekdayDate = DateAdd("d", _
                Offset + 7 * row, Date) 
            ListMondays = Format(WeekdayDate, _
                "mmmm d") 
    End Select 

End Function

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

Public Function ListMDBs(fld As Control, id As Variant, _ 
    row As Variant, col As Variant, code As Variant) _
    As Variant 
    
    Static dbs(127) As String
    Static 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

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。