简体   繁体   English

在VBA中使用CurrentDb.OpenRecordset

[英]Use of CurrentDb.OpenRecordset in VBA

I am editing something in VBA in MS access. 我在MS访问中编辑VBA中的内容。 Can someone please tell me the difference between below two statements. 有人可以告诉我下面两个陈述之间的区别。

1. Set rs1 = CurrentDb.OpenRecordset("tblOPCEPLTB")
2. Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblOPCEPLTB")

What I am guessing is 2nd line fetches all rows from table tblOPCEPLTB and store in rs variable. 我猜测的是第二行从表tblOPCEPLTB获取所有行并存储在rs变量中。 So what does 1st do? 那么1st会做什么? After executing 1st what should it have? 在执行1st之后应该有什么?

Please help here 请帮忙

The first statement opens a table-type recordset if that table is a local Access table (because the argument passed is a table, dbOpenTable is the default) 如果该表是本地Access表,则第一个语句打开表类型记录集(因为传递的参数是表, dbOpenTable是默认值)

The second statement opens a dynaset-type recordset (because the argument passed is a query, dbOpenDynaset is the default) 第二个语句打开动态集类型记录集(因为传递的参数是查询, dbOpenDynaset是默认值)

The main difference between dynaset-type and table-type recordsets is that table-type recordsets support indexes. 动态集类型和表类型记录集之间的主要区别在于表类型记录集支持索引。

The following code will succeed and look up the entry with a primary key value of 5 以下代码将成功并查找主键值为5的条目

Set rs = CurrentDb.OpenRecordset("tblOPCEPLTB")
rs.Index = "PrimaryKey"
rs.Seek "=", 5

The following will fail, since seeks are only supported on table-type recordsets: 以下操作将失败,因为仅在表类型记录集上支持搜索:

Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblOPCEPLTB")
rs.Index = "PrimaryKey"
rs.Seek "=", 5

If you only wish to append records to a table, then specify that when opening the recordset: 如果您只想将记录追加到表中,请在打开记录集时指定:

Set rs = CurrentDb.OpenRecordset("tblOPCEPLTB", Options:=dbAppendOnly)

This way, no records will get locked or loaded, since the recordset will only support appending 这样,没有记录会被锁定或加载,因为记录集只支持附加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM