简体   繁体   English

如何通过vba查看访问表中的记录集?

[英]How to view a recordset in an access table by means of vba?

With help of the embedded access vb editor i've written a small code to analyse the field values of my database, and want to finally view the recordsets in a table inside the opened access. 在嵌入式访问vb编辑器的帮助下,我编写了一个小代码来分析我的数据库的字段值,并希望最终在打开的访问内的表中查看记录集。 As a newbie i can only use Debug.Print to display the field names. 作为一个新手,我只能使用Debug.Print来显示字段名称。 Could anyone of you tell me with which statements/commands i can execute my SQL String in order to view the result recordset with values? 你有没有人告诉我哪些语句/命令我可以执行我的SQL字符串,以便查看带有值的结果记录集?

Debug.Print FinalSQLString

Here is the basic recipe: 这是基本配方:

Dim db As Database
Dim rs As Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT * FROM myTable")

Know that you are using Jet Data Access Objects (DAO) with Access - google that for details. 知道您正在使用Jet数据访问对象(DAO)与Access - 谷歌的详细信息。

Expression (rs.BOF and rs.EOF) = True indicates there were no rows. 表达式(rs.BOF and rs.EOF) = True表示没有行。

Use rs.MoveFirst , rs.MoveNext to go to the first and next rows. 使用rs.MoveFirstrs.MoveNext转到第一行和下一行。 Test rs.EOF after rs.MoveNext ; rs.EOF后测试rs.MoveNext ; when True , last row was already processed. 当为True ,最后一行已经处理完毕。

rs(FieldName) returns the value of the column named FieldName (a string expression). rs(FieldName)返回名为FieldName的列(字符串表达式)的值。

rs(1) returns the value of the second column. rs(1)返回第二列的值。

When done, rs.Close . 完成后, rs.Close 。关闭。


There is no way to hand Access the RecordSet and have it displayed in a Datasheet view. 无法手动访问RecordSet并将其显示在数据表视图中。 Instead, you will have to create a QueryDef object and use it to perform the query and display the Datasheet view of the results: 相反,您必须创建一个QueryDef对象并使用它来执行查询并显示结果的数据表视图:

Dim qd As QueryDef

On Error Resume Next
CurrentDb.QueryDefs.Delete "temp"
On Error GoTo 0

Set qd = db.CreateQueryDef("temp", "SELECT * FROM myTable")

DoCmd.OpenQuery "temp", acViewNormal, acEdit

As far as I know, there is no way to display a datasheet containing a VBA instance of a recordset. 据我所知,没有办法显示包含记录集的VBA实例的数据表。 If the source of your recordset is strQSL, you could however create a table with your results, and open that one, or more elegantly, create queryDef and open it: 如果您的记录集的源是strQSL,那么您可以创建一个包含结果的表,并打开一个或更优雅地创建queryDef并打开它:

Sub ShowQd(strQdName As String, strSql As String)
'creates queryDef and display it in a datasheet'
    Dim qd As DAO.QueryDef

    Set qd = CurrentDb.CreateQueryDef(strQdName)
    With qd
        .ReturnsRecords = True
        .SQL = strSql
    End With
    DoCmd.OpenQuery strQdName
End Sub

If you focus on displaying things, you could also put a ListBox in a form, set its Number of columns to the number of fields returned by your query ( qd.Fields.Count ), and set your strSql as the RowSource of the ListBox. 如果您专注于显示内容,您还可以将ListBox放在表单中,将其列数设置为查询返回的字段数( qd.Fields.Count ),并将strSql设置为ListBox的RowSource。 AND... if you put all your related code in that form, you now have a form that you can import in any db to quickly display what you want :) AND ...如果您将所有相关代码放在该表单中,您现在可以在任何数据库中导入一个表单以快速显示您想要的内容:)
Good luck ! 祝好运 !

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

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