计算记录集 [英] Counting recordsets

查看:62
本文介绍了计算记录集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是rsa.RecordCount总是返回'1'时

我知道应该返回大约1600条记录。我在某个地方读到了

,应该在记录计数之前使用.MoveLast但返回ar

错误'无效操作''。当我进行底部循环时,计数器

愉快地点击了正确的次数。我不明白......

我有以下代码(这只是一个简化的例子 - 我想要

来使用if语句中的recordcount):


Dim db作为DAO.Database

Dim rsa作为DAO.Recordset

Dim strsql As String


strsql =" SELECT *" &安培; _

" FROM tblInductionLots;"


设置db = DBEngine(0)(0)

设置rsa = db .OpenRecordset(strsqla,dbOpenForwardOnly)


MsgBox rsa.RecordCount


Counter = O

Do rsa。 EOF

柜台=柜台+ 1

rsa.MoveNext

MsgBox柜台

Loop

The problem I''m having is the rsa.RecordCount always returns a ''1'' when
I know there should be about 1600 records returned. I read somewhere
that one should use ''.MoveLast'' before recordcount but that returns ar
error ''Invalid Operation''. And when I do the bottom loop, the counter
happily clicks over the right number of times. Me no understand...
I''ve got the following code (this is just a simplified example - I want
to use the recordcount in an If Statement):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql = "SELECT * " & _
"FROM tblInductionLots;"

Set db = DBEngine(0)(0)
Set rsa = db.OpenRecordset(strsqla, dbOpenForwardOnly)

MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop

推荐答案

MoveLast应该可以工作,除非根本没有记录。


这对于打开一个只有前进的记录集并跳到最后,

所以试试:

设置rsa = db.OpenRecordset(strsqla,dbOpenDynaset)

如果rsa.RecordCount> 0然后

rsa.MoveLast

MsgBox rsa.RecordCount

结束如果


我也注意到你有一个名为Counter的变量,它在程序的顶部没有Dim',并且你的代码似乎是将这个未声明的变体设置为字母O的
(这可能被视为另一个未申报的空白

变体)。您可以通过将其添加到每个

模块的顶部来避免这些问题:

选项明确

要获取Access,请执行此操作将来的模块,从

工具菜单中选择选项(在代码窗口中)。在编辑器选项卡上,单击以下框:

需要变量声明


-

Allen Browne - Microsoft MVP。西澳大利亚州珀斯。

访问用户提示 - http:// allenbrowne.com/tips.html

回复群组,而不是mvps dot org的allenbrowne。


" Regnab" < P ******* @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
The MoveLast should work, unless there are no records at all.

It is not very useful to open a forward-only recordset and jump to the end,
so try:
Set rsa = db.OpenRecordset(strsqla, dbOpenDynaset)
If rsa.RecordCount > 0 Then
rsa.MoveLast
MsgBox rsa.RecordCount
End If

I also notice you have a variable named Counter that is not Dim''d at the top
of the procedure, and your code seems to be setting this undeclared variant
to the letter O (which is probably treated as another undeclared empty
variant). You can avoid these problems by adding this to the top of every
module:
Option Explicit
To get Access to do this for you for future modules, choose Options from the
Tools menu (in the code window). On the Editor tab, click the box for:
Require Variable Declaration

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Regnab" <p.*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
我遇到的问题是rsa.RecordCount总是返回1时
我知道应该返回大约1600条记录。我在某个地方读到
应该在recordcount之前使用''。MoveLast''但是返回错误''无效操作''。当我进行底部循环时,计数器
愉快地点击了正确的次数。我不明白......

我有以下代码(这只是一个简化的例子 - 我想
在If语句中使用recordcount):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql =" SELECT *" &安培; _
" FROM tblInductionLots;"

设置db = DBEngine(0)(0)
设置rsa = db.OpenRecordset(strsqla,dbOpenForwardOnly)
< MsgBox rsa.RecordCount

计数器= O
直到rsa.EOF
计数器=计数器+ 1
rsa.MoveNext
MsgBox计数器
循环
The problem I''m having is the rsa.RecordCount always returns a ''1'' when
I know there should be about 1600 records returned. I read somewhere
that one should use ''.MoveLast'' before recordcount but that returns ar
error ''Invalid Operation''. And when I do the bottom loop, the counter
happily clicks over the right number of times. Me no understand...
I''ve got the following code (this is just a simplified example - I want
to use the recordcount in an If Statement):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql = "SELECT * " & _
"FROM tblInductionLots;"

Set db = DBEngine(0)(0)
Set rsa = db.OpenRecordset(strsqla, dbOpenForwardOnly)

MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop



更改代码的这一部分:

MsgBox rsa.RecordCount


柜台= O

直到rsa.EOF

柜台=柜台+ 1

rsa.MoveNext

MsgBox柜台

循环


要:

如果rsa.RecordCount = 0那么

rsa.MoveLast

结束如果

MsgBox rsa.RecordCount

-

PC数据表

您的资源以获取Access,Excel和Word应用程序的帮助
re ****** @ pcdatasheet.com
www.pcdatasheet.com


如果您无法在新闻组中获得所需的帮助,我可以帮助您一个

非常合理的费用。超过1000名访问用户来找我寻求帮助。

需要一个月历或7天日历吗?需要预约安排?需要

房间预订安排?需要员工的工作安排?联系我!


" Regnab" < P ******* @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
Change this part of your code:
MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop

To:
If rsa.RecordCount = 0 Then
rsa.MoveLast
End If
MsgBox rsa.RecordCount
--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdatasheet.com
www.pcdatasheet.com

If you can''t get the help you need in the newsgroup, I can help you for a
very reasonable fee. Over 1000 Access users have come to me for help.
Need a month calendar or 7 day calendar? Need appointment scheduling? Need
room reservations scheduling? Need employee work scheduling? Contact me!


"Regnab" <p.*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
我遇到的问题是rsa.RecordCount总是返回1时
我知道应该返回大约1600条记录。我在某个地方读到
应该在recordcount之前使用''。MoveLast''但是返回错误''无效操作''。当我进行底部循环时,计数器
愉快地点击了正确的次数。我不明白......

我有以下代码(这只是一个简化的例子 - 我想
在If语句中使用recordcount):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql =" SELECT *" &安培; _
" FROM tblInductionLots;"

设置db = DBEngine(0)(0)
设置rsa = db.OpenRecordset(strsqla,dbOpenForwardOnly)
< MsgBox rsa.RecordCount

计数器= O
直到rsa.EOF
计数器=计数器+ 1
rsa.MoveNext
MsgBox计数器
循环
The problem I''m having is the rsa.RecordCount always returns a ''1'' when
I know there should be about 1600 records returned. I read somewhere
that one should use ''.MoveLast'' before recordcount but that returns ar
error ''Invalid Operation''. And when I do the bottom loop, the counter
happily clicks over the right number of times. Me no understand...
I''ve got the following code (this is just a simplified example - I want
to use the recordcount in an If Statement):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql = "SELECT * " & _
"FROM tblInductionLots;"

Set db = DBEngine(0)(0)
Set rsa = db.OpenRecordset(strsqla, dbOpenForwardOnly)

MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop



Allen

为什么不使用DCOUNT()函数检查记录?

Bob


" Allen Browne" <铝********* @ SeeSig.Invalid>在消息中写道

news:43 *********************** @ per-qv1-newsreader-01.iinet.net。 au ...
Allen
Why don''y you use the DCOUNT() funcion to check for records?
Bob

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:43***********************@per-qv1-newsreader-01.iinet.net.au...
除非根本没有记录,否则MoveLast应该可以正常工作。

打开仅向前记录集并跳转到$是没有用的b $ b结束,所以试试:
设置rsa = db.OpenRecordset(strsqla,dbOpenDynaset)
如果rsa.RecordCount> 0然后
rsa.MoveLast
MsgBox rsa.RecordCount
结束如果

我还注意到你有一个名为Counter的变量,它在$上没有Dim' b $ b程序的顶部,你的代码似乎是将这个未声明的
变体设置为字母O(可能被视为另一个未声明的空
变体)。您可以通过将其添加到每个
模块的顶部来避免这些问题:
Option Explicit
要获得Access以便为将来的模块执行此操作,请从
中选择选项工具菜单(在代码窗口中)。在编辑器选项卡上,单击以下框:
需要变量声明

- Allen Browne - Microsoft MVP。西澳大利亚州珀斯。
访问用户提示 - http://allenbrowne.com/ tips.html
回复群组,而不是mvps dot org的allenbrowne。

Regnab < P ******* @ gmail.com>在消息中写道
新闻:11 ********************** @ f14g2000cwb.googlegr oups.com ...
The MoveLast should work, unless there are no records at all.

It is not very useful to open a forward-only recordset and jump to the end, so try:
Set rsa = db.OpenRecordset(strsqla, dbOpenDynaset)
If rsa.RecordCount > 0 Then
rsa.MoveLast
MsgBox rsa.RecordCount
End If

I also notice you have a variable named Counter that is not Dim''d at the top of the procedure, and your code seems to be setting this undeclared variant to the letter O (which is probably treated as another undeclared empty
variant). You can avoid these problems by adding this to the top of every
module:
Option Explicit
To get Access to do this for you for future modules, choose Options from the Tools menu (in the code window). On the Editor tab, click the box for:
Require Variable Declaration

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Regnab" <p.*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
我遇到的问题是rsa.RecordCount总是返回1时
我知道应该返回大约1600条记录。我在某个地方读到
应该在recordcount之前使用''。MoveLast''但是返回错误''无效操作''。当我进行底部循环时,计数器
愉快地点击了正确的次数。我不明白......

我有以下代码(这只是一个简化的例子 - 我想
在If语句中使用recordcount):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql =" SELECT *" &安培; _
" FROM tblInductionLots;"

设置db = DBEngine(0)(0)
设置rsa = db.OpenRecordset(strsqla,dbOpenForwardOnly)
< MsgBox rsa.RecordCount

计数器= O
直到rsa.EOF
计数器=计数器+ 1
rsa.MoveNext
MsgBox计数器
循环
The problem I''m having is the rsa.RecordCount always returns a ''1'' when
I know there should be about 1600 records returned. I read somewhere
that one should use ''.MoveLast'' before recordcount but that returns ar
error ''Invalid Operation''. And when I do the bottom loop, the counter
happily clicks over the right number of times. Me no understand...
I''ve got the following code (this is just a simplified example - I want
to use the recordcount in an If Statement):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql = "SELECT * " & _
"FROM tblInductionLots;"

Set db = DBEngine(0)(0)
Set rsa = db.OpenRecordset(strsqla, dbOpenForwardOnly)

MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop




这篇关于计算记录集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆