+ Reply to Thread
Results 1 to 12 of 12

Numbers only in text box

  1. #1
    Registered User
    Join Date
    10-24-2005
    Posts
    55

    Numbers only in text box

    Hi

    Is it possible to make a error message (when pressing a command button) appear if a user inputs into a textbox (on a user form) Text instead of numbers

    The number format would also have a decimal e.g. £ 120.05 or sometimes not £145. So if text was placed in the text box e.g. "Ring for price" a error message would appear asking to remove text and input price...

    Many thanks

    RAW

  2. #2
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    One idea would be to stop the user entering non numirc number si the texbox when typing into the text box. If thisis of interest to you I can post some code for you.
    VBA - The Power Behind the Grid

    Posting a sample of your workbook makes it easier to look at the Issue.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    I'd put that routine in the TextBox_Change routine. If the user presses a no-good character, your routine will immediatly delete it from TextBox.Text.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    You could use IsNumeric in the textbox exit event.

    The only problem I can see with that is that you seem to be indicating the user is entering the £ symbol.

    Is that right?

  5. #5
    Registered User
    Join Date
    10-24-2005
    Posts
    55
    Hi

    No the user would not have to input the £ sign only the amount (the £ sign is done with a label next to the text box)

    Quote Originally Posted by stevebriz
    One idea would be to stop the user entering non numirc number si the texbox when typing into the text box. If thisis of interest to you I can post some code for you.
    That would be very helpfully thank you

    RAW

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Raw,

    You can use a Command Button to validate the TextBox entry, but wouldn't it be easier to have the validation done automatically after the user has made the entry? I will give you code for both ways.

    Command Button:
    Please Login or Register  to view this content.
    TextBox Event:
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    These userform event driven routines will keep the user from entering an invalid entry.
    It would help the user if you either provided a label explaining what format you want or un-commented the message box in the _Change routine.
    Please Login or Register  to view this content.
    Last edited by mikerickson; 05-05-2007 at 10:57 PM.

  8. #8
    Registered User
    Join Date
    10-24-2005
    Posts
    55
    Hi guy

    thanks for the code Leith Ross & Mikerickson I have tried both (not at the same time!) to see what one would suit us the best but i get a error

    With the
    Please Login or Register  to view this content.
    Error that displays is "compile error sub or function not defined", with the IsCurrency begin highlighted yellow

    With the other code

    Please Login or Register  to view this content.
    The
    Please Login or Register  to view this content.
    Part gives me a error, VBA hightlights this in yellow

    Many thanks for the code

    Thanks
    RAW

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Raw,

    Use this code in the TextBox_BeforeUpdate() event. If the value can not be converted to a valid currency, a message box is displayed telling the user the entry is not valid, clears the textbox, and forces the user to enter a valid amount.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Hmm..it works on my machine. What error is caused? You might try this variation:
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    10-24-2005
    Posts
    55
    Hi

    Quote Originally Posted by mikerickson
    Hmm..it works on my machine. What error is caused? You might try this variation:
    Please Login or Register  to view this content.
    The error i get with this code and the one before is "Run time error 5" As soon as i press the run button to test the form this error displays

    It must be something i am doing but can not see what

    Many thanks

    RAW

  12. #12
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    This might help, it sounds like an enviroment senesitve situation, which this might cure
    Please Login or Register  to view this content.
    if it still persists, use the immediate window to check the value of inputStr, its length and isNull(inputStr).

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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