?
Solved

why the year function dosn't work in access 2013

Posted on 2013-11-23
8
Medium Priority
?
883 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

801 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