【发布时间】:2013-10-18 15:06:16
【问题描述】:
我正在尝试在 Access 2010 中计算表中的字段数。我需要 vb 脚本吗?
【问题讨论】:
标签: ms-access vba field ms-access-2010
我正在尝试在 Access 2010 中计算表中的字段数。我需要 vb 脚本吗?
【问题讨论】:
标签: ms-access vba field ms-access-2010
您可以从TableDefFields 集合的.Count 属性中检索表中的字段数。这是一个立即窗口示例(Ctrl+g 将带您到那里)...
? CurrentDb.TableDefs("tblFoo").Fields.Count
13
如果您实际上是指行数而不是字段数,则可以使用 TableDef RecordCount 属性或 DCount。
? CurrentDb.TableDefs("tblFoo").RecordCount
11
? DCount("*", "tblFoo")
11
【讨论】:
使用查询:
'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")
注意,在获取RecordCount 之前执行MoveLast 很重要。
在 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")
【讨论】:
快速简便的方法:将表格导出到 Excel 并突出显示第 1 行以获取列数。
【讨论】: