Results 1 to 10 of 10
  1. #1
    reema is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2012
    Posts
    15

    Year(date) returns type mismatch

    What's wrong with this?? :



    Private Sub Form_Load()
    Dim mydate As String


    mydate = YEAR(Date)


    End Sub

    It is giving me type-mismatch at mydate = Year(date) line.

  2. #2
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Year(Date) returns a numeric value, not a string.

    Either declare Mydate as integer, or use str(year(date)) to force it to string type.

    I should point out that that sub is essentially useless anyway. Because you declare mydate inside the sub, it is known only to that sub, and not outside it.

    What is it you want to do in the Load event?

    John
    Last edited by John_G; 03-27-2012 at 10:11 PM. Reason: Additional comment

  3. #3
    reema is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2012
    Posts
    15
    Hi John,

    The problem is that year(date) expression returns type mismatch - I see that when I add this expression to the watch window..

    Regarding this sub, it isnt really the full code... i have a long code in this form load event sub. But I need to compare year of a date_field to today's year. I couldn't get that to work and in process of debugging that I found that just the expression of year(date) isnt working.

    I also tried doing Cstr(year(date)) and also Cstr(year(datevalue(date))) and neither of them works....

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Shot in the dark warning. You don't have field named date do you?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    reema is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2012
    Posts
    15
    nope, i dont have field called date

  6. #6
    reema is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2012
    Posts
    15
    see the attached screenshot. it shows all the expressions i have added to the watch window and how year(date) returns type mismatch.

  7. #7
    reema is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2012
    Posts
    15
    seems like the insert image isnt working... so ignore the previous post...

  8. #8
    reema is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2012
    Posts
    15
    btw, this is exactly same problem as mine... http://www.tek-tips.com/viewthread.cfm?qid=411627

  9. #9
    reema is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2012
    Posts
    15
    anyway, I found it... I somehow had a hidden textbox whose name was year.... thanks Paul for putting me in the right direction.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Glad you found the problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Date - Textbox type mismatch 13
    By mdex in forum Programming
    Replies: 7
    Last Post: 01-20-2012, 08:44 AM
  2. Replies: 4
    Last Post: 01-10-2012, 06:26 PM
  3. Type Mismatch....WHY?!!?
    By jgelpi16 in forum Queries
    Replies: 9
    Last Post: 03-07-2011, 09:18 AM
  4. Type mismatch
    By jgelpi16 in forum Programming
    Replies: 1
    Last Post: 08-07-2010, 06:54 AM
  5. Type Mismatch - HELP!
    By jgelpi in forum Programming
    Replies: 3
    Last Post: 07-17-2009, 03:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums