473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Year Function

I need to extract the year part of a date and I have the following code to test

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2. Dim mydate, myyear
  3.  
  4. mydate = Date
  5. myyear = Year(mydate)
  6.  
  7. End Sub
But it gives me a type mismatch when trying to find the Year of mydate. The same problem occurs with the Month function but the Day function works fine. I have used this code before without problem and am now very confused. Any suggestions gratefully received.

Thanks
Feb 19 '08 #1
9 8324
cori25
83
I suggest using this to get the year:

Dim strMonth As String

strMonth = Format(Date), "mmm-yyyy")
Feb 19 '08 #2
Scott Price
1,384 Expert 1GB
Explicitly declare your variables, this is a good general practice as well as a possible solution to your problem.

Dim myDate As Date

Be aware that VBA (unlike some other programming languages) defaults an implicit declaration to the Variant data type, which can have some unintended consequences:

Dim myDate, myMonth As Date results in one variable declared as a Date, the other as a variant. In VBA each variable must be dimensioned separately.

Regards,
Scott
Feb 19 '08 #3
Explicitly declare your variables, this is a good general practice as well as a possible solution to your problem.

Dim myDate As Date

Be aware that VBA (unlike some other programming languages) defaults an implicit declaration to the Variant data type, which can have some unintended consequences:

Dim myDate, myMonth As Date results in one variable declared as a Date, the other as a variant. In VBA each variable must be dimensioned separately.

Regards,
Scott
I have amended my code to

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2. Dim mydate As Date
  3. Dim myyear As Date
  4.  
  5. mydate = Date
  6. myyear = Year(mydate)
  7.  
  8. End Sub
but I now get 'can't find the field Year referred to in your expression' as an error message?
Feb 19 '08 #4
Scott Price
1,384 Expert 1GB
I just ran this without problems in my test database:


Expand|Select|Wrap|Line Numbers
  1. Dim MyDate As Date
  2. Dim MyYear As String
  3.  
  4. MyDate = Date
  5. MyYear = Year(MyDate)
  6. Debug.Print MyYear
The output is 2008 as expected. The MyYear needs to be a String data type, because a Date data type attempts to coerce the 2008 result into a Date with the output of 6/30/1905 for some strange reason :-)

If you continue having problems, I suggest you might have some corruption problems creeping in.

Regards,
Scott
Feb 19 '08 #5
Thanks - it seems to be working now!
Feb 19 '08 #6
Scott Price
1,384 Expert 1GB
Glad it's working for you!

Thanks for posting back to let us know.

Regards,
Scott
Feb 19 '08 #7
Stewart Ross
2,545 Expert Mod 2GB
Hi all. Year(), Month() and Day() all return integer components from a date - which can then be used within queries tocompare day, month and year ranges, or in generation of new dates (for example, using DateSerial(Year(Date), Month(Date), 1) to return the date corresponding to the first day of the current month).

There was a little confusion over types in parts of the responses, as MyYear was being given a Date type instead of an Integer value.

Cheers

Stewart
Feb 19 '08 #8
NeoPa
32,556 Expert Mod 16PB
...
The output is 2008 as expected. The MyYear needs to be a String data type, because a Date data type attempts to coerce the 2008 result into a Date with the output of 6/30/1905 for some strange reason :-)
...
If you put the value 2008 into a Date field it will try to interpret it as THAT number of DAYS since 31 December 1899 (the base date for VBA dates).
Feb 20 '08 #9
Scott Price
1,384 Expert 1GB
If you put the value 2008 into a Date field it will try to interpret it as THAT number of DAYS since 31 December 1899 (the base date for VBA dates).
Oh, THAT'S the strange reason :-) Somewhere I knew that, but forgot the exact date it starts at.

Thanks NeoPa!

Regards,
Scott
Feb 20 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ltamisin | last post by:
Hi Im working on a Year(Date) function right now, the output of that function is this "2005", my question is how can i change the format into this "05" Function: Year(Date) Output: 2005...
7
by: Fendi Baba | last post by:
The function is called from opencalendar(targetfield). Thanks for any hints on what could be the problem. .............................................................. var...
14
by: Protoman | last post by:
Hi!!! I need some help on a project I'm that calculates leap years; I'm getting errors that I have no idea what they mean; here's the code: #include <iostream> #include <cstdlib> using...
2
by: Douglas | last post by:
I have a Vehicle MOT field in my table which i have as a Date field I dont really want to hold the year, just 'dd mmm' as MOTs are the same date every year. I have the field on my form as a...
14
by: Alan | last post by:
Hi everyone! I'm trying to produce a periodic financial report on projects from various departments. My database is set up with the tables tblDepartment, tblProjects, tblPeriods, and tblBudgets...
3
by: ethoemmes | last post by:
Hi Does anyone know of a function I can use to return the current tax year. Our tax year runs from 6 April. I need a function which will return the tax year for a given date. TIA
18
by: Bruno Baguette | last post by:
Hello, I have to design a table wich will store some action reports. Each report have an ID like this 1/2004, 2/2004, ... and each years, they restart to 1 (1/2004, 1/2005, 1/2006,...). So, I...
3
by: jannoergaard | last post by:
Hi I want to write a function that can return a sum for a given date range. The same function should be able to return the sum for the same period year before. Let me give an example: The...
4
by: ssylee | last post by:
I have set my year value in the register on my RTC (DS1302) to be 2008 using this code: // This function sets the year on the RTC // Note: The year takes in only the last two digits of the year...
4
by: hallsers | last post by:
Here is the problem i am having: - When a user logs in, on page load it will take the dob of all users in a stored friends list and compare them against the following conditions - To display any...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.