+ Reply to Thread
Results 1 to 6 of 6

Type mismatch on a date textbox when leaving it blank

  1. #1
    Registered User
    Join Date
    10-24-2012
    Location
    Europe
    MS-Off Ver
    Excel 2010
    Posts
    25

    Type mismatch on a date textbox when leaving it blank

    Hi guys, I wonder if you can help me.

    I have a form with two TextBox corresponding to dates. The first one is mandatory, and a valid date has to be entered. The second is optional: it could be blank or if not, a valid date must be entered.

    I tried to do it with DTpickers, but this solution doesn't work well on all Excel versions. So I decided to do it with simple textbox. But if the second date textbox is left empty, I got the "type mismatch" error message. If I defined it as variant instead of date it works well although I can't avoid the user enter a wrong date. Besides, I have another problem, as the date format must be dd-mm-aa, and this only works if the user enter a valid date on mm-dd-aa format (the default format for VB).

    This is the code:
    Please Login or Register  to view this content.
    Thanks in advance for your suggestions.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Type mismatch on a date textbox when leaving it blank

    Hi, shardin,

    you could check the length of the string of the second textbox and use CDate to convert the strings:

    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Type mismatch on a date textbox when leaving it blank

    Hi shardin,

    Here's a potential solution that doesn't require any variables (though you could still use them if need be):

    Please Login or Register  to view this content.
    Note also my comment next to your 'this instruction is ignored' comment as I'm confused by it.

    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Type mismatch on a date textbox when leaving it blank

    Hi, Robert,

    why check TextBox2 for length and put in Textbox1 in B1?

    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    10-24-2012
    Location
    Europe
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Type mismatch on a date textbox when leaving it blank

    Thanks a lot Holger & Robert, I didn't know about the Cdate tool.

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Type mismatch on a date textbox when leaving it blank

    Hi Holger,

    That was simply a typo - it should be TextBox2. Well spotted!!

    Cheers,

    Robert

+ 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