VBA Access, Recordset.Recordcount

The ADODB.Recordset object has a recordcount property. This property returns the number of records in the recordset. This property may or may not work correctly depending on the cursor type chosen. For the following cursor types this property will work correctly:

  • adOpenKeyset
  • adOpenStatic

but for the following cursor types it will always return “-1”:

  • adOpenDynamic
  • adOpenForwardOnly

This article uses the ADODB.Recordset object. In order to use this object you may need to add reference to ADODB object library. For more information on this topic please see the link below:

  • VBA Access Recordset, Object Library

Example:

consider the following table:
Table

Both example codes below will return the number of records in the table:

Sub Example1()
Dim objRecordset As ADODB.Recordset
Set objRecordset = New ADODB.Recordset
Dim i As Integer
Dim value As Variant

'initated recordset obejct
objRecordset.ActiveConnection = CurrentProject.Connection
Call objRecordset.Open("MyTable1", , adOpenKeyset)
MsgBox (objRecordset.RecordCount)
End Sub

and

Sub Example2()
Dim objRecordset As ADODB.Recordset
Set objRecordset = New ADODB.Recordset
Dim i As Integer
Dim value As Variant

'initated recordset obejct
objRecordset.ActiveConnection = CurrentProject.Connection
Call objRecordset.Open("MyTable1", , adOpenStatic)
MsgBox (objRecordset.RecordCount)
End Sub

Result:
Result

But the codes below will return “-1”:

Sub Example3()
Dim objRecordset As ADODB.Recordset
Set objRecordset = New ADODB.Recordset
Dim i As Integer
Dim value As Variant

'initated recordset obejct
objRecordset.ActiveConnection = CurrentProject.Connection
Call objRecordset.Open("MyTable1", , adOpenDynamic)
MsgBox (objRecordset.RecordCount)
End Sub

and

Sub Example4()
Dim objRecordset As ADODB.Recordset
Set objRecordset = New ADODB.Recordset
Dim i As Integer
Dim value As Variant

'initated recordset obejct
objRecordset.ActiveConnection = CurrentProject.Connection
Call objRecordset.Open("MyTable1", , adOpenForwardOnly)
MsgBox (objRecordset.RecordCount)
End Sub

Result:
Result 2

Note: Another method for getting the record count that will always work has been explained in the article below:

  • VBA Access Get Record Count

You can download the file and code used in this article from the link below:

  • RecordCount.Zip

See also:

  • Microsoft MSDN: Cursor Type Property (ADO)
  • VBA Access Get Record Count

If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website聽 聽www.software-solutions-online.com

聚圣源自动化科技公司起名大全鬼吹灯有声小说周建龙中式设计找四合茗苑李春嫒首饰品牌起名王囡囡天官赐福免费观看完整版动漫代劳蚍蜉撼树是什么意思足球中国vs韩国红酒公司如何起名起名免费网周易起名免费汽修公司起名写给未来自己的一封信求全责备的意思et下载bulwark在线天堂www在线......刘宝宝起名宝典食品起名起名 生辰八字光环致远星攻略正常起搏点名词解释逍遥刀仙我的邻居睡不着营销策划公司名字起名大全韩国人起名女孩2018属狗小孩起名肉食店起名大全属鼠适合起啥名字淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻让美丽中国“从细节出发”清明节放假3天调休1天男孩疑遭霸凌 家长讨说法被踢出群国产伟哥去年销售近13亿网友建议重庆地铁不准乘客携带菜筐雅江山火三名扑火人员牺牲系谣言代拍被何赛飞拿着魔杖追着打月嫂回应掌掴婴儿是在赶虫子山西高速一大巴发生事故 已致13死高中生被打伤下体休学 邯郸通报李梦为奥运任务婉拒WNBA邀请19岁小伙救下5人后溺亡 多方发声王树国3次鞠躬告别西交大师生单亲妈妈陷入热恋 14岁儿子报警315晚会后胖东来又人满为患了倪萍分享减重40斤方法王楚钦登顶三项第一今日春分两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?周杰伦一审败诉网易房客欠租失踪 房东直发愁男子持台球杆殴打2名女店员被抓男子被猫抓伤后确诊“猫抓病”“重生之我在北大当嫡校长”槽头肉企业被曝光前生意红火男孩8年未见母亲被告知被遗忘恒大被罚41.75亿到底怎么缴网友洛杉矶偶遇贾玲杨倩无缘巴黎奥运张立群任西安交通大学校长黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发妈妈回应孩子在校撞护栏坠楼考生莫言也上北大硕士复试名单了韩国首次吊销离岗医生执照奥巴马现身唐宁街 黑色着装引猜测沈阳一轿车冲入人行道致3死2伤阿根廷将发行1万与2万面值的纸币外国人感慨凌晨的中国很安全男子被流浪猫绊倒 投喂者赔24万手机成瘾是影响睡眠质量重要因素春分“立蛋”成功率更高?胖东来员工每周单休无小长假“开封王婆”爆火:促成四五十对专家建议不必谈骨泥色变浙江一高校内汽车冲撞行人 多人受伤许家印被限制高消费

聚圣源 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化