Date construct with DatePart is kicking my.... well, you know.

J

J. Keggerlord

Private Sub cmdCreatePath_Click()


Dim strDateCons As String

<bunch of my code that is (surprisingly) working like it should>

strDateCons = DatePart(yyyy, Me.txtDateEntered) & "-" & DatePart(mm,
Me.txtDateEntered) & _
"-" & DatePart(dd, Me.txtDateEntered) Debug.Print strDateCons

End Sub

When I click the magic button... nuffin'! I get an error message that
states "Compile error: Variable not defined" and it highlights the 'yyyy'
part of my first DatePart statement.

What I actually wanted it to do was to take the date from txtDateEntered
(which is a populated field when I try this) and return a date construct of
yyyy-mm-dd which then will become part of another string. Any ideas what I
might be doing wrong? Major thanks in advance to anyone who can help with
this!
 
F

fredg

Private Sub cmdCreatePath_Click()

Dim strDateCons As String

<bunch of my code that is (surprisingly) working like it should>

strDateCons = DatePart(yyyy, Me.txtDateEntered) & "-" & DatePart(mm,
Me.txtDateEntered) & _
"-" & DatePart(dd, Me.txtDateEntered) Debug.Print strDateCons

End Sub

When I click the magic button... nuffin'! I get an error message that
states "Compile error: Variable not defined" and it highlights the 'yyyy'
part of my first DatePart statement.

What I actually wanted it to do was to take the date from txtDateEntered
(which is a populated field when I try this) and return a date construct of
yyyy-mm-dd which then will become part of another string. Any ideas what I
might be doing wrong? Major thanks in advance to anyone who can help with
this!

What datatype is txtDateEntered?
If it is Date datatype, then all you need is:

strDateCons = Format(txtDateEntered],"yyyy-mm-dd")

Using your example construct, it would be:

strDateCons = DatePart("yyyy", Me.txtDateEntered) & "-" &
DatePart("mm", Me.txtDateEntered) & _
"-" & DatePart("dd", Me.txtDateEntered)

which is a lot of work to arrive at the same place.

If txtDateEntered is a text datatype you'll need to give an example of
exactly what all of the entered text data looks like.
Could be Jan 15, 2008 or January 15, 2008 or 15 Jan 2008 or 1/15/2008
or 2008-1-15, etc...
 
M

Marshall Barton

J. Keggerlord said:
strDateCons = DatePart(yyyy, Me.txtDateEntered) & "-" & DatePart(mm,
Me.txtDateEntered) & _
"-" & DatePart(dd, Me.txtDateEntered) Debug.Print strDateCons

When I click the magic button... nuffin'! I get an error message that
states "Compile error: Variable not defined" and it highlights the 'yyyy'
part of my first DatePart statement.


The part of the date needs to be a string (i.e. in quotes)

DatePart("yyyy", Me.txtDateEntered)

OTOH, that is a complicated way to format a date when you
can just use the Format function to do it all:

strDateCons = Format(Me.txtDateEntered, "yyyy-mm-dd")
 
F

fredg

Private Sub cmdCreatePath_Click()

Dim strDateCons As String

<bunch of my code that is (surprisingly) working like it should>

strDateCons = DatePart(yyyy, Me.txtDateEntered) & "-" & DatePart(mm,
Me.txtDateEntered) & _
"-" & DatePart(dd, Me.txtDateEntered) Debug.Print strDateCons

End Sub

When I click the magic button... nuffin'! I get an error message that
states "Compile error: Variable not defined" and it highlights the 'yyyy'
part of my first DatePart statement.

What I actually wanted it to do was to take the date from txtDateEntered
(which is a populated field when I try this) and return a date construct of
yyyy-mm-dd which then will become part of another string. Any ideas what I
might be doing wrong? Major thanks in advance to anyone who can help with
this!

What datatype is txtDateEntered?
If it is Date datatype, then all you need is:

strDateCons = Format(txtDateEntered],"yyyy-mm-dd")

Using your example construct, it would be:

strDateCons = DatePart("yyyy", Me.txtDateEntered) & "-" &
DatePart("mm", Me.txtDateEntered) & _
"-" & DatePart("dd", Me.txtDateEntered)

which is a lot of work to arrive at the same place.

If txtDateEntered is a text datatype you'll need to give an example of
exactly what all of the entered text data looks like.
Could be Jan 15, 2008 or January 15, 2008 or 15 Jan 2008 or 1/15/2008
or 2008-1-15, etc...

I meant to include in the previous post....
There is no need to set up strDateCons at all.
If you wish to use the formatted date within a string all you need is
something like this:
="You last payment in the amount of " & [AmtPaid] & " was received on
" & Format([txtDateEntered],"yyyy-mm-dd") & "."
 
J

J. Keggerlord

D'oh!!! I learn new Access functions every day this way.... Wish I had time
to get formal training in VBA.

Thank you both for helping the "pwned N00b" out!

fredg said:
Private Sub cmdCreatePath_Click()

Dim strDateCons As String

<bunch of my code that is (surprisingly) working like it should>

strDateCons = DatePart(yyyy, Me.txtDateEntered) & "-" & DatePart(mm,
Me.txtDateEntered) & _
"-" & DatePart(dd, Me.txtDateEntered) Debug.Print strDateCons

End Sub

When I click the magic button... nuffin'! I get an error message that
states "Compile error: Variable not defined" and it highlights the 'yyyy'
part of my first DatePart statement.

What I actually wanted it to do was to take the date from txtDateEntered
(which is a populated field when I try this) and return a date construct of
yyyy-mm-dd which then will become part of another string. Any ideas what I
might be doing wrong? Major thanks in advance to anyone who can help with
this!

What datatype is txtDateEntered?
If it is Date datatype, then all you need is:

strDateCons = Format(txtDateEntered],"yyyy-mm-dd")

Using your example construct, it would be:

strDateCons = DatePart("yyyy", Me.txtDateEntered) & "-" &
DatePart("mm", Me.txtDateEntered) & _
"-" & DatePart("dd", Me.txtDateEntered)

which is a lot of work to arrive at the same place.

If txtDateEntered is a text datatype you'll need to give an example of
exactly what all of the entered text data looks like.
Could be Jan 15, 2008 or January 15, 2008 or 15 Jan 2008 or 1/15/2008
or 2008-1-15, etc...
 

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

Top