5

我正在尝试计算 Access 2010 中表中的字段数。我需要 vb 脚本吗?

4

3 回答 3

11

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

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

如果您实际上是指行数而不是字段数,则可以使用TableDef RecordCount属性 或DCount

? CurrentDb.TableDefs("tblFoo").RecordCount
 11 
? DCount("*", "tblFoo")
 11 
于 2013-10-18T15:26:10.170 回答
3

使用查询:

'To get the record count
SELECT Count(*) FROM MyTable

在 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")

请注意MoveLast,在获取 之前执行 很重要RecordCount

在 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")
于 2013-10-18T15:13:05.333 回答
1

快速简便的方法:将表格导出到 Excel 并突出显示第 1 行以获取列数。

于 2017-02-11T15:01:33.923 回答