DatePart VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all

Bit of help would be great and very much appreciated - how can I get only
year ("06" not "2006") in a concencated value ???
(Note the yyyy and the yy difference)

This works
Private Sub Combo123_AfterUpdate()
Me.Code = Me.Combo123.Column(0) & DatePart("yyyy", [SomeField])
End Sub

This doesn't
Private Sub Combo123_AfterUpdate()
Me.Code = Me.Combo123.Column(0) & DatePart("yy", [SomeField])
End Sub
 
As you've found, DatePart doesn't recognize yy as a valid parameter.
However, the Format function does. Since you're trying to treat it as text
anyhow, try:

Private Sub Combo123_AfterUpdate()
Me.Code = Me.Combo123.Column(0) & Format([SomeField], "yy")
End Sub
 
Many thanks Douglas

Tried the Format route before but got it wrong so thank you for your
expertise again.

--
Wayne
Manchester, England.
Enjoy whatever it is you do


Douglas J. Steele said:
As you've found, DatePart doesn't recognize yy as a valid parameter.
However, the Format function does. Since you're trying to treat it as text
anyhow, try:

Private Sub Combo123_AfterUpdate()
Me.Code = Me.Combo123.Column(0) & Format([SomeField], "yy")
End Sub




--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Wayne-I-M said:
Hi all

Bit of help would be great and very much appreciated - how can I get only
year ("06" not "2006") in a concencated value ???
(Note the yyyy and the yy difference)

This works
Private Sub Combo123_AfterUpdate()
Me.Code = Me.Combo123.Column(0) & DatePart("yyyy", [SomeField])
End Sub

This doesn't
Private Sub Combo123_AfterUpdate()
Me.Code = Me.Combo123.Column(0) & DatePart("yy", [SomeField])
End Sub
 
This should work:
Me.Code = Me.Combo123.Column(0) & Right(Str(Year([SomeField])),2)

Regards/JK
 
Ooops, just in case [someField] is not a Date Field

Me.Code = Me.Combo123.Column(0) & Right(Str(Year(CDate([SomeField]))),2)

Reagrds/JK


Me.Code = Me.Combo123.Column(0) &
JK said:
This should work:
Me.Code = Me.Combo123.Column(0) & Right(Str(Year([SomeField])),2)

Regards/JK


Wayne-I-M said:
Hi all

Bit of help would be great and very much appreciated - how can I get
only
year ("06" not "2006") in a concencated value ???
(Note the yyyy and the yy difference)

This works
Private Sub Combo123_AfterUpdate()
Me.Code = Me.Combo123.Column(0) & DatePart("yyyy", [SomeField])
End Sub

This doesn't
Private Sub Combo123_AfterUpdate()
Me.Code = Me.Combo123.Column(0) & DatePart("yy", [SomeField])
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top