Results 1 to 3 of 3
  1. #1
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    365

    Datetype drfintions


    Code:
    Dim db As DAO.Database    Set db = CurrentDb
        Dim t As DAO.TableDef
        Set t = db.TableDefs("table1")
        Dim f As DAO.Field
            For Each f In t.Fields
                Debug.Print  f.Type
            Next
    This shows numbers e.g 1, 4, 10 etc. How do you convert to names like "short text", "number" etc.
    And to set a datatype for a field, do you use numbers or names ?
    Thanks

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    See this for Datatypes

    Here's a routine you could confirm and adapt.

    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: FieldTypeName
    ' Purpose: To return the name of the field type
    ' Procedure Kind: Function
    ' Procedure Access: Public
    ' Parameter fld (Field): name of the field
    ' Return Type: String
    ' Author: Jack
    ' Date: 04-May-23
    ' Orig: Allen Browne,  referenced here: MajP AWF
    'From: https://www.access-programmers.co.uk/forums/threads/vba-how-to-get-the-number-of-decimal-places-for-a-table-field.327483/post-1874935
    ' ----------------------------------------------------------------
    Function FieldTypeName(fld As DAO.Field) As String
        'Purpose: Converts the numeric results of DAO Field.Type to text.
        'Source/Copyright: Allen Browne
        'URL: http://allenbrowne.com/func-06.html
        Dim strReturn As String    'Name to return
    
        Select Case CLng(fld.Type) 'fld.Type is Integer, but constants are Long.
            Case dbBoolean: strReturn = "Yes/No"            ' 1
            Case dbByte: strReturn = "Byte"                 ' 2
            Case dbInteger: strReturn = "Integer"           ' 3
            Case dbLong                                     ' 4
                If (fld.Attributes And dbAutoIncrField) = 0& Then
                    strReturn = "Long Integer"
                Else
                    strReturn = "AutoNumber"
                End If
            Case dbCurrency: strReturn = "Currency"         ' 5
            Case dbSingle: strReturn = "Single"             ' 6
            Case dbDouble: strReturn = "Double"             ' 7
            Case dbDate: strReturn = "Date/Time"            ' 8
            Case dbBinary: strReturn = "Binary"             ' 9 (no interface)
            Case dbText                                     '10
                If (fld.Attributes And dbFixedField) = 0& Then
                    strReturn = "Text"
                Else
                    strReturn = "Text (fixed width)"        '(no interface)
                End If
            Case dbLongBinary: strReturn = "OLE Object"     '11
            Case dbMemo                                     '12
                If (fld.Attributes And dbHyperlinkField) = 0& Then
                    strReturn = "Memo"
                Else
                    strReturn = "Hyperlink"
                End If
            Case dbGUID: strReturn = "GUID"                 '15
    
            'Attached tables only: cannot create these in JET.
            Case dbBigInt: strReturn = "Big Integer"        '16
            Case dbVarBinary: strReturn = "VarBinary"       '17
            Case dbChar: strReturn = "Char"                 '18
            Case dbNumeric: strReturn = "Numeric"           '19
            Case dbDecimal: strReturn = "Decimal"           '20
            Case dbFloat: strReturn = "Float"               '21
            Case dbTime: strReturn = "Time"                 '22
            Case dbTimeStamp: strReturn = "Time Stamp"      '23
    
            'Constants for complex types don't work prior to Access 2007 and later.
            Case 101&: strReturn = "Attachment"         'dbAttachment
            Case 102&: strReturn = "Complex Byte"       'dbComplexByte
            Case 103&: strReturn = "Complex Integer"    'dbComplexInteger
            Case 104&: strReturn = "Complex Long"       'dbComplexLong
            Case 105&: strReturn = "Complex Single"     'dbComplexSingle
            Case 106&: strReturn = "Complex Double"     'dbComplexDouble
            Case 107&: strReturn = "Complex GUID"       'dbComplexGUID
            Case 108&: strReturn = "Complex Decimal"    'dbComplexDecimal
            Case 109&: strReturn = "Complex Text"       'dbComplexText
            Case Else: strReturn = "Field type " & fld.Type & " unknown"
        End Select
    
        FieldTypeName = strReturn
        End Function

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    TypeName is often easier than getting the type (as a number) and converting/relating that number to a string name.

    EDIT - to elaborate: Debug.Print TypeName(f)
    If f refers to a control, then this is one of the rare times you'd need to use Value property if you want to know the type name of the value and not the control:
    TypeName(f.Value)
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums