vbscript 将字段信息写入范围

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vbscript 将字段信息写入范围相关的知识,希望对你有一定的参考价值。

Sub WriteFieldsInfoToRange(db As ADODB.Connection, startCell As Range, Optional tableName As String = vbNullString)
'Description: Writes name and other info of all fields of all or one selected table

Dim adoRecSet As ADODB.Recordset
Dim i         As Long
Dim j         As Long

'   if startCell consists of more then one cell then upper-left is used
    If startCell.Cells.Count > 1 Then startCell = startCell.Cells(1)

    Select Case tableName
        Case vbNullString 'no table name given -> output all tables
            Set adoRecSet = db.OpenSchema(adSchemaColumns)
        Case Else
            Set adoRecSet = db.OpenSchema(adSchemaColumns, Array(Empty, Empty, tableName, Empty))
    End Select
    
    i = 0
    With adoRecSet
      For j = 0 To .Fields.Count - 1
          startCell.Offset(i, j) = .Fields(j).Name
      Next j
      
      i = 1
      .MoveFirst
      Do While Not .EOF
          For j = 0 To .Fields.Count - 1
              startCell.Offset(i, j) = .Fields(j).Value
          Next j
          .MoveNext
          i = i + 1
      Loop
      .Close
    End With
    Set adoRecSet = Nothing

End Sub

以上是关于vbscript 将字段信息写入范围的主要内容,如果未能解决你的问题,请参考以下文章

vbscript 将记录集写入范围

vbscript 将RecordSet列写入范围

vbscript 将记录集写入范围

vbscript 将标题写入范围

vbscript VBScript将可用磁盘空间写入文件

使用 VBA 将超链接字段写入循环内的另一个工作表范围