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

后端 未结 14 1267
我在风中等你
我在风中等你 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:40

    I came here searching for the same requirement and after refering to this thread, drafted below code for my requirement. The Field names form the source table will be added to an array and later the Field names are assigned to the second table. Just sharing it here, this migh help someone later..

    Public Sub FieldName_Change()
    Dim intNumberOfFields, intX As Integer
    Dim txtTableName,txttmpTableName txtFieldName() As String
    
    intNumberOfFields = GetFieldNames(txtTableName, txtFieldName())
    For intX = 1 To intNumberOfFields
      CurrentDb.TableDefs(txttmpTableName).Fields("F" & intX).Name = txtFieldName(intX)
    Next intX
    End Sub
    
    
    Public Function GetFieldNames(ByVal txtTableName As String, ByRef txtFiledName() As String) As Integer
    Dim rs As DAO.Recordset
    Dim n As Long
    Dim sResult As String
    
    Set rs = CurrentDb.OpenRecordset(txtTableName)
    ReDim txtFiledName(rs.Fields.Count)
    With rs
        For n = 0 To .Fields.Count - 1
            txtFiledName(n + 1) = .Fields(n).Name
        Next n
        .Close
        GetFieldNames = n
    End With
    Set rs = Nothing
    End Function
    
    0 讨论(0)
  • 2020-12-09 17:42

    A quick and dirty method involves Excel. Do the following:

    1. Open the table in Datasheet view.
    2. Export to an Excel file, using the particular procedure for your Access version.
    3. Open Excel file (if not already open).
    4. Select and copy the first row containing the headers.
    5. Add another sheet to the workbook (if none exist).
    6. Click A1.
    7. Paste Special > Transpose

    The Fields will be pasted in a single column. To find out their Field Index number, in Cell B1 type "0", then series fill down to the last row of the field numbers.

    In addition, you can sort the column alphabetically, especially for legacy flat files involving dozens of fields. This really saves a lot of time when I'm trying to convert a flatfile to a relational model.

    0 讨论(0)
  • 2020-12-09 17:42

    This SQL works in Access 2016 for Queries, not Tables, but might be useful.

    SELECT MSysObjects.Name AS QueryName, 
           IIf(IsNull([Name1]),
               Right([Expression],Len([Expression])-InStr(1,[Expression],".")),[name1])
               AS FieldName
      FROM MSysQueries INNER JOIN MSysObjects 
        ON MSysQueries.ObjectId = MSysObjects.Id
     WHERE MSysQueries.Attribute=6;
    
    0 讨论(0)
  • 2020-12-09 17:42

    Seems like this task was easier in older days. Likely this answer is highly version-dependent. It works for me in a quick test against an Access 2007 DB:

    select 
    Specs.SpecName AS TableName,
    Columns.FieldName
    from
    MSysIMEXColumns Columns
    inner join MSysIMEXSpecs Specs on Specs.SpecID = Columns.SpecID
    order by
    Specs.SpecName,
    Columns.FieldName
    
    0 讨论(0)
  • 2020-12-09 17:44

    I work in ms access far too much.

    The only way I know of to do this, would be using vba, and defining for example a recordset, and looping through the fields.

    Eg:

    Sub ListFields()
    
    dim rst as new adodb.recordset
    rst.open "SELECT * FROM SomeTable", CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
    ' Note: adOpenForwardOnly and adLockReadOnly are the default values '
    ' for the CursorType and LockType arguments, so they are optional here '
    ' and are shown only for completeness '
    
    dim ii as integer
    dim ss as string
    for ii = 0 to rst.fields.count - 1
        ss = ss & "," & rst.fields(ii).name
    next ii
    
    Debug.Print ss
    
    End Sub
    

    The string variable ss will contain a comma-delimited list of all the column names in the table named "SomeTable".

    With a little reformatting of the logic you should be able to insert this data into another table if you wanted to, then query it out.

    Does this help?

    0 讨论(0)
  • 2020-12-09 17:44

    You can simply use the Documenter tool. Go to Database Tools > Database Documenter, select the table and press OK.

    0 讨论(0)
提交回复
热议问题