Office中国论坛/Access中国论坛

标题: [求助]ADO取出一个数据表的每字段名? [打印本页]

作者: 菩明本何    时间: 2006-11-5 18:24
标题: [求助]ADO取出一个数据表的每字段名?
我在做一个表格。想要用代码ADO取某一个数据表的各个字段??要怎么做??
作者: fan0217    时间: 2006-11-6 03:35

Sub Main()
    Dim Cnxn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim fld As ADODB.Field
    Dim strCnxn As String
    Dim strSQL As String
    Dim FieldType As String
   
    Set Cnxn = New ADODB.Connection
    strCnxn = "rovider='sqloledb';Data Source='MySqlServer';" & _
        "Initial Catalog='Pubs';Integrated Security='SSPI';"
    Cnxn.Open strCnxn
  
    Set rst = New ADODB.Recordset
    strSQL = "employee"
    rst.Open strSQL, Cnxn, , , adCmdTable
   
    Debug.Print "Fields in Employees Table:" & vbCr
   
    For Each fld In rstEmployees.Fields
        Select Case fld.Type
            Case adChar
               FieldType = "adChar"
            Case adVarChar
               FieldType = "adVarChar"
            Case adSmallInt
               FieldType = "adSmallInt"
            Case adUnsignedTinyInt
               FieldType = "adUnsignedTinyInt"
            Case adDBTimeStamp
               FieldType = "adDBTimeStamp"
        End Select

        Debug.Print "  Name: " & fld.Name & vbCr & "  Type: " & FieldType & vbCr
    Next
   
    rstEmployees.Close
    Cnxn.Close
    Set rstEmployees = Nothing
    Set Cnxn = Nothing
End Sub





欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3