+ Reply to Thread
Results 1 to 8 of 8

Textbox if empty validation code

  1. #1
    Registered User
    Join Date
    03-02-2008
    Posts
    42

    Textbox if empty validation code

    I have created the below userform.

    userform.xls

    When the textboxes are empty the label will change colour and indicate that the person needs to put value in the textbox.


    I have about ten textboxes

    What I can't seem to do is:
    1) get all the labels that have textboxes empty to change coulour at one go, my Code seems to do the first one then go to the next.

    2) can't get the label to change back to original colour when the textbox is not empty

    Please help

    Thanks

    Please Login or Register  to view this content.

  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
    Hello Panic,

    I change your code and placed the the "If" statements into the Exit event of the each [b}TextBox[/b]. The attach file has the changes already made. The user form code is shown below...
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    03-02-2008
    Posts
    42
    Leith Ross

    Thanks for that, but exit code doesn't really work for me.

    I will have 10 to 15 textboxes, spread acroos a multi-page, exit code will only work if they have the curser in the text boxes.

    What I wanted was all the text boxes that are empty, have their labels turned red in the instance the save button is pressed.

    Hence they will only press the save button once or twice, instead of the reminder message coming up every save or exit textbox.

    I'll give an example "out of 10 textboxes, three are empty. Press Save, their corresponding labels need to change to red with a message coming saying the they need to complete all red labeled boxes"

    I hope this clear

    Thanks

    Panic

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You can easily check if all textboxes are completed. It would be more difficult to colour the label next to it. This code will colour all empty textboxes


    Please Login or Register  to view this content.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    03-02-2008
    Posts
    42
    That might work.

    If the boxes are empty then they will be red but if the boxes have value they would be white.

    what is the code for boxes not being empty

    Please Login or Register  to view this content.

    Thanks

    Panic

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    What you need is to highlight the empty cells using the earlier code. Adding this code to each textbox will clear the colour on entering it, simply amend and add it to each textbox

    Please Login or Register  to view this content.

  7. #7
    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 Panic,

    This version of your file has 2 macros. One macro loads a control array that has the names of the Text Boxes and the Labels associated with them. The second macro will validate the entries using the control array. It will set focus to the first empty text box and change the label caption color to red.

    You will need to expand the control array. There is one array for the text boxes and another array for the labels. These are combined to make the control array whose dimensions are 2, n. The first subscript (0, n) holds the name of the text box, and the second subscript(1, n) holds the name of the label for that text box. I have marked the code in red.
    Module Code
    Please Login or Register  to view this content.
    Macro Module Code
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    03-02-2008
    Posts
    42
    Hi Leith Ross,

    This is excellent!!!!!

    I added few comboboxes and it works, This code lets me select the boxes (controls) i want included in this validation

    I'll see if I can use this code to empty all of the itemised boxes too

    "Unless you can help"

    anyway

    Thank you very much

    Panic

+ 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