Solved

why the year function dosn't work in access 2013

Posted on 2013-11-23
8
881 Views
Last Modified: 2013-11-24
i use a CDate  to get a date from a text value (i have a combo box that has two column one name ot the month and the other the number of the month) and i write a code like this
Dim a As Date
a = CDate(Me.MonthCombo & "/1/" & Me.Year)
 MsgBox Month(a)
MsgBox Year(a)

Open in new window



and i get a mesgbox with the month name, but when it's has to appear the message box with the year i get an error message : run time error 13 type mishmatch


why is that?

thanks a lot
0
Comment
Question by:bill201
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 38

Expert Comment

by:Jim P.
ID: 39672143
The word "Year" is a built in function in many databases and should not be be tied to a form.

Your code should be
Dim a As Date
a = CDate(Me.MonthCombo & "/1/" & Year(Now()))
 MsgBox Month(a)
MsgBox Year(a)

Open in new window

0
 

Author Comment

by:bill201
ID: 39672144
i want to be able to selet a year and not just the current year
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 39672172
Then rename your combo box to something like YearCombo.

Then it would be
 Dim a As Date
a = CDate(Me.MonthCombo & "/1/" & Me.YearCombo.Value)
 MsgBox Month(a)
MsgBox Year(a)

Open in new window


Year, Month, Day, Date, Now are all functions/variables within Access. Using them is always problematic. You can get around it by using square brackets ([Name]) but it is a stupid issue when you forget them.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bill201
ID: 39672308
Is this the only solution that I prefer that that the year will not be a combo box
0
 

Author Comment

by:bill201
ID: 39672324
where i can  use square brackets, i try like this:

Dim a As Date
a = CDate(Me.MonthCombo & "/1/" & [Year])
                        
MsgBox Month(a)
MsgBox Year([a])

Open in new window


But that did not solve the problem
0
 
LVL 38

Accepted Solution

by:
Jim P. earned 250 total points
ID: 39672586
Dim a As Date
a = CDate(Me.MonthCombo & "/1/" & Me.[Year].Value)
 MsgBox Month(a)
MsgBox Year(a)

Open in new window


But I still recommend that you change the name of the text box to anything else than just "Year" such as YearTxt.
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 39672651
As Jim is showing you the issue is with your naming conventions.  Access is getting confused because 'Year' means multiple things... it refers to both the Year() function and your Year textbox.

This will work too... specifying the VBA prefix for the Year function:

Dim a As Date
a = CDate(Me.MonthCombo & "/1/" & Me.Year)
 MsgBox Month(a)
MsgBox VBA.Year(a)

Open in new window


But the BEST solution is to adhere to standard naming conventions.  With standard naming conventions such as txtYear, txtName cboMonth (prefixing your control names with control types), code like you originally posted will work *consistently* because the names do not conflict with built in functions.

See the best practice/standard naming conventions in the following article.  The standards that most of the regulars here use for Access VBA are found midway down the page:
http://www.xoc.net/standards/rvbanc.asp
0
 

Author Closing Comment

by:bill201
ID: 39672814
thanks alot for your excellent solution and ambizup also for the link
0

Featured Post

Webinar: MongoDB® Index Types

Join Percona’s Senior Technical Services Engineer, Adamo Tonete as he presents “MongoDB Index Types, How, When and Where Should They be Used?” on Wednesday, July 12, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question