Query to get field name with datatype and size (1 Viewer)

Tor_Fey

Registered User.
Local time
Today, 06:54
Joined
Feb 8, 2013
Messages
121
Does anyone have a SQL query to retrieve field names with datatype and size from all tables within an access database?

I was hoping to use something like the below code... But, I want the SQL code to know the current database and tables without me having to specify them

SQL:
SELECT column_name as 'Column Name', data_type as 'Data Type',
character_maximum_length as 'Max Length'
FROM information_schema.columns
WHERE table_name = 'tblUsers'

In the following example, the code can be just cut and paste from one database to another without having to change anything, and this is what i would like to achieve with the new SQL code to query field names with data types and size.

SQL:
SELECT MSysObjects.name, MSysObjects.DateCreate, MSysObjects.DateUpdate
FROM MSysObjects
WHERE (((MSysObjects.name) Not Like '~*' And (MSysObjects.name) Not Like 'MSys*') AND ((MSysObjects.type) In (1,4,6)))
ORDER BY MSysObjects.DateUpdate DESC;

Your help is always appreciated

Kind regards
Tor Fey
 

Minty

AWF VIP
Local time
Today, 06:54
Joined
Jul 26, 2013
Messages
10,366
Try something like
SQL:
SELECT T.Name                   AS TableName,
       Schema_name(T.schema_id) AS SchemaName,
       C.Name                   AS ColumnName,
       Ty.Name                  AS ColumnDataType,
       C.is_nullable            AS IsNullAble,
       C.is_identity            AS IsIdentity
FROM   sys.tables T
       INNER JOIN sys.columns C
               ON T.OBJECT_ID = C.OBJECT_ID
       INNER JOIN sys.types Ty
               ON C.system_type_id = Ty.system_type_id
WHERE  T.is_ms_shipped = 0
ORDER  BY T.name
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:54
Joined
Mar 14, 2017
Messages
8,774
This should not be in the sql server forum
 

Tor_Fey

Registered User.
Local time
Today, 06:54
Joined
Feb 8, 2013
Messages
121
Hi Minty;

I tried this code and get the following error:

SyntaxError.png


Kind Regards
Tor Fey
 

Minty

AWF VIP
Local time
Today, 06:54
Joined
Jul 26, 2013
Messages
10,366
This should not be in the sql server forum
Hang on - I thought you wanted this for SQL server - your code is for SQL and you state
I was hoping to use something like the below code... But, I want the SQL code to know the current database and tables without me having to specify them

SQL:
SELECT column_name as 'Column Name', data_type as 'Data Type',
character_maximum_length as 'Max Length'
FROM information_schema.columns
WHERE table_name = 'tblUsers'

Although I missed the bit at the beginning that says
datatype and size from all tables within an access database?

In which case you need need to use the TableDefs Collection.

Try this https://www.devhut.net/2010/06/10/ms-access-vba-list-table-fields/

But loop through it.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:54
Joined
Feb 19, 2002
Messages
43,223
This is a piece of code that populates a table with the information. Access doesn't expose a MSYS table with this info so you have to get it from the fields collection. To use the code define tblTableFields with the column names referenced in the code. This is from a documentation tool so it documents tables in the database specified by the field on the frmPrintDoc. You can just change it to reference the CurrentDB()
Code:
Sub Create_tblTableFields()

''''Reference Field object definition https://msdn.microsoft.com/en-us/library/office/ff193203.aspx

    Dim db As dao.Database
    Dim tblLoop As dao.TableDef
    Dim fldLoop As dao.Field
    Dim TD1 As dao.TableDef
    Dim QD1 As dao.QueryDef
    Dim TempSet1 As dao.Recordset
    Dim strDatabase As String
    Dim ThisDB As dao.Database
    Dim CountTables As Integer
   
   ''''On Error GoTo Create_tblTableFields_Error

  On Error GoTo Err_Create_tblTableFields
    'strDatabase = "C:\hartman\LinkDB2.mdb"
    strDatabase = Forms!frmPrintDoc!txtDBName
   
    CountTables = 0
    Set ThisDB = CurrentDb()
    If strDatabase = "" Then
        Set db = CurrentDb()
    Else
        Set db = DBEngine.Workspaces(0).OpenDatabase(strDatabase)
    End If
   
    db.Containers.Refresh
   
    Set TD1 = ThisDB.TableDefs!tblTableFields
    Set TempSet1 = TD1.OpenRecordset

    ' Loop through TableDefs collection.
    For Each tblLoop In db.TableDefs
        ' Enumerate Fields collection of each
        ' TableDef object.
        CountTables = CountTables + 1
        Forms!frmPrintDoc!txtTableCount = CountTables
        Forms!frmPrintDoc!txtTableName = tblLoop.Name
        Forms!frmPrintDoc.Repaint
               
        If Left(tblLoop.Name, 4) = "MSys" Or Left(tblLoop.Name, 2) = "xx" Or Left(tblLoop.Name, 2) = "zz" Or Left(tblLoop.Name, 1) = "~" Then
        Else
            For Each fldLoop In tblLoop.Fields
                TempSet1.AddNew
                TempSet1!TableName = tblLoop.Name
                TempSet1!FieldName = fldLoop.Name
                TempSet1!OrdinalPosition = fldLoop.OrdinalPosition
                TempSet1!AllowZeroLength = fldLoop.AllowZeroLength
                TempSet1!DefaultValue = fldLoop.DefaultValue
                TempSet1!Size = fldLoop.Size
                TempSet1!Required = fldLoop.Required
                TempSet1!Type = fldLoop.Type
                TempSet1!ValidationRule = fldLoop.ValidationRule
                TempSet1!Attributes = fldLoop.Attributes
                On Error Resume Next ' the following property is only available when it is not null
                TempSet1!Description = fldLoop.Properties("Description")
                TempSet1!FieldType = GetType(fldLoop.Type)
                TempSet1!Caption = fldLoop.Properties("Caption")
                If fldLoop.Attributes And dbAutoIncrField Then  'performs bitwise operation
                    TempSet1!AutoNum = True
                    TempSet1!Required = True
                Else
                    TempSet1!AutoNum = False
                End If
                TempSet1.Update
            Next fldLoop
        End If
    Next tblLoop

Exit_Create_tblTableFields:
    db.Close
    Exit Sub

Err_Create_tblTableFields:
    Select Case Err.Number
        Case 3043, 3055
            MsgBox "Please select a valid database.  Error #" & Err.Number, vbOKOnly
        Case 91   ' db was not opened so it cannot be closed.
            Exit Sub
        Case Else
            MsgBox Err.Number & " (" & Err.Description & ") in procedure Create_tblTableFields of Module DocumentCollections"
    End Select
    Resume Exit_Create_tblTableFields
   Exit Sub
End Sub
 

Users who are viewing this thread

Top Bottom