+ Reply to Thread
Results 1 to 7 of 7

Data validation of dates entered using Calendar Control not working

  1. #1
    Registered User
    Join Date
    03-09-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Data validation of dates entered using Calendar Control not working

    Hi everyone,

    Been stuck on this date validation issue for about a week now, browsing online through forums and I've not been able to find a solution so hopefully someone out there can help me.

    I’m a first time VBA coder (and am slowly making my way through my newly purchased VBA for Dummies book), so please be kind!

    Basically, I’ve created a form (not a userform) in an Excel worksheet (Excel 2010) by using the ActiveX Controls and through much trial and error have managed to populate a couple of text boxes by using calendar control 8.0 - I tried using the datetimepicker but had some issues with the calendar appearing in 2 places on the worksheet and screwing up the formatting of the text box.

    The problem I have is when I try to validate the dates against each other.
    Here’s the scenario: I have an on-sale date and a price revert date and I need to make sure that the date that has been entered into the “price revert” date falls after the “on-sale” date – it doesn’t matter how long after, just as long as it is >= the on-sale.

    My current code is:

    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer

    Please Login or Register  to view this content.
    Where RevDate refers to the Price Revert textbox and OSDate refers to the On-sale textbox.
    I encounter the following issues with the above code
    1. ANY date I enter into the Price Revert textbox results in the msgbox appearing regardless of whether or not it is before the On-sale date.
    2. Even though the msgbox appears, it still allows me to enter an invalid date and move through the form.
    3. Once I’ve entered an invalid date, if I re-select another invalid date (for testing purposes), the msgbox does not appear.

    It seems a little contradictory, with the msgbox appearing and not appearing but I assure you I’ve encountered each of these issues.
    I’ve tried ignoring the textboxes completely and running the validation check against their respective linked cells in Sheet2 but to no avail. If it helps to attach the actual form, let me know.

    What I would like to do is for the “Invalid Date” message to appear only if the user selects a date that is before the on-sale.

    Also, just as an aside, would it be best for me to validate the dates in the calendar control or the text box control?

    Apologies if I sound like a complete moron, but I’m trying to teach myself. It appears I’m not a very good teacher.

    Thanks in advance for any help you can provide.
    Last edited by KateV; 03-10-2014 at 09:21 PM. Reason: code tags

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Data validation of dates entered using Calendar Control not working

    RevDate.Value and OSDate.Value are both text strings and not date values as both controls are textboxes. If you compare the two values directly, you are comparing text and not serial date values.

    Try something like this to convert the text-dates to serial-dates and then compare...
    If CDate(RevDate.Value) <= CDate(OSDate.Value) Then

    After you show the invalid date message, clear the textbox e.g; OSDate.Value = ""

    It would be better\easier in my mind to test the dates from a calendar control event procedure.

    If you need specific help, it would be best to post an example workbook though I have an older version of Excel. I cannot load some of the newer versions of the ActiveX controls.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    03-09-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Data validation of dates entered using Calendar Control not working

    Thanks for the quick response AlphaFrog. I tried your suggestion but I kept getting a runtime error.

    I have attached for you a copy of my form:

    Price New Line Advice FormV1 - New CalCopy.xlsm

    I had previously tried formatting the text boxes in "dd/mm/yyyy" format but for some reason, any date selected preceding the current date formats as "mm/dd/yyyy".

    I'm not sure what else I need to be doing.

  4. #4
    Registered User
    Join Date
    03-09-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Data validation of dates entered using Calendar Control not working

    Further to the above, It appears that anytime I select a date in which the day is a single digit ie. 2nd March 2014, the format changes to mm/dd/yyyy so instead of being displayed as 02/03/2014 it is displayed as 03/02/2014 - using my current code

  5. #5
    Registered User
    Join Date
    03-09-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Data validation of dates entered using Calendar Control not working

    Figured it out after concentrating a little more.
    Your assistance was much appreciated though!

    My new code (It does what I need it to, I just need to accept that the dates will be in US format rather than Aus):

    Please Login or Register  to view this content.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Data validation of dates entered using Calendar Control not working

    I had been working on it. This is how I would do it for what it's worth. It uses only one Calendar control called DateCal and it positions it next to each textbox as needed. Ausi date format (dd/mm/yyyy) is OK.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    03-09-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Data validation of dates entered using Calendar Control not working

    Well your code works much better! and aesthetically the form looks better.
    I've modified so that it uses command buttons to call the calendar rather than double clicking on the textbox.

    Thank you so much for taking the time to look at this for me. I really do appreciate your help!

+ 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] Use Calendar Control to update different fields with unique dates
    By onmyway in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-11-2013, 01:15 PM
  2. Disable Selected Dates in Calendar Control
    By puuts in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-25-2013, 02:08 AM
  3. Should I use calendar control to accept dates from user?
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-13-2012, 03:20 AM
  4. validation rules not working when someone copy paste data on validation cell
    By jthakrar in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-17-2010, 03:36 AM
  5. Limit data entered to list (drop-down control)
    By Ronnie in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-12-2006, 07:06 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