向组合框或列表框添加“(All)”-杨雪
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


向组合框或列表框添加“(All)”

发表时间:2021/11/15 8:45:14 评论(0) 浏览(2329)  评论 | 加入收藏 | 复制
   
摘 要:向组合框或列表框添加“(All)”
正 文:
使用列表框或组合框输入选择条件时,您可能希望能够指定所有记录。 AddAllToList 过程说明了如何在组合框顶部添加 (All) 项。

要使用 AddAllToList 过程,必须将组合框或列表框的 RowSourceType 属性设置为 AddAllToList。

可以通过设置组合框或列表框的 Tag 属性来指定要添加到列表中的 (All) 以外的项。 例如,可以通过将 Tag 属性的值设置为 添加到列表 <None> 顶部 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) 
Else 
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 
 
rst.MoveLast 
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 
Else 
AddAllToList = Null 
End If 
Else 
 
rst.MoveFirst 
rst.Move lngRow - 1 
AddAllToList = rst(lngCol) 
End If 
Case acLBEnd 
lngDisplayID = 0 
rst.Close 
End Select 
 
Bye_AddAllToList: 
Exit Function 
 
Err_AddAllToList: 
MsgBox Err.Description, vbOKOnly + vbCritical, "AddAllToList" 
AddAllToList = False 
Resume Bye_AddAllToList 
End Function

Access软件网交流QQ群(群号:198465573)
 
 相关文章
【access源码示例】精确筛选\精确查询,文本框筛选列表框数据\...  【红尘如烟  2012/5/6】
【access源码示例】模糊筛选\模糊查询,文本框筛选列表框数据\...  【红尘如烟  2012/5/12】
列表框绑定列查找指定内容并选中  【MDZZ  2017/12/11】
【Access列表框知识分享】列表框设置多选后,默认值设置无效  【麥田  2020/4/17】
利用列表框(listbox)进行多选  【欧志华  2020/8/12】
【Access列表框多级联动查询示例】院校查询多级联动查询的示例,...  【欧志华  2020/12/9】
【转载】分享国外连续型子窗体代替列表框示例  【金宇  2021/1/2】
在组合框和列表框中分级显示数据  【张志  2021/1/21】
常见问答
技术分类
相关资源
文章搜索
关于作者

杨雪

文章分类

文章存档

友情链接