|
本帖最后由 smileyoufu 于 2023-5-10 09:33 编辑
一个好的使用习惯是,使用关键字,指定是DAO还是ADO,这样就不容易出错:【ADO】
Function ShowSchema()
'列出表
'目的:使用ADO列出表。
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim i As Integer
Set cn = CurrentProject.Connection
Set rs = cn.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "TABLE"))
For i = 0 To rs.Fields.Count - 1
Debug.Print rs.Fields(i).Name
Next
Do While Not rs.EOF
Debug.Print rs.Fields("TABLE_NAME").Value
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set cn = Nothing
End Function
【DAO】
Function ShowFields(strTable As String)
'目的:如何读取表的字段。
'用法:调用显示字段(“表1”)
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Set db = CurrentDb()
Set tdf = db.TableDefs(strTable)
For Each fld In tdf.Fields
Debug.Print fld.Name, FieldTypeName(fld)
Next
Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
End Function
|
|