+ Reply to Thread
Results 1 to 13 of 13

Code to control data entry in Combo-box situated in userform

  1. #1
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Code to control data entry in Combo-box situated in userform

    Hi.
    Below is a code situated in a combobox in my userform.
    The object of the code is that if the user enters anything but a number or the word "LISTED" then a message box should pop up and then clear the combobox.
    the last part of the code does the following: once i enter a number in the box the code should automatically insert right after the number a value of " .00%", so if the user enters a 50 in the box the code should automatically turn it into a 50.00%
    The trouble i believe is that the code is thinking that this addition of " .00%" is part of the instance of being a non-numeric value and the message box therefore gets displayed

    Please Login or Register  to view this content.
    Any help on this would be greatly appreciated.

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

    Re: Code to control data entry in Combo-box situated in userform

    Hi kosherboy,

    Your main problem was that the code was running in an endless loop each time 'PriceBoxVend()' changed the value (by adding the '%' sign).

    I made the following significant changes to the code:
    a. I added the Global Boolean variable (with module Scope) 'bGblInhibitUserFormEvents'. The variable is used to stop 'PriceBoxVend_Change()' from recognizing changes to the ComboBox value while you are inside Sub PriceBoxVend_Change(). We need this variable because there is no UserForm equivalent of Application.EnableEvents.
    b. When you added the .00% sign, you also added a blank in between the number and the decimal point. That caused the value to be INVALID. I removed the extra SPACE character.
    c. I used variable sValue and xValue to be temporary copies of the ComboBox value. I did this because it was easier for me to read, and because it stopped unnecessary Change Events from being generated.
    d. Other changes were made just because it is my programming style, and is easier for me to understand the code when it is written in my style.


    Please Login or Register  to view this content.
    I hope this helps.

    Lewis

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: Code to control data entry in Combo-box situated in userform

    deleted by bvj
    Last edited by protonLeah; 01-02-2015 at 01:18 AM.
    Ben Van Johnson

  4. #4
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to control data entry in Combo-box situated in userform

    Hi Lewis,
    Thanks for taking your time on my problem.
    Small issue, when i added your code to take the place of my old code one of my variables bugged out. I'm not sure what is causing the issue but here is a small summary of what i have:
    This code runs when the userform is called:
    Please Login or Register  to view this content.
    Not sure if this would help to understand my situation but i use a public variable for the following (this way i can call it anywhere in my project:
    Please Login or Register  to view this content.
    Thanks

  5. #5
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to control data entry in Combo-box situated in userform

    Lewis,
    I closed my workbook and tried your code again and it worked somewhat.
    The code lets me enter 1 digit but when i enter a second digit like "55" the msg box popped up and the combobox displayed this "5.00%5" instead of "55.00%".
    Also, i would like the code to be flexible and allow decimal points as well.

    Thanks again.

  6. #6
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to control data entry in Combo-box situated in userform

    Quote Originally Posted by protonLeah View Post
    [CODE]Option Explicit
    Hi protonleah,
    Thanks for responding.
    Your code still had the msg box pop up when i tried entering a number.

  7. #7
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to control data entry in Combo-box situated in userform

    I'm not sure if this would be helpful but i use the same idea, mentioned in my first post, to actual cells via data validation.
    Here is my data validation formula:
    Please Login or Register  to view this content.
    So this restricts me to letting me only enter a number (which will turn to a percentage) or the word LISTED and in this case also the word N/A.

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

    Re: Code to control data entry in Combo-box situated in userform

    I appreciate the rep points, but I think the thanks are a little bit premature.

    The code lets me enter 1 digit...
    The Change Event is not very friendly. It processes one keystroke at a time. If you're patient, and wait until the % comes up, you can then enter another number wherever you want, and you will eventually get the number you want.

    --------------

    An alternative is the Exit event, which lets you type as many characters as you want and only processes when the control loses focus. Of the two, I prefer the Change Event.

    For software I write, I don't do data checking until I am ready to process everything. When I click on a CommandButton to process, data is checked before CommandButton processing is allowed to continue.

    ---------------------

    For what i't worth, here is working Exit event code. Change the name of the Change event code (put an 'x' in front of the name), when testing the Exit event code. The two don't play nicely together.
    Please Login or Register  to view this content.
    Lewis

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Code to control data entry in Combo-box situated in userform

    I prefer the BeforeUpdate event to the Exit event

    Please Login or Register  to view this content.
    You might try using this version which allows the user to enter "23 %"

    Please Login or Register  to view this content.
    Last edited by mikerickson; 01-01-2015 at 10:28 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  10. #10
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to control data entry in Combo-box situated in userform

    Quote Originally Posted by LJMetzger View Post
    I appreciate the rep points, but I think the thanks are a little bit premature.

    Lewis
    Hi Lewis.
    I gave you the rep because i appreciate the time you took in trying to solve my problem.
    P.S. rep points can be given out to every response and doesn't cost me anything

    In reference to your latest code, it works great! I don't, however, understand why you have to have everything updated by the command button. Because let's say a user is entering lots of data and some of that data does not compile with the program and when he presses the command button at the end 15 msg boxes pop up showing everything he did wrong when he could have corrected it, or have the software correct it while he was entering the data.
    Also, in my case i need the userform to update as the user goes along so that the userform will be userfreindly
    One last thing, if i stick to your code are there any issues that might occur?

    Anyway, thanks for the code, i really appreciate that you took your time in working on the code and explaining your work to me. Your knowledge in vba is also very much appreciated.

  11. #11
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to control data entry in Combo-box situated in userform

    Quote Originally Posted by mikerickson View Post
    I prefer the BeforeUpdate event to the Exit event
    Mikerickson, looks like you have a different approach which is similar to Lewis's and works like a charm as well.

    Thank you Mikerickson for your time, all the best!

    This thread is solved.
    Last edited by kosherboy; 01-02-2015 at 01:25 AM.

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

    Re: Code to control data entry in Combo-box situated in userform

    Hi Moe,

    Thanks for the additional rep points.

    See the attached file which implements the following each in a different control:
    a. Change Event
    b. Exit Event
    c. Before Update Event
    d. ComboBox with No Events. Data entry from OptionButtons, ComboBox List and SpinButtons.
    e. Text Box with No Events. Data entry from Optionbuttons and SpinButtons. This control is locked to prevent incorrect data entry.

    Pros and Cons of Each Method:
    a. Change Event only handles user numerical input one digit at a time.
    b. Exit Event - FAILS to handle selections from the Combo Box until focus is moved to a different control.
    This is OK if the ComboBox list has correct values (which it should have).
    c. Before Update Event - has identical code and behaves the same as the Exit Event.
    d. ComboBox with no Event allows data entry from List or from data entry. Verification done when 'Process Data' is selected.
    e. TextBox with no Event is locked so data entry is either from 'OptionButtons' or from SpinButtons.


    I added 'Batch processing' with a 'Process Data' Command Button. It verifies data in each control. A status message is displayed in a 'Status Label' at the end of verification. If there are data input errors, the errors must be corrected before data processing is allowed.
    Please Login or Register  to view this content.
    Lewis
    Last edited by LJMetzger; 01-02-2015 at 01:28 PM.

  13. #13
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to control data entry in Combo-box situated in userform

    Hi Lewis,
    It was very generous of you to take your time in preparing and uploading the file, i understand it was time consuming and i appreciate it very much. I reviewed the file over the weekend and i see now the difference between the different options. This file will be great for future reference.

    Best regards,

    kosherboy

+ 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. Code works with Combo Box-Form Control, but not with Combo box - ActiveX Control
    By nangys in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-01-2014, 08:51 AM
  2. [SOLVED] Userform data entry code
    By Nitefox in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 18
    Last Post: 12-21-2013, 12:48 AM
  3. [SOLVED] Userform multipage control exit events code execution not completed before next user entry
    By jane serky in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-10-2013, 04:39 AM
  4. Pass Date value from custom calendar control to Userform Combo Box:
    By rvc81 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-23-2013, 12:59 AM
  5. how to insert calender control in excel userform combo box
    By excelkeechak in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2009, 10:18 AM

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