Description
The Visual Basic function you create must accept five arguments. The first argument must be declared as a control and the remaining arguments as Variants. The function itself must return a Variant.
Syntax Function functionname(fld As Control, id As Variant, row As Variant, col As Variant,Argument | Description |
fld | A control variable that refers to the list box or combo box being filled. |
id | A unique value that identifies the control being filled. This is useful when you want to use the same user-defined function for more than one list box or combo box and must distinguish between them. (The example sets this variable to the value of the Timer function.) |
row | The row being filled (zero-based). |
col | The column being filled (zero-based). |
code | An intrinsic constant that specifies the kind of information being requested. |
Constant | Meaning | Function returns |
acLBInitialize | Initialize | Nonzero if the function can fill the list; False (0) or Null otherwise. |
acLBOpen | Open | Nonzero ID value if the function can fill the list; False or Null otherwise. |
acLBGetRowCount | Number of rows | Number of rows in the list (can be zero); –1 if unknown. |
acLBGetColumnCount | Number of columns | Number of columns in the list (can't be zero); must match the property sheet value. |
acLBGetColumnWidth | Column width | Width (in twips) of the column specified by the col argument; –1 to use the default width. |
acLBGetValue | List entry | List entry to be displayed in the row and column specified by the row and col arguments. |
acLBGetFormat | Format string | Format string to be used to format the list entry displayed in the row and column specified by the row and col arguments; –1 to use the default format. |
acLBEnd | End (the last call to a user-defined function always uses this value) | Nothing. |
acLBClose | (Not used) | Not used. |
See Also Static statement.
Example The following user-defined function returns a list of the next four Mondays following today's date. To call this function from a list box control, enter ListMondays as the RowSourceType property setting and leave the RowSource property setting blank.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
The next example uses a static array to store the names of the databases in the current directory. To call this function, enter ListMDBs as the RowSourceType property setting and leave the RowSource property setting blank.
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.
ReturnVal = Timer ' Generate unique ID for control.
Case acLBGetRowCount ' Get number of rows.
ReturnVal = Entries
Case acLBGetColumnCount ' Get number of columns.
ReturnVal = 1
Case acLBGetColumnWidth ' Column width.
ReturnVal = -1 ' -1 forces use of default width.
Case acLBGetValue ' Get data.
ReturnVal = dbs(row)
Case acLBEnd ' End.
Erase dbs
End Select
ListMDBs = ReturnVal
End Function