使用 Column 屬性可以引用多列組合框或列表框中特定的列或列與行的組合。Variant 型,只讀。
expression.Column(Index, Row)
expression 必需。返回“應(yīng)用于”列表中的一個對象的表達(dá)式。
Index 必需,Long 型。范圍為 0 到 ColumnCount 屬性的設(shè)置值減 1 的長整型整數(shù)。
Row 可選,Variant 型。范圍為 0 到 ListCount 屬性值減 1 的整數(shù)。
該屬性設(shè)置只能通過宏或 Visual Basic 來使用,在“設(shè)計”視圖中不可用,并且在其他視圖中均為只讀。
用 0 引用第一列,用 1 引用第二列,依此類推。用 0 引用第一行,用 1 引用第二行,依此類推。例如在含有一列客戶 ID 和一列客戶名稱的列表框中,可以使用如下方式引用第二列、第五行的客戶名稱:
Forms!Contacts!Customers.Column(1, 4)
可以使用 Column 屬性將組合框或列表框的內(nèi)容指定給另一控件,如文本框。例如,若要將文本框的 ControlSource 屬性設(shè)為列表框第二列中的值,可以使用以下表達(dá)式:
=Forms!Customers!CompanyName.Column(1)
如果引用了組合框或列表框中的列,但用戶未做選擇,則 Column 屬性設(shè)置將為 Null。可以使用 IsNull 函數(shù)來確定是否進(jìn)行了選擇,示例如下:
If IsNull(Forms!Customers!Country)
Then MsgBox "No selection."
End If
注釋 若要確定組合框或列表框有多少列,可以查看 ColumnCount 屬性設(shè)置。
以下示例使用 Column 屬性和 ColumnCount 屬性來打印列表框中選定的值:
Public Sub Read_ListBox()
Dim intNumColumns As Integer
Dim intI As Integer
Dim frmCust As Form
Set frmCust = Forms!frmCustomers
If frmCust!lstCustomerNames.ItemsSelected.Count > 0 Then
' Any selection?
intNumColumns = frmCust!lstCustomerNames.ColumnCount
Debug.Print "The list box contains "; intNumColumns; _
IIf(intNumColumns = 1, " column", " columns"); _
" of data."
Debug.Print "The current selection contains:"
For intI = 0 To intNumColumns - 1
' Print column data.
Debug.Print frmCust!lstCustomerNames.Column(intI)
Next intI
Else
Debug.Print "You haven't selected an entry in the " _
& "list box."
End If
Set frmCust = Nothing
End Sub