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

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

    This is not SQL, but this question is the top Google result for someone like me who just needs to list out the field names needed for a query to select since Access does not support "* except foo, bar" for getting 99% of a table.

    Answer adapted from a social.msdn.com answer by Patrick Wood, Access MVP https://social.msdn.microsoft.com/Forums/office/en-US/1fe5546b-db3f-4e17-9bf8-04f4dee233b7/how-to-list-all-the-field-names-in-a-specified-table?forum=accessdev

    Change tablename to your name in the module. This Function should be at the global level:

    Function ListTdfFields()
        ' NOT doing DIMs, since then you must enable/attach ADODB or DAO
        ' Dim db As ADO.Database
        Set db = CurrentDb
        tablename = "tblProductLicense"  ' <=== YOUR TABLE NAME HERE
        Set tdf = db.TableDefs(tablename)
        For Each fld In tdf.Fields
            Debug.Print tablename; ".["; fld.Name; "]," ; 
            ' remove ending ; for 1 line per field
        Next
        Debug.Print ""
        Set tdf = Nothing
        Set db = Nothing
    End Function
    

    Then add a macro RunCode ListTdfFields() and run it. Output will be sent to the Immediate window of the VBA design view for the module.

提交回复
热议问题