MS ACCESS 2007 VBA:DAO 记录集....如何查看返回集合中的所有“字段”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3118688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MS ACCESS 2007 VBA : DAO recordset....how can I view all the "fields" in the returned collection
提问by Justin
so if i do a SQL statement like so:
因此,如果我执行这样的 SQL 语句:
sql = "SELECT * FROM tblMain"
set rs = currentdb.openrecordset(sql)
what method can i use to view every "field name" in this collection i have just created. i am getting some very strange error stating that the item is not found in this collection.
我可以使用什么方法来查看我刚刚创建的这个集合中的每个“字段名称”。我收到一些非常奇怪的错误,指出在此集合中找不到该项目。
i know the field exists in the table, i have triple checked the spelling everywhere when i reference it, and the SQL should be pulling everything, but i want to see it.
我知道该字段存在于表中,我在引用它时对所有地方的拼写进行了三次检查,并且 SQL 应该提取所有内容,但我想查看它。
is there a debug.print method to see all these fields
是否有 debug.print 方法可以查看所有这些字段
thanks Justin
谢谢贾斯汀
回答by David-W-Fenton
This is a variation on the other answers, but I believe it's better to use a For/Each loop than a counter:
这是其他答案的变体,但我相信使用 For/Each 循环比使用计数器更好:
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Set rs = CurrentDB.OpenRecordset("SELECT * FROM tblMain")
For Each fld In rs.Fields
Debug.Print fld.Name
Next fld
Set fld = Nothing
rs.Close
Set rs = Nothing
回答by Raj More
You can iterate through the fields collection of the recordset.
您可以遍历记录集的字段集合。
Code is OTTOMH
代码是 OTTOMH
Dim NumFields as Integer
For NumFields = 0 to rs.Fields.Count -1
Debug.Print Rs.Fields(NumFields).Name
Next
Alternately, you can set a breakpoint at set rs = currentdb.openrecordset(sql)
and then as soon as the statement executes, right-click on rs
, choose add watchand view the whole thing in the Watcheswindow.
或者,您可以在 at 设置断点,set rs = currentdb.openrecordset(sql)
然后在语句执行后立即右键单击rs
,选择add watch并在Watches窗口中查看整个内容。
回答by ray
Here is a script that will look for a field containing the string you specify in every table in an Access database (except System and Attached Tables) and write it to text files:
这是一个脚本,它将查找包含您在 Access 数据库中的每个表(系统和附加表除外)中指定的字符串的字段并将其写入文本文件:
Option Compare Database
Option Explicit
Sub main()
Dim db As Database
Dim rs As Recordset
Dim bFinished As Boolean
Dim sFieldName As String
Dim iPosition, z, x As Integer
Dim bRetVal As Boolean
Dim tdTemp As TableDef
Dim iDatabaseNumbers As Integer
Const FIELD_TO_FIND = "FieldName"
Set db = CurrentDb
Open Left(db.Name, Len(db.Name) - 4) & "_" & FIELD_TO_FIND & ".txt" For Output As #1
For x = 0 To db.TableDefs.Count - 1
Set tdTemp = db.TableDefs(x)
bRetVal = IIf(tdTemp.Attributes And dbSystemObject, False, True)
If bRetVal Then
bRetVal = IIf(tdTemp.Attributes And dbAttachedTable, False, True)
End If
If bRetVal Then
Set rs = db.OpenRecordset(db.TableDefs(x).Name)
If rs.RecordCount > 0 Then
For z = 0 To rs.Fields.Count - 1
sFieldName = rs.Fields(z).Name
If InStr(1, sFieldName, FIELD_TO_FIND, vbTextCompare) > 0 Then
Print #1, db.TableDefs(x).Name
Exit For
End If
Next z
End If
End If
Next x
Close #1
MsgBox "Done"
End Sub
You could adjust accordingly to make it do what you need.
您可以相应地进行调整以使其满足您的需求。