INF: Function to Return a Variant's Data Type Name

    Article ID: Q120496
    Creation Date: 13-SEP-1994
    Revision Date: 19-SEP-1996

    The information in this article applies to:

    • Microsoft Access versions 1.0, 1.1, 2.0

    SUMMARY

    Moderate: Requires basic macro, coding, and interoperability skills.

    The VarType() function returns a number (from 0 to 8) indicating the data type used to store a Variant in Access Basic. This article describes a sample user-defined Access Basic function that you can use to return a text description of the data type, instead of just a number.

    This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x, or the "Building Applications" manual in version 2.0.

    MORE INFORMATION

    The following table lists the numbers returned by the VarType() function and their data type descriptions:

       Number Returned   Data Type Description
       ---------------------------------------
             0                 Empty
             1                 Null
             2                 Integer
             3                 Long
             4                 Single
             5                 Double
             6                 Currency
             7                 Date
             8                 String
    
    
    The following steps demonstrate how to set up the sample function DataTypeName():
    1. Create a new module and enter the following lines in the Declarations section of the module:

            Option Compare Database
            Option Explicit
      
    2. Create the following function in the module.

      NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

            '================================================================
            ' This function returns a text description of the number returned
            ' by the VarType() function.
            '================================================================
            Function DataTypeName (MyValue)
               On Error goto bye
               DataTypeName = Choose(VarType(MyValue) + 1, "Empty", "Null", _
               "Integer","Long", "Single", "Double", "Currency", "Date", _
               "String")
               bye:
            End Function
      
    3. From the View menu, choose Immediate Window. Type the following line in the Immediate window, and then press ENTER:

            ?DataTypeName((Date()))
      

      The function returns "Date," indicating the Date/Time data type.

    4. Type the following line in the Immediate window, and then press ENTER:

            ?DataTypeName(Str$(1234.56))
      

      Note that the function returns "String," indicating a String data type.

    5. Type the following line in the Immediate window and then press ENTER:

            ?DataTypeName(Val("1234.56"))
      

      Note that the function returns "Double," indicating a Double data type.

    REFERENCES

    Microsoft Access "Language Reference," version 1.1, pages 65 and 492-495


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
©1997 Microsoft Corporation. All rights reserved. Legal Notices.

Additional reference words: 1.00 1.10 2.00
KBCategory: kbusage
KBSubcategory: PgmOthr