+ Reply to Thread
Results 1 to 4 of 4

Best way to error-check Me.tbxMyTextField.Value ???

  1. #1
    Registered User
    Join Date
    02-20-2012
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2010
    Posts
    25

    Best way to error-check Me.tbxMyTextField.Value ???

    I have a UserForm with a text field that I'm grabbing the value that the user entered:

    Please Login or Register  to view this content.
    I like to write my code a bulletproof as possible--no runtime errors for me! I'm checking that the field isn't empty, as you can see.

    Is there any other error-checking I can/should add after the following statement, to head off things going wrong in Excel VBA?

    Please Login or Register  to view this content.
    Thanks

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

    Re: Best way to error-check Me.tbxMyTextField.Value ???

    Hello btmtdk,

    I don't understand what you are doing. You do realize that any data typed into or pulled from a TextBox is a String Data type?

    The CStr method is used to coerce Numeric Data Types into String Data Types. This is completely unnecessary as your data is already a string.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Best way to error-check Me.tbxMyTextField.Value ???

    Hi btmtdk,

    Excellent question.

    When checking for a string you might want to:
    a. Eliminate leading and trailing blanks if they are not allowed
    b. Flag numbers if they are not allowed
    c. Flag numbers if they are not allowed in a certain position in the string
    d. Flag alpha text if not allowed in a certain position in the string
    e. Flag other characters if not allowed (e.g. %, $, #, etc.)

    When checking for a number:
    a. Make sure the field is not blank (if blank not allowed)
    b. Make sure the value is numeric
    c. Flag the value if it is not within the expected range

    There are many ways to process the value, including
    a. With each keystroke
    b. When the TextBox loses focus
    c. When user selects a CommandButton to process all data on the UserForm

    My personal preference is to do CommandButton driven processing. Any TextBox that has a bad value is color coded appropriately.

    Even though TextBoxes always return strings, I like to access the .Text attribute rather than the .Value attribute. I do this because when I access data in spreadsheets, I use analogous code.

    There is a little know attribute for UserForm controls called the .Tag attribute. It can be used to do anything you want. I like to use it to keep the current value. When the command button is activated, I can then compare all TextBox values to their previous values to see if any value has changed.
    Please Login or Register  to view this content.
    Here is typical code I would use:
    Please Login or Register  to view this content.
    Lewis

  4. #4
    Registered User
    Join Date
    02-20-2012
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Best way to error-check Me.tbxMyTextField.Value ???

    Thanks guys! Very helpful replies. Appreciate it! Thanks for sharing your best practices.

+ 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. [SOLVED] Error checking routine - check range values on various worksheets and report if error
    By TheRobsterUK in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-29-2014, 06:36 AM
  2. [SOLVED] VBA to error check sheets for any cell containing an error
    By joshnathan in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-09-2014, 06:20 AM
  3. I thought this would be easy (check for text, check error, do math)
    By bruinsrme in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-14-2013, 01:18 AM
  4. Replies: 2
    Last Post: 02-18-2013, 10:17 AM
  5. Replies: 1
    Last Post: 12-14-2012, 10:11 AM

Tags for this Thread

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