?
Solved

why the year function dosn't work in access 2013

Posted on 2013-11-23
8
Medium Priority
?
895 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1000 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 1000 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

Technology Partners: 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!

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

850 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