Ms access 如何计算表中的字段数?
我试图在Access 2010中计算表中的字段数。我需要vb脚本吗?使用查询:Ms access 如何计算表中的字段数?,ms-access,vba,field,ms-access-2010,Ms Access,Vba,Field,Ms Access 2010,我试图在Access 2010中计算表中的字段数。我需要vb脚本吗?使用查询: '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 "
'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")
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")
注意,在获取记录计数之前执行MoveLast
非常重要
在ADO中,它看起来像:
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")
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")
您可以从TableDef
fields
集合的.Count
属性中检索表中的字段数。下面是一个即时窗口示例(Ctrl+g将带您到那里)
?CurrentDb.TableDefs(“tblFoo”).Fields.Count
13
如果实际上是指行数而不是字段数,则可以使用TableDef
RecordCount
属性或DCount
?CurrentDb.TableDefs(“tblFoo”).RecordCount
11
? 数据计数(“*”,“tblFoo”)
11
快速简便的方法:将表格导出到Excel并突出显示第1行以获得列数。@HansUp,该死的,我错过了那一行。我总是忘了马上上班Window@Linger你的回答让我怀疑这个问题是关于字段计数还是行计数,所以我提供了两者。好吧,我觉得我很傻。。。在Access中在何处运行此命令?可以使用Ctrl+g打开即时窗口并在那里执行这些语句。但是,如果愿意,您可以在VBA代码中使用相同的技术。