Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 如何计算表中的字段数?_Ms Access_Vba_Field_Ms Access 2010 - Fatal编程技术网

Ms access 如何计算表中的字段数?

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 "

我试图在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 " & 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代码中使用相同的技术。