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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:59:01  来源:igfitidea点击:

How to count number of fields in a table?

vbams-accessaccess-vbafieldms-access-2010

提问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 .Countproperty of the TableDefFieldscollection. Here is an Immediate window example (Ctrl+gwill take you there) ...

您可以从集合的.Count属性中检索表中的字段数TableDefFields。这是一个立即窗口示例(Ctrl+g将带您到那里)...

? CurrentDb.TableDefs("tblFoo").Fields.Count
 13

If you actually meant the number of rows instead of fields, you can use the TableDefRecordCountproperty or DCount.

如果您实际上是指行数而不是字段数,则可以使用TableDefRecordCount属性或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 MoveLastbefore 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 行以获取列数。