access类似DCount功能的扩展函数-统计不同值的数量
- 2019-10-11 16:28:00
- Allen Browne 翻译
- 6265
access中,使用 DCount 函数可以统计特定记录集内的总数。但是DCount还有些不足:
1. 内置函数DCount()无法计算不同值的数量;
2. Access中的域聚合函数效率低下。
这里给出一个自定义的Ecount函数,它提供了一个额外的参数,可以计算不同的值
Public Function ECount(Expr As String, Domain As String, Optional Criteria As String, Optional bCountDistinct As Boolean) As Variant On Error GoTo Err_Handler 'Purpose: Enhanced DCount() function, with the ability to count distinct. 'Return: Number of records. Null on error. 'Arguments: Expr = name of the field to count. Use square brackets if the name contains a space. ' Domain = name of the table or query. ' Criteria = any restrictions. Can omit. ' bCountDistinct = True to return the number of distinct values in the field. Omit for normal count. 'Notes: Nulls are excluded (whether distinct count or not.) ' Use "*" for Expr if you want to count the nulls too. ' You cannot use "*" if bCountDistinct is True. 'Examples: Number of customers who have a region: ECount("Region", "Customers") ' Number of customers who have no region: ECount("*", "Customers", "Region Is Null") ' Number of distinct regions: ECount("Region", "Customers", ,True) Dim db As DAO.Database Dim rs As DAO.Recordset Dim strSql As String 'Initialize to return Null on error. ECount = Null Set db = DBEngine(0)(0) If bCountDistinct Then 'Count distinct values. If Expr <> "*" Then 'Cannot count distinct with the wildcard. strSql = "SELECT " & Expr & " FROM " & Domain & " WHERE (" & Expr & " Is Not Null)" If Criteria <> vbNullString Then strSql = strSql & " AND (" & Criteria & ")" End If strSql = strSql & " GROUP BY " & Expr & ";" Set rs = db.OpenRecordset(strSql) If rs.RecordCount > 0& Then rs.MoveLast End If ECount = rs.RecordCount 'Return the number of distinct records. rs.Close End If Else 'Normal count. strSql = "SELECT Count(" & Expr & ") AS TheCount FROM " & Domain If Criteria <> vbNullString Then strSql = strSql & " WHERE " & Criteria End If Set rs = db.OpenRecordset(strSql) If rs.RecordCount > 0& Then ECount = rs!TheCount 'Return the count. End If rs.Close End If Exit_Handler: Set rs = Nothing Set db = Nothing Exit Function Err_Handler: MsgBox Err.Description, vbExclamation, "ECount Error " & Err.Number Resume Exit_Handler End Function
如下图,可以计算字段中不重复的值,如果参数为false,即功能跟Dcount是一样的。
Access数据库自身
- office课程播放地址及课程明细
- Excel Word PPT Access VBA等Office技巧学习平台
- 将( .accdb) 文件格式数据库转换为早期版本(.mdb)的文件格式
- 将早期的数据库文件格式(.mdb)转换为 (.accdb) 文件格式
- KB5002984:配置 Jet Red Database Engine 数据库引擎和访问连接引擎以阻止对远程数据库的访问(remote table)
- Access 365 /Access 2019 数据库中哪些函数功能和属性被沙箱模式阻止(如未启动宏时)
- Access Runtime(运行时)最全的下载(2007 2010 2013 2016 2019 Access 365)
Access Activex第三方控件
- Activex控件或Dll 在某些电脑无法正常注册的解决办法(regsvr32注册时卡住)
- office使用部分控件时提示“您没有使用该ActiveX控件许可的问题”的解决方法
- RTF文件(富文本格式)的一些解析
- Access树控件(treeview) 64位Office下出现横向滚动条不会自动定位的解决办法
- Access中国树控件 在win10电脑 节点行间距太小的解决办法
- EXCEL 2019 64位版(Office 2019 64位)早就支持64位Treeview 树控件 ListView列表等64位MSCOMMCTL.OCX控件下载
- VBA或VB6调用WebService(直接Post方式)并解析返回的XML
Access ADP Sql Server等
- 早期PB程序连接Sqlserver出现错误
- MMC 不能打开文件C:/Program Files/Microsoft SQL Server/80/Tools/Binn/SQL Server Enterprise Manager.MSC 可能是由于文件不存在,不是一个MMC控制台,或者用后来的MMC版
- sql server连接不了的解决办法
- localhost与127.0.0.1区别
- Roych的浅谈数据库开发系列(Sql Server)
- sqlserver 自动备份对备份目录没有存取权限的解决办法
- 安装Sql server 2005 express 和SQLServer2005 Express版企业管理器 SQLServer2005_SSMSEE
文章分类
联系我们
联系人: | 王先生 |
---|---|
Email: | 18449932@qq.com |
QQ: | 18449932 |
微博: | officecn01 |