vba 如何计算表中的字段数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19452952/
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
How to count number of fields in a table?
提问by Cici
I am trying to count number of fields in a table in Access 2010. Do I need a vb script?
我想在 Access 2010 中计算表中的字段数。我需要 vb 脚本吗?
回答by HansUp
You can retrieve the number of fields in a table from the .Count
property of the TableDef
Fields
collection. Here is an Immediate window example (Ctrl+gwill take you there) ...
您可以从集合的.Count
属性中检索表中的字段数TableDef
Fields
。这是一个立即窗口示例(Ctrl+g将带您到那里)...
? CurrentDb.TableDefs("tblFoo").Fields.Count
13
If you actually meant the number of rows instead of fields, you can use the TableDef
RecordCount
property or DCount
.
如果您实际上是指行数而不是字段数,则可以使用TableDef
RecordCount
属性或DCount
.
? CurrentDb.TableDefs("tblFoo").RecordCount
11
? DCount("*", "tblFoo")
11
回答by Linger
Using a query:
使用查询:
'To get the record count
SELECT Count(*) FROM MyTable
In DAO it would look like:
在 DAO 中,它看起来像:
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("SELECT * FROM MyTable")
rst.MoveLast
'To get the record count
MsgBox ("You have " & rst.RecordCount & " records in this table")
'To get the field count
MsgBox ("You have " & rst.Fields.Count & " fields in this table")
Note, it is important to perform the MoveLast
before getting the RecordCount
.
请注意,MoveLast
在获取RecordCount
.
In ADO it would look like:
在 ADO 中,它看起来像:
Set conn = Server.CreateObject("ADODB.Connection")
conn.Provider = "Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("MyDatabaseName.mdb"))
Set rst = Server.CreateObject("ADODB.recordset")
rst.Open "SELECT * FROM MyTable", conn
'To get the record count
If rst.Supports(adApproxPosition) = True Then _
MsgBox ("You have " & rst.RecordCount & " records in this table")
'To get the field count
MsgBox ("You have " & rst.Fields.Count & " fields in this table")
回答by Erick Molnar
Quick and easy method: Export the table to Excel and highlight row 1 to get number of columns.
快速简便的方法:将表格导出到 Excel 并突出显示第 1 行以获取列数。