Table of contents

"(全部)"添加到组合框或列表框Add "(All)" to a Combo Box or List Box

office 365 dev account|上次更新日期: 2017/9/25
1 参与者

当您使用列表框或组合框中输入选择条件时,您可能希望能够指定的所有记录。AddAllToList 过程说明了如何在组合框的顶部添加(全部)项。When you use a list box or combo box to enter selection criteria, you might want to be able to specify all records. The AddAllToList procedure illustrates how to add an (All) entry at the top of a combo box.

利用 AddAllToList 过程,您必须设置为AddAllToList的组合框或列表框的行来源类型属性。To utilize the AddAllToList procedure, you must set the RowSourceType property of the combo box or list box to AddAllToList.

您可以指定以外(全部)可通过设置组合框或列表框中的标记添加到列表中的项。例如,您可以添加标记属性的值设置列表的顶部到1;<None>You can specify a different item than (All) to add to the list by setting the Tag property of the combo box or list box. For example, you can add to the top of the list by setting the value of the Tag property to 1;<None>.

Function AddAllToList(ctl As Control, lngID As Long, lngRow As Long, _ 
lngCol As Long, intCode As Integer) As Variant 

Static dbs As Database, rst As Recordset 
Static lngDisplayID As Long 
Static intDisplayCol As Integer 
Static strDisplayText As String 
Dim intSemiColon As Integer 

On Error GoTo Err_AddAllToList 
Select Case intCode 
Case acLBInitialize 
' See if function is already in use. 
If lngDisplayID <> 0 Then 
MsgBox "AddAllToList is already in use by another control!" 
AddAllToList = False 

Exit Function 
End If 

' Parse the display column and display text from Tag property. 
intDisplayCol = 1 
strDisplayText = "(All)" 
If ctl.Tag <> "" Then 
intSemiColon = InStr(ctl.Tag, ";") 
If intSemiColon = 0 Then 
intDisplayCol = Val(ctl.Tag) 
intDisplayCol = Val(Left(ctl.Tag, intSemiColon - 1)) 
strDisplayText = Mid(ctl.Tag, intSemiColon + 1) 

End If 
End If 

' Open the recordset defined in the RowSource property. 
Set dbs = CurrentDb 
Set rst = dbs.OpenRecordset(ctl.RowSource, dbOpenSnapshot) 

' Record and return the lngID for this function. 
lngDisplayID = Timer 
AddAllToList = lngDisplayID 

Case acLBOpen 
AddAllToList = lngDisplayID 

Case acLBGetRowCount 
' Return number of rows in recordset. 
On Error Resume Next 

AddAllToList = rst.RecordCount + 1 

Case acLBGetColumnCount 
' Return number of fields (columns) in recordset. 
AddAllToList = rst.Fields.Count 

Case acLBGetColumnWidth 
AddAllToList = -1 

Case acLBGetValue 
If lngRow = 0 Then 
If lngCol = intDisplayCol - 1 Then 
AddAllToList = strDisplayText 
AddAllToList = Null 
End If 

rst.Move lngRow - 1 
AddAllToList = rst(lngCol) 
End If 
Case acLBEnd 
lngDisplayID = 0 
End Select 

Exit Function 

MsgBox Err.Description, vbOKOnly + vbCritical, "AddAllToList" 
AddAllToList = False 
Resume Bye_AddAllToList 
End Function
© 2018 Microsoft