Err Msg Using CInt() Function: Type Mismatch

Last reviewed: July 29, 1997
Article ID: Q111868
The information in this article applies to:
  • Microsoft Visual Basic Programming System, Applications Edition, version 1.0 --------------------------------------------------------------------

SUMMARY

In Microsoft Visual Basic Programming System, Applications Edition, when you use the CInt() function with a string that cannot be converted to a number, you receive the following error message:

   Run-time error '13':

   Type Mismatch

MORE INFORMATION

The CInt function converts an expression to an integer. You can use any valid numeric or string expression, but the string expression must be able to be converted to a number. The Visual Basic Reference states that you should use the CInt function instead of the Val function to provide internationally aware conversions from any other data type to Integer. However, the two functions do not behave exactly the same.

When you use the Val function to return the numbers contained in a string, only the first numeric characters in the string are returned. While the Val function returns only the numeric part of a string expression, the CInt function returns the error message above if the string expression cannot be evaluated as a number.

The following is a table of expressions and values returned by the CInt and the Val functions.

   Expression                    Value Returned
   --------------------------------------------

   Val("1726 56th Ave NE")       172656
   CInt("1726 56th Ave NE")      Error message
   Val("asdf")                   0
   Cint("asdf")                  Error message
   Val("1,000")                  1
   CInt("1,000")                 1000
   Val("1.34")                   1.34
   CInt("1.34")                  1

Because the Val function returns the value 0 when used with an expression that contains either 0 or no numbers at all, you cannot use it to test whether an input string is a valid number. You can, however, use the CInt function in a procedure to determine whether an input string is a valid number.

REFERENCES

For more information about the CInt Function, choose the Search button in the Visual Basic Reference and type:

    CInt


Additional query words: W_eXcel
Keywords : kberrmsg kbprg
Version : 1.00
Platform : WINDOWS


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.

Last reviewed: July 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.