标题:[求助]得到数据库中所有表的字段!
只看楼主
zeyidao
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-9-17
 问题点数:0 回复次数:3 
[求助]得到数据库中所有表的字段!

Sub TableInfo()
 Dim i, j As Integer, Fname As String
 Dim db1 As Database, Td1 As TableDefs
 Dim fld1 As Fields
 Dim FieldNum, RecNum As Integer

 Fname$ = "d:\mdb\xx.mdb" ‘XX为ACCESS数据库文件
 Set db1 = OpenDataBase(Fname$) '打开一数据库文件
 Set Td1 = db1.TableDefs
 For i = 1 To Td1.Count - 1
  Debug.Print Td1(i).Name ‘输出表名
  Set fld1 = Td1(i).Fields '出错!!!!
  FieldNum = fld1.Count
  RecNum = Td1(i).RecordCount
  Debug.Print "当前表共有"; FieldNum; "个字段" ‘输出字段的个数
  Debug.Print "当前表有:"; RecNum; "记录" ‘输出记录的个数
  For j = 0 To fld1.Count - 1
   Debug.Print "字段名", fld1(j).Name ‘输出字段名
   Debug.Print "类型", fld1(j).Type ‘输出字段类型
  Next j
 Next i
End Sub

请高手指教一下

搜索更多相关主题的帖子: 数据库 字段 Set Dim Fname 
2007-10-12 15:32
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
得分:0 
应该是这样吧
for j=1 to Td1(i).Fields.Count-1
set fld1=Td1(i).Fields(j)

我自己没试过,猜的,你试试看吧,记得后面要有next j

从BFS(Breadth First Study)到DFS(Depth First Study)
2007-10-12 15:41
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
得分:0 

[CODE]Option Explicit
Private Sub Form_Click()
'添加ado引用
Dim Con As New ADODB.Connection
Dim Rs As ADODB.Recordset
Dim frs As ADODB.Recordset
Con.CursorLocation = adUseClient
Con.Open "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=Northwind;User ID=sa;Password=admin;"
Set Rs = Con.OpenSchema(adSchemaTables)
Dim i As Integer
Dim tbname As String

While Not Rs.EOF
If Rs.Fields("Table_Type") = "TABLE" Then
tbname = Rs.Fields("Table_name").Value
Debug.Print tbname
Set frs = New ADODB.Recordset
frs.CursorLocation = adUseClient
frs.Open "select * from [" & tbname & "] where 1=2", Con, adOpenForwardOnly, adLockReadOnly, adCmdText
For i = 0 To frs.Fields.Count - 1
Debug.Print " " & frs.Fields(i).Name
Next
frs.Close
Set frs = Nothing
End If
Rs.MoveNext
Wend
Rs.Close
Con.Close
End Sub[/CODE]



我的msn: myfend@
2007-10-12 16:04
心中有剑
Rank: 2
等 级:新手上路
威 望:5
帖 子:611
专家分:0
注 册:2007-5-18
得分:0 

同意楼上的!

我写过一个导出acc到excel的函数啊

'*************************************************************************
'**函 数 名:ExportTableToOneExcel
'**输 入:ByVal AccName(String) -数据库的名称
'** :ByVal ExpExcelName(String) -导出excel的名称
'** :Optional tableName(String = "") -要导出的表明 默认为空代表导出所有表
'** :Optional DbPassWord(String = "") -数据库的密码 默认为空
'**输 出:无
'**功能描述:实现把acc数据库的表导出到excel里面
'**全局变量:在执行此函数前 先要 引用ado和access
'**调用模块:
'**作 者:心中有剑
'**日 期:2007-08-28 13:27:40
'**修 改 人:
'**日 期:
'**版 本:V1.0.0
'*************************************************************************
Public Function ExportTableToOneExcel(ByVal AccName As String, ByVal ExpExcelName As String, Optional tableName As String = "", Optional DbPassWord As String = "")
Dim i As Long
Dim rstSchema As ADODB.Recordset
Dim accApp As Access.Application
Dim cnnTemp As ADODB.Connection
Set accApp = New Access.Application
accApp.OpenCurrentDatabase AccName, , DbPassWord '打开数据库
Set cnnTemp = accApp.CurrentProject.Connection '设置ado的连接为当前数据库的连接
Set rstSchema = cnnTemp.OpenSchema(adSchemaTables)
Do Until rstSchema.EOF
If rstSchema("TABLE_TYPE") = "TABLE" Then
For i = 0 To rstSchema.Fields.Count - 1
If rstSchema(i).Name = "TABLE_NAME" Then '判断是不是用户表
If tableName <> "" Then '判断是否要导出制定表
If rstSchema.Fields(i).Value = tableName Then
accApp.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, tableName, ExpExcelName, True
Exit Do
End If
Else '导出所有表
accApp.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, rstSchema.Fields(i).Value, ExpExcelName, True
End If
End If
Next
End If
rstSchema.MoveNext
Loop
rstSchema.Close
Set cnnTemp = Nothing
accApp.CloseCurrentDatabase
Set accApp = Nothing
End Function


2007-10-12 16:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-177054-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.296789 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved