+ Reply to Thread
Results 1 to 4 of 4

Preventing An Error using CLng When Expression Contains Alpha

  1. #1
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Preventing An Error using CLng When Expression Contains Alpha

    I have a textbox (tb_empl_num2) in my userform. The user is expected to enter a 5 digit number.
    As the value entered into the textbox is text (despite all numbers being entered), I use clng(tb_empl_num2) to convert the text to a long number.

    I am trying to catch the error when the user enters something other than 5 numeric digits, perhaps something with a letter or symbol in it (eg "alpha"). If they attempt to enter such, the clng(tb_empl_number2) will cause an error since it can't convert the alpha characters.

    Please Login or Register  to view this content.
    is something I'm trying to do, but I'm getting a 'type mismatch error'

  2. #2
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Preventing An Error using CLng When Expression Contains Alpha

    This seems to do what I'm looking for ...

    Please Login or Register  to view this content.

  3. #3
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,915

    Re: Preventing An Error using CLng When Expression Contains Alpha

    You could use something like:

    Please Login or Register  to view this content.
    which is probably safer. For example CLng will accept something like "123E4" as a valid number.
    Rory

  4. #4
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Preventing An Error using CLng When Expression Contains Alpha

    Thank you Rory, very good lesson! I never thought about that possibility. Although unlikely, I'm sure if it can happen, it will eventually.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Access Expression Error
    By jeepjenn in forum Access Tables & Databases
    Replies: 2
    Last Post: 04-23-2015, 01:48 PM
  2. [SOLVED] Compile error: Constant expression required error when merging two Codes
    By Kezwick in forum Outlook Programming / VBA / Macros
    Replies: 8
    Last Post: 06-26-2013, 09:32 AM
  3. [SOLVED] Compile Error: Expected Expression, Syntax Error
    By gjohn282 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-20-2012, 11:28 PM
  4. [SOLVED] Error message of CLng
    By gordonf35 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-11-2012, 05:21 PM
  5. VB Error in CLng
    By SMILE in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-26-2011, 03:48 AM
  6. Error using Clng function in vba to convert string to long data type
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-05-2011, 06:12 AM
  7. VBA compilation error : expected expression error
    By raknahs in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-21-2010, 12:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1