I need to extract the year part of a date and I have the following code to test - Private Sub Form_Open(Cancel As Integer)
-
Dim mydate, myyear
-
-
mydate = Date
-
myyear = Year(mydate)
-
-
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
9 8324
I suggest using this to get the year:
Dim strMonth As String
strMonth = Format(Date), "mmm-yyyy")
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
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 - Private Sub Form_Open(Cancel As Integer)
-
Dim mydate As Date
-
Dim myyear As Date
-
-
mydate = Date
-
myyear = Year(mydate)
-
-
End Sub
but I now get 'can't find the field Year referred to in your expression' as an error message?
I just ran this without problems in my test database: -
Dim MyDate As Date
-
Dim MyYear As String
-
-
MyDate = Date
-
MyYear = Year(MyDate)
-
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
Thanks - it seems to be working now!
Glad it's working for you!
Thanks for posting back to let us know.
Regards,
Scott
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
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).
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: Fendi Baba |
last post by:
The function is called from opencalendar(targetfield). Thanks for any
hints on what could be the problem.
..............................................................
var...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |