Results 1 to 6 of 6

Thread: How to check no. of characters in a textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2018
    Posts
    1

    How to check no. of characters in a textbox

    HI,

    I am creating a form where I want to enter a mobile number in a text box.
    The mobile number is of 10 digits only
    If it is Less than 10 digits or more than then 10 digits, I want to display a message box which says "Enter a valid number"

    Please help.
    I am fairly new to VBA.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: How to check no. of characters in a textbox

    1) Restrict your textbox to digits only input --> Tons of examples here
    2) You can use Len(MyTextBox.Text) to check for length --> If Len(MyTextBox.Text)<>10 Then MsgBox("KaBoom!")
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to check no. of characters in a textbox

    VBA questions should go into the VBA forum (MS Office), not VB. The environment is different enough to make answers to questions different even when the core programming syntax is similar.

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: How to check no. of characters in a textbox

    Huh?

    Missed the last line in the first post.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to check no. of characters in a textbox

    You can also set the max length of a text box which will restrict entry to no more than the given number of characters.
    You also have the option of using the masked edit box which will give you more control over the format of the entry and the way it is displayed to the user.

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: How to check no. of characters in a textbox

    As suggested by datamiser:

    Place a masked edit control on your form (add the component: "Microsoft Masked Edit Control 6.0" to your ToolBox)
    In its Mask Property, type "###-###-####" (without the quotes)
    Change the Prompt Include Property to FALSE

    That is all you should need.

Posting Permissions

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



Click Here to Expand Forum to Full Width