确定(在VBA中)记录集是否已打开 [英] Determining (in VBA) if a recordset is open

查看:93
本文介绍了确定(在VBA中)记录集是否已打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果发生错误,如果

记录集打开,我想关闭记录集并将数据库设置为空。打开记录集

和数据库打开不是一个好主意,对吧?


谢谢,


-paul w

Hi,

I''d like to close a recordset and set the database to nothing if a
recordset is open if an error has occured. Leaving a recordset open
and a database open isn''t a good idea, right?

Thanks,

-paul w

推荐答案

一旦记录集对象和数据库对象超出范围,VBA

会自动关闭对象,但是,这是一个很好的做法,

专门关闭记录集> rs.Close并将数据库对象设置为

没有>设置DB =没什么。它有助于进一步降低仍然在堆栈上的空指针(在系统级别)的可能性。


如果您正在使用全局变量另一方面,

当程序使用

时,这些对象不会超出范围。在这种情况下,如果你没有关闭记录集并试图在另一个程序中重新使用它再次使用
,那么Access会抱怨。我发现在本地级别使用Recordset对象是最好的

(使用它的程序本地




Rich


***通过开发人员指南 http://www.developersdex .com ***
Once a recordset object and a database object go out of scope, VBA
automatically closes the objects, although, it is a good practice to
specifically close a recordset >rs.Close and set a database object to
nothing >Set DB = Nothing. It helps to further reduce the possibility
of a null pointer still on the stack (at the system level).

If you are using global variables for your objects, on the other hand,
these objects won''t go out of scope when a procedure is finished using
them. In this case, if you don''t close a recordset and try to reuse it
again in another procedure, then Access will complain. I find it best
to use Recordset objects at the local level (local to the procedure
using it.

Rich

*** Sent via Developersdex http://www.developersdex.com ***


PW< pa ******** @ removewilliamson Gloswrote in

news :pd ******************************** @ 4ax.com:
PW <pa********@removewilliamsonenterprises.comwrote in
news:pd********************************@4ax.com:

如果发生错误,如果

记录集打开,我想关闭记录集并将数据库设置为空。保留记录集

打开并打开数据库不是一个好主意,对吗?
I''d like to close a recordset and set the database to nothing if a
recordset is open if an error has occured. Leaving a recordset
open and a database open isn''t a good idea, right?



通常的方法是测试记录集变量是否为Nothing。

由于没有它没有办法初始化一个记录集变量而没有它指向一个开放记录集的
,这很好用。


在退出例程中我用这个:


如果没有什么就没有那么

rs.Close

设置rs =没什么

结束如果


对于db变量,我也这样做,但没有关闭的情况

其中db变量指向当前在<...中打开的MDB br />
访问UI。使用CurrentDB()初始化变量时,.Close没有问题,但如果你用DBEngine(0)(0)初始化它,可能会导致错误。 br />

我知道数据库变量可能不是

什么也没有打开,我假设在rs.Close

和Set rs = Nothing变量不是什么但是

关闭,但这只会在你关闭之后发生

记录集。如果你只是在你的清理中这样做,它不应该是一个

问题。


-

David W 。芬顿 http://www.dfenton.com/

usenet at dfenton dot com http://www.dfenton.com/DFA/


" David W. Fenton" < XX ******* @ dfenton.com.invalidwrote在

新闻:Xn ********************** ************@127.0.0。 1:
"David W. Fenton" <XX*******@dfenton.com.invalidwrote in
news:Xn**********************************@127.0.0. 1:

PW< pa ******** @ removewilliamson Gloswrote in

news:pd **** ****************************@4ax.com:
PW <pa********@removewilliamsonenterprises.comwrote in
news:pd********************************@4ax.com:

> ;如果发生错误,如果
记录集打开,我想关闭记录集并将数据库设置为空。让记录集打开并打开数据库并不是一个好主意,对吧?
>I''d like to close a recordset and set the database to nothing if a
recordset is open if an error has occured. Leaving a recordset
open and a database open isn''t a good idea, right?



通常的方法是测试记录集变量是否为Nothing。

因为没有办法初始化记录集变量而没有它指向一个开放的记录集
,这很好用。


在退出程序中我用这个:


如果没有什么就没有那么

rs.Close

设置rs =没什么

结束如果


对于db变量,我也这样做,但没有关闭的情况下

其中db变量指向当前在

Access UI中打开的MDB。使用CurrentDB()初始化变量时,.Close没有问题,但如果你用DBEngine(0)(0)初始化它,可能会导致错误。 br />

我知道数据库变量可能不是

什么也没有打开,我假设在rs.Close

和Set rs = Nothing变量不是什么但是

关闭,但这只会在你关闭之后发生

记录集。如果你只是在你的清理中这样做,它不应该是一个

问题。


The usual method is to test if the recordset variable Is Nothing.
Since there''s no way to initialize a recordset variable without it
pointing to an open recordset, this works pretty well.

In the exit routine I use this:

If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If

For db variables, I do the same, but without the close in cases
where the db variable is pointing to the MDB currently opened in the
Access UI. The .Close causes no problem when the variable is
initialized with CurrentDB() but can cause an error if you
initialized it with DBEngine(0)(0).

I do know that it''s possible for a database variable to be Not
Nothing and not be open, and I''d assume that between the rs.Close
and the Set rs = Nothing that the variable is Not Nothing but
closed, but that''s only going to happen once you''ve closed the
recordset. If you only do that in your cleanup, it oughtn''t be an
issue.



令人遗憾的是,有问题的记录集不是ADO记录集,我认为是b $ b。然后我们可以测试它的State属性


adStateClosed 0表示对象已关闭。

adStateOpen 1表示对象已打开。


我们会知道......以及其他一些事情。


adStateConnecting 2表示对象正在连接。

adStateExecuting 4表示对象正在执行命令。

adStateFetching 8表示对象的行正在检索



但我想我们不能指望

一种过时的技术给我们很多;

我们只需要忍受它,一段时间也就是说,它幸存下来。


当然如果它是一个ADO记录集,在这种情况下我们也不需要

测试它无论如何ADO指针收集在垃圾收集中,并且无需任何干预即可发布



我想难怪DAO是kaput!


-

Lyle Fairfield


来自 http://msdn.microsoft.com/library/de...l=/library/en-

us / dnmdac / html / data_mdacroadmap.asp

过时的数据访问技术

过时的技术是尚未增强的技术或

在几个产品版本中更新,将从未来的产品版本中排除。在编写新的

应用程序时,请勿使用这些技术。当您使用这些技术修改使用这些技术编写的现有应用程序时,请考虑将这些应用程序迁移到

ADO.NET。

考虑以下组件已过时:

....

数据访问对象(DAO):DAO提供对JET(Access)数据库的访问。

此API可以可以从Microsoft Visual Basic?,Microsoft Visual C ++?,

和脚本语言中使用。它包含在Microsoft Office 2000和

Office XP中。 DAO 3.6是该技术的最终版本。它不会在64位Windows操作系统上提供


.....

It''s unfortunate the recordset in question isn''t an ADO recordset, I
suppose. Then we could just test its State property for

adStateClosed 0 Indicates that the object is closed.
adStateOpen 1 Indicates that the object is open.

and we''d know ... and a few other things.

adStateConnecting 2 Indicates that the object is connecting.
adStateExecuting 4 Indicates that the object is executing a command.
adStateFetching 8 Indicates that the rows of the object are being
retrieved.

But I suppose we can''t expect
an obsolete technology to give us very much;
we just have to live with it, for a while that is, while it survives.

Of course if it were an ADO recordset, in this case we wouldn''t have to
test it anyway as ADO pointers are gathered up in garbage-collection and
released without any intervention anyway.

I guess it''s no wonder that DAO is kaput!

--
Lyle Fairfield

from http://msdn.microsoft.com/library/de...l=/library/en-
us/dnmdac/html/data_mdacroadmap.asp

Obsolete Data Access Technologies
Obsolete technologies are technologies that have not been enhanced or
updated in several product releases and that will be excluded from future
product releases. Do not use these technologies when you write new
applications. When you modify existing applications that are written
using these technologies, consider migrating those applications to
ADO.NET.
The following components are considered obsolete:
....
Data Access Objects (DAO): DAO provides access to JET (Access) databases.
This API can be used from Microsoft Visual Basic?, Microsoft Visual C++?,
and scripting languages. It was included with Microsoft Office 2000 and
Office XP. DAO 3.6 is the final version of this technology. It will not
be available on the 64-bit Windows operating system.
.....


这篇关于确定(在VBA中)记录集是否已打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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