Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting a Year from a Date Field??? 2

Status
Not open for further replies.

SilFaz

Programmer
Oct 3, 2003
37
0
0
AU
Hi everyone,

I am trying to populate a field (Year) in a table based on the contents of another field (ChangeDate).

When the ChangeDate is entered on a form, I want the year to appear in the Year field on my table without the user having to manually enter it again into the form. It may seem redundant to have the year in the table again (since it is already in the ChangeDate) but I need it for searching purposes on another form.

How can I extract just the year in the table (Note... my VB coding skills are poor at best... please bear with me).

Thank you,

Silvia
 
Use the Year Function
Code:
   myYear = Year([DateField])
 
Hello,

I'm sorry, but where would I put that? I'm trying to have it populate in the table...

Thanks,

Silvia
 
Your "need it for searching purposes" probably doesn't need a separate field. You could use the after update event of the text box bound to your date field to update the year field:
Me.txtYearField = Year(Me.txtDateField)

Duane
MS Access MVP
Find out how to get great answers faq219-2884.
 
Option Compare Database
Dim NYear As String
Dim INYear As Integer
Dim XNyear As Variant


Private Sub Date_of_Expense_AfterUpdate()

XNyear = Year(Me.[Date_of_Expense])

INYear = NYear
Me.Year = Year(Me.[Date_of_Expense])

End Sub


I cannot get this to work.
It stops at the first statement XNYear = etc.
Me.[Date_of_Expense]) conatins 12/12/2003. I want to extract the 2003. What is wrong ?
 
Year is the name of a function. I would rename the controls on the form to txtDOExp and txtYear. Then add code in the after update event of the txtDOExp:
If IsDate(Me.txtDOExp) Then
Me.txtYear = Year(Me.txtDOExp)
End If

Duane
MS Access MVP
Find out how to get great answers faq219-2884.
 


Microsoft Access can't find the field Year referred to in your expression. (at Nyear = )


Private Sub txtDate_of_Expense_AfterUpdate()
Dim NYear As String
Dim INYear As Integer
Dim XNyear As Variant
NYear = Year(Me.txtDate_of_Expense)

'INYear = NYear
'Me.Year = Year(Me.[Date_of_Expense])


If IsDate(Me.[Date_of_Expense]) Then

Me.txtYear = Year(Me.[txtDate_of_Expense])

End If

End Sub
 
Check your references by selecting Tools|References and look for a missing reference.

Duane
MS Access MVP
Find out how to get great answers faq219-2884.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top