如何计算表中的字段数?

时间:2013-10-18 15:06:16

标签: vba ms-access access-vba field ms-access-2010

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

3 个答案:

答案 0 :(得分:9)

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

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

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

? CurrentDb.TableDefs("tblFoo").RecordCount
 11 
? DCount("*", "tblFoo")
 11 

答案 1 :(得分:4)

使用查询:

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

答案 2 :(得分:0)

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