How to List Field's Name in table in Access Using SQL

后端 未结 14 1272
我在风中等你
我在风中等你 2020-12-09 17:15

Can you please let me know if it is possible to list all fields name in a MS Access table?

14条回答
  •  失恋的感觉
    2020-12-09 17:53

    There are already some good answers but I decided to add my own twist. Hopefully, they are self-explanatory.

    Usage:

    • getFieldNames(TableName:="Table1",IncludeBrackets:=True,Delimiter:=vbNewLine,CopyToClipboard:=True)
    • getFieldNames(TableName:="Table1",IncludeBrackets:=True,CopyToClipboard:=True)
    • getFieldNames(TableName:="Table1",IncludeBrackets:=True)
    • getFieldNames(TableName:="Table1")

    I use this to build an array of field names:

    • Chr(34) & getFieldNames(TableName:="Table1",IncludeBrackets:=False, Delimiter:= Chr(34) & "," & Chr(34)) & Chr(34)

    Function getFieldNames(ByVal TableName As String, Optional ByVal IncludeBrackets As Boolean, Optional ByVal Delimiter As String = ", ", Optional ByVal CopyToClipboard As Boolean) As String
        Dim rs As DAO.Recordset
    
        On Error Resume Next
        Set rs = CurrentDb.OpenRecordset(TableName)
        On Error GoTo 0
    
        If rs Is Nothing Then Exit Function
    
        Dim results() As String
        ReDim results(rs.Fields.Count - 1)
    
        Dim n As Long
        For n = 0 To rs.Fields.Count - 1
            results(n) = rs.Fields(n).Name
        Next
        rs.Close
    
        Dim result As String
        If IncludeBrackets Then
            result = "[" & Join(results, "]" & Delimiter & "[") & "]"
        Else
            result = Join(results, Delimiter)
        End If
    
    
        If CopyToClipboard Then
            With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
                .SetText result
                .PutInClipboard
            End With
        End If
    
        getFieldNames = result
    End Function
    

提交回复
热议问题