How to display only year as date in txtbox

  • Thread starter Thread starter sebastico
  • Start date Start date
S

sebastico

Hello
Access 2003
txtAddYear
Default Value: Date()

In a form I have this code which works well. However, I need to display only
year in txtbox. Could you help me how to do it?

Private Sub bAddYear_Click()
Select Case Me.fraOptions.Value
Case Is = 1
Me.txtStart.Value = DateAdd("yyyy", 1, [txtStart])
Case Is = 2
Me.txtStart.Value = DateAdd("yyyy", 5, [txtStart])
Case Is = 3
Me.txtStart.Value = DateAdd("yyyy", 10, [txtStart])
End Select
End Sub
 
Hello
Access 2003
txtAddYear
Default Value: Date()

In a form I have this code which works well. However, I need to display only
year in txtbox. Could you help me how to do it?

Simply use the field as the Control Source for the textbox and set its Format
property to "yyyy".

The field will still contain the complete date, but all you will see will be
the year portion. If you select into the field to edit it you'll see the
entire date.
 
John
If I understood, I wrote "yyyy" in the format's field. However, the txtStar
still displays the entire date dd/m/yyyy.
On the other hand, could you tell me how to display year 1960 in txtStart
any time the forms is open?

Many thanks for your kindness
 
If you want only the year in the text box why not use the Year function to
place that in the text box.

Default Value: Year(Date())

Adding to the year would be simple
Private Sub bAddYear_Click()
Select Case Me.fraOptions.Value
Case Is = 1
Me.txtStart.Value = me.TxtStart + 1
Case Is = 2
Me.txtStart.Value = me.TxtStart + 5
Case Is = 3
Me.txtStart.Value = me.TxtStart + 10
End Select
End Sub

If you always want 1960 as the start point then enter that value as the
default value.

The best solution depends on what you want to do with the value in the text
box. If you need to use the year value to generate a date (or date range)
then you may be able to use the DateSerial function to generate a date based
on the value of txtStart.

For instance
DateSerial(txtStart,1,1)
would give you January 1 of the relevant year.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
Well
The txtStart has by Default Value Date(). If I write "yyyy" when I open the
form txtStar displays same dd/m/yyyy.

don't know what you mean by "the format's field", but the suggestion was to
set the format property of the *text box* to "yyyy".

I did it, but txtStart shows the date as dd/m/yyyy.

BruceM via AccessMonster.com said:
I don't know what you mean by "the format's field", but the suggestion was to
set the format property of the *text box* to "yyyy".

It is very unclear what you are trying to do. Is txtStart bound to a field?
It seems you want to change the value in txtStart based on fraOptions, but
only if the user clicks a command button (or whatever bAddYear is). If
txtStart is unbound, the value will remain for every record until you click
the command button again. Why not store the original date, and calculate the
DateAdd date?

John
If I understood, I wrote "yyyy" in the format's field. However, the txtStar
still displays the entire date dd/m/yyyy.
On the other hand, could you tell me how to display year 1960 in txtStart
any time the forms is open?

Many thanks for your kindness
Hello
Access 2003
[quoted text clipped - 10 lines]
the year portion. If you select into the field to edit it you'll see the
entire date.

--
Message posted via AccessMonster.com


.
 
John
txtStart now shows only the year as I need. However, txt shows 2010 and I
need 1960 as start year. T
I'm sorry that I don't understand your last lines
 
IF you want the value to default to 1960. THEN enter 1960 as the default value
for the control.

I don't know what else or how else to tell you that.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
Back
Top