Results 1 to 9 of 9

Thread: VBA IsDate function not appearing to work

  1. #1

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    VBA IsDate function not appearing to work

    Hello.

    A weird one - wondered if anyone has come across this.

    Usually when using isdate function it tells me whether the string/number sent to it is a date.

    Appeared to work fine.

    However now it is not working fine and I cannot see why.

    I'm pulling data from Excel which is in a date format, but not a proper date (I think). I change the cell formatting to text (via number format and '@')
    and the contents change to a number. Ah ha. IsDate should work. It doesn't.

    Just so people can check to see if maybe something is corrupted/running wrong here...
    The date, number
    24/08/1971 , 26169

    If any one has come across this and knows how to fix Access VBA to work, please let me know. For now I'm going to use the format function which (for some reason) is changing the value to a date correctly.

    Thanks in advance

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: VBA IsDate function not appearing to work

    change the format to Date and select 03/14/01 or 3/14/2001 or something
    Excel likes to switch dates to julian format for some silly reason
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: VBA IsDate function not appearing to work

    wait.. I misread that..

    how are u pulling data? is the sheet linked? imported? etc...
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: VBA IsDate function not appearing to work

    instance of excel opened, the sheet opened in the instance. The cell accessed via the objects. I used to put it straight into the recordset field, but when data overflows the db field instead of the usual errors, the recordset seemed to corrupt and had to be cancelled/closed and reopened.

    So the value/text goes into a temporary string variable. Then into the db field.

    So I am changing those formatted fields from a date (with a * (star) in excel formatting) to text then using that text to check whether it is a date, and processing accordingly.

    Only as originally posted - it didn't like it.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: VBA IsDate function not appearing to work

    Ecniv
    From the VBA help file on Isdate
    Syntax

    IsDate(expression)

    The required expression argument is a Variant containing a date expression or string expression recognizable as a date or time.
    IsDate will always return an error if you pass it a LONG or INT value.
    Here's a proc showing this
    VB Code:
    1. Sub TestISDate()
    2. Dim dTestDate As Date
    3. Dim sTestString As Date
    4. Dim lTestLong As Long
    5.    
    6.     dTestDate = "24/08/1971"
    7.    
    8.     sTestString = CStr(dTestDate)
    9.     lTestLong = CLng(dTestDate)
    10.    
    11.     Debug.Print dTestDate, IsDate(dTestDate)
    12.     Debug.Print sTestString, IsDate(sTestString)
    13.     Debug.Print lTestLong, IsDate(lTestLong)
    14. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: VBA IsDate function not appearing to work

    Hi,

    Wow didn't know that. Did know about numbers, but not longs... tried your example and yeah it didn't like it. Possibly it is converting to a long then ....

    Hmmm I'll try some things

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA IsDate function not appearing to work

    I think you wanted to show this as a String

    Dim sTestString As Date

    You can use DateSerial if you need to pass a number.

    Function DateSerial(Year As Integer, Month As Integer, Day As Integer)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: VBA IsDate function not appearing to work

    No, in my naming convention a prefix of 's' means a date, honestly it does....

    You can also use a cdate to convert a LONG to a DATE
    VB Code:
    1. Sub TestISDate()
    2. Dim dTestDate As Date
    3. Dim sTestString As String
    4. Dim lTestLong As Long
    5.    
    6.     dTestDate = "24/08/1971"
    7.    
    8.     sTestString = CStr(dTestDate)
    9.     lTestLong = CLng(dTestDate)
    10.    
    11.     Debug.Print dTestDate, IsDate(dTestDate)
    12.     Debug.Print sTestString, IsDate(sTestString)
    13.     Debug.Print lTestLong, IsDate(lTestLong)
    14.     Debug.Print CDate(lTestLong), IsDate(CDate(lTestLong))
    15. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: VBA IsDate function not appearing to work

    Don't mind how it gets there as long as it works.

    At the moment, when I change the cells formatting to text it converts the date to a number.

    Now those at the top half of the sheet are fine as proper dates. Those on the bottom on some sheets (out of 1000+) are formatted to a different date (one with a * next to it).

    And ofcourse there are ones with crap data in

    Just I used to think isdate worked with numbers and dates and strings, but it is not liking it - even when I put a number into the string variable before comparing.

    Either that or this is a complete waste of your professional times (respectively) as I feel like I am getting thicker by the second in work heheh

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width