GET type of fields

User avatar
sal21
PlatinumLounger
Posts: 4346
Joined: 26 Apr 2010, 17:36

GET type of fields

Post by sal21 »

The code:
Debug.Print RS.Fields(K).Type

return a numeric value to identify the type of filed.
possible to get the type value, similar, string, numeri, date, ecc...

i have see in google, for each numeric value, exists a table in clear

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: GET type of fields

Post by StuartR »

StuartR


User avatar
HansV
Administrator
Posts: 78402
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: GET type of fields

Post by HansV »

See DataTypeEnum for a list of field types. You will need only a few of them.
You could use code like this:

Code: Select all

    Dim strFieldType As String
    Select Case RS.Fields(K).Type
        Case adBoolean
            strFieldType = "Boolean"
        Case adCurrency
            strFieldType = "Currency"
        Case adDate
            strFieldType = "Date"
        Case adDouble
            strFieldType = "Double"
        Case adInteger
            strFieldType = "Long Integer"
        Case adSingle
            strFieldType = "Single"
        Case adSmallInt
            strFieldType = "Integer"
        Case adVarChar
            strFieldType = "Text"
        Case Else
            strFieldType = "Other Field Type"
    End Select
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4346
Joined: 26 Apr 2010, 17:36

Re: GET type of fields

Post by sal21 »

HansV wrote:
02 Oct 2022, 21:43
See DataTypeEnum for a list of field types. You will need only a few of them.
You could use code like this:

Code: Select all

    Dim strFieldType As String
    Select Case RS.Fields(K).Type
        Case adBoolean
            strFieldType = "Boolean"
        Case adCurrency
            strFieldType = "Currency"
        Case adDate
            strFieldType = "Date"
        Case adDouble
            strFieldType = "Double"
        Case adInteger
            strFieldType = "Long Integer"
        Case adSingle
            strFieldType = "Single"
        Case adSmallInt
            strFieldType = "Integer"
        Case adVarChar
            strFieldType = "Text"
        Case Else
            strFieldType = "Other Field Type"
    End Select
I had thought too but 34 enum in select case ...
if is the only way, ok.

User avatar
HansV
Administrator
Posts: 78402
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: GET type of fields

Post by HansV »

You probably never use most of the field types - only some of them apply to a Microsoft Access database. The code that I posted will probably be sufficient.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4346
Joined: 26 Apr 2010, 17:36

Re: GET type of fields

Post by sal21 »

HansV wrote:
02 Oct 2022, 22:06
You probably never use most of the field types - only some of them apply to a Microsoft Access database. The code that I posted will probably be sufficient.
infact ....

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: GET type of fields

Post by SpeakEasy »

If only there were a way to list enums by name and value ... and, if you are working from Vb6 as I believe you are, there is! But it is a bit advanced and may in the end be more effort than the Case statement.

Happy to post though, if you are interested, sal21

User avatar
sal21
PlatinumLounger
Posts: 4346
Joined: 26 Apr 2010, 17:36

Re: GET type of fields

Post by sal21 »

SpeakEasy wrote:
03 Oct 2022, 08:19
If only there were a way to list enums by name and value ... and, if you are working from Vb6 as I believe you are, there is! But it is a bit advanced and may in the end be more effort than the Case statement.

Happy to post though, if you are interested, sal21
YES, i'm interested, if you want :grin:

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: GET type of fields

Post by SpeakEasy »

OK. You need a form with a textbox set to multiline (and with a vertical scrollbar enabled, preferably). You will also need to add a reference to the Typelib Information library. Then:

Code: Select all

Option Explicit

' Requires a multiline textbox (preferably with vertical scrollbar enabled)
Private Sub Form_Load()
    Text1.Text = GetADOEnumMemberName("DataTypeEnum")
End Sub


' Requires a reference to Typelib Information to be added to the project. If distributing your executable, make sure it is packaged with this library.
' This example is hardcoded to look at ADO enums, but with appropriate parameter passed to TLI.TypeLibInfoFromFile can work with any ActiveX library
Public Function GetADOEnumMemberName(strEnumName As String, Optional varVal As Variant) As Variant

    Dim TLIApp As TLIApplication
    Dim myConstant As ConstantInfo
    Dim EnumMember As MemberInfo
    Dim ADOTLI As TypeLibInfo

    Set TLIApp = New TLIApplication

    ' Get our type library info from the relevant file
    ' This is the line that makes this example specific to the ADO object library
    Set ADOTLI = TLI.TypeLibInfoFromFile("c:\program files\common files\system\ado\msado15.dll")
 
    ' Loop through members of libraries constants
    For Each myConstant In ADOTLI.Constants
        If myConstant.TypeKind = TKIND_ENUM And myConstant.Name = strEnumName Then
            For Each EnumMember In myConstant.Members
                If Not IsMissing(varVal) Then
                    Select Case TypeName(varVal)
                        Case "String"
                            If EnumMember.Name = varVal Then GetADOEnumMemberName = EnumMember.Value
                        Case "Integer"
                            If EnumMember.Value = varVal Then GetADOEnumMemberName = EnumMember.Name
                    End Select
                Else
                ' List all members of this enum in debug window if you like
                ' Debug.Print EnumMember.Name, EnumMember.Value
                ' it is left as an exercise to the reader to determine a nice way of returning this info to the calling procedure...
                ' This example happens to return as a list in a string, but you can choose other ways, eg a dictionary.
                 GetADOEnumMemberName = GetADOEnumMemberName & EnumMember.Name & " " & EnumMember.Value & vbCrLf
                End If
            Next
        End If
    Next
End Function

User avatar
sal21
PlatinumLounger
Posts: 4346
Joined: 26 Apr 2010, 17:36

Re: GET type of fields

Post by sal21 »

SpeakEasy wrote:
03 Oct 2022, 10:50
OK. You need a form with a textbox set to multiline (and with a vertical scrollbar enabled, preferably). You will also need to add a reference to the Typelib Information library. Then:

Code: Select all

Option Explicit

' Requires a multiline textbox (preferably with vertical scrollbar enabled)
Private Sub Form_Load()
    Text1.Text = GetADOEnumMemberName("DataTypeEnum")
End Sub


' Requires a reference to Typelib Information to be added to the project. If distributing your executable, make sure it is packaged with this library.
' This example is hardcoded to look at ADO enums, but with appropriate parameter passed to TLI.TypeLibInfoFromFile can work with any ActiveX library
Public Function GetADOEnumMemberName(strEnumName As String, Optional varVal As Variant) As Variant

    Dim TLIApp As TLIApplication
    Dim myConstant As ConstantInfo
    Dim EnumMember As MemberInfo
    Dim ADOTLI As TypeLibInfo

    Set TLIApp = New TLIApplication

    ' Get our type library info from the relevant file
    ' This is the line that makes this example specific to the ADO object library
    Set ADOTLI = TLI.TypeLibInfoFromFile("c:\program files\common files\system\ado\msado15.dll")
 
    ' Loop through members of libraries constants
    For Each myConstant In ADOTLI.Constants
        If myConstant.TypeKind = TKIND_ENUM And myConstant.Name = strEnumName Then
            For Each EnumMember In myConstant.Members
                If Not IsMissing(varVal) Then
                    Select Case TypeName(varVal)
                        Case "String"
                            If EnumMember.Name = varVal Then GetADOEnumMemberName = EnumMember.Value
                        Case "Integer"
                            If EnumMember.Value = varVal Then GetADOEnumMemberName = EnumMember.Name
                    End Select
                Else
                ' List all members of this enum in debug window if you like
                ' Debug.Print EnumMember.Name, EnumMember.Value
                ' it is left as an exercise to the reader to determine a nice way of returning this info to the calling procedure...
                ' This example happens to return as a list in a string, but you can choose other ways, eg a dictionary.
                 GetADOEnumMemberName = GetADOEnumMemberName & EnumMember.Name & " " & EnumMember.Value & vbCrLf
                End If
            Next
        End If
    Next
End Function
WOW!
TKS BRO.

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: GET type of fields

Post by SpeakEasy »

You might want to look at the second parameter as well - you can use it to look up a specific enumeration item either by name or by value

e.g

GetADOEnumMemberName("DataTypeEnum", 7)

will return adDate

and

GetADOEnumMemberName("DataTypeEnum", "adDate")

will return 7

User avatar
sal21
PlatinumLounger
Posts: 4346
Joined: 26 Apr 2010, 17:36

Re: GET type of fields

Post by sal21 »

:thankyou:
SpeakEasy wrote:
03 Oct 2022, 18:47
You might want to look at the second parameter as well - you can use it to look up a specific enumeration item either by name or by value

e.g

GetADOEnumMemberName("DataTypeEnum", 7)

will return adDate

and

GetADOEnumMemberName("DataTypeEnum", "adDate")

will return 7