Solved

why the year function dosn't work in access 2013

Posted on 2013-11-23
8
843 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

825 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