473,473 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Converting Text to Date

I have tried every function that seemed applicable and can't seem to convert
a simple text field containing numbers, for example "022807", into a date.
Nor can I do the reverse and convert a date field into text, like 02/28/07
into text "022807". Can anyone help?
Mar 1 '07 #1
8 32420
Try:

CDate(Format([MyField], "00\/00\/00"))

The Format() converts the string into a string with embedded slashes.
CDate() then converts it into a date.
Use criteria to filter out nulls.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Steve Cartnal" <sc******@alltel.netwrote in message
news:94***************************@ALLTEL.NET...
>I have tried every function that seemed applicable and can't seem to
convert a simple text field containing numbers, for example "022807", into
a date. Nor can I do the reverse and convert a date field into text, like
02/28/07 into text "022807". Can anyone help?
Mar 1 '07 #2
x = "022807"
y = dateserial(mid(x,5,2), left(x,2), mid(x,3,2))
? y
2/28/07

z = format(y, "mmddyy")
? z
022807

This MSKB article provides information on how dates are stored in Access.
http://support.microsoft.com/kb/q130514/

HTH - Bob

Steve Cartnal wrote:
>I have tried every function that seemed applicable and can't seem to convert
a simple text field containing numbers, for example "022807", into a date.
Nor can I do the reverse and convert a date field into text, like 02/28/07
into text "022807". Can anyone help?
--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200703/1

Mar 1 '07 #3
Thanks Allen. That works. I had tried the CDate function, and I did get a
date. It just wasn't the date I wanted. Adding the slashes first makes much
sense, but I don't believe that I would have ever thought of trying that on
my own. Thanks much.

"Allen Browne" <Al*********@SeeSig.Invalidwrote in message
news:45***********************@per-qv1-newsreader-01.iinet.net.au...
Try:

CDate(Format([MyField], "00\/00\/00"))

The Format() converts the string into a string with embedded slashes.
CDate() then converts it into a date.
Use criteria to filter out nulls.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Steve Cartnal" <sc******@alltel.netwrote in message
news:94***************************@ALLTEL.NET...
>>I have tried every function that seemed applicable and can't seem to
convert a simple text field containing numbers, for example "022807", into
a date. Nor can I do the reverse and convert a date field into text, like
02/28/07 into text "022807". Can anyone help?

Mar 1 '07 #4
Thanks much Bob. I'll try those when I get a chance.

I could be wrong, but in the second example, wouldn't that still be a date
and not text? (My problem centered on exporting records containing several
date fields. All but one of the dates is to be formatted as "YYYYMMDD". The
odd ball is suppose to be "MMDDYY". So when I set up my export specification
and specified date fields to be YMD and checked the box to use four-digit
years, as they all were suppose to be except that one, the oddball followed
all the others. The field size in the export spec. for that field is 6, so I
would get "200702", instead of "022807". So I figured I could just convert
the date to text somewhere in the process, and can't get that to work. I
believe Access would treat your second example as a date still and when it
was exported to the text file it would be the same as I'm getting now.
"raskew via AccessMonster.com" <u28575@uwewrote in message
news:6e8679e8c8906@uwe...
>x = "022807"
y = dateserial(mid(x,5,2), left(x,2), mid(x,3,2))
? y
2/28/07

z = format(y, "mmddyy")
? z
022807

This MSKB article provides information on how dates are stored in Access.
http://support.microsoft.com/kb/q130514/

HTH - Bob

Steve Cartnal wrote:
>>I have tried every function that seemed applicable and can't seem to
convert
a simple text field containing numbers, for example "022807", into a date.
Nor can I do the reverse and convert a date field into text, like 02/28/07
into text "022807". Can anyone help?

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200703/1

Mar 1 '07 #5
On Mar 1, 12:34 am, "Steve Cartnal" <scart...@alltel.netwrote:
I have tried every function that seemed applicable and can't seem to convert
a simple text field containing numbers, for example "022807", into a date.
Nor can I do the reverse and convert a date field into text, like 02/28/07
into text "022807". Can anyone help?
This may not be the best way, but it works. Try it and see if you
like it

Dim Date1 As String, Date2 As String, Date3 As String
Date1 = "022807"
Date2 = Left(Date1, 2) + "/" + Mid(Date1, 3, 2) + "/" + Right(Date1,
2)
Date3 = Replace(Date2, "/", "")
msgbox "Date1 = " & Date1 & vbCrLf & "Date2 = " & Date2 & vbCrLf &
"Date3 = " & Date3

Mar 1 '07 #6
Steve -

The second example if fact returns a number formated with a leading zero. To
return it as text, use the Cstr() function, e.g.
z = cstr(format(y, "mmddyy"))
To test: try ? cdbl(z). It will return a data type mismatch because z is now
in text.

Bob
Steve Cartnal wrote:
>Thanks much Bob. I'll try those when I get a chance.

I could be wrong, but in the second example, wouldn't that still be a date
and not text? (My problem centered on exporting records containing several
date fields. All but one of the dates is to be formatted as "YYYYMMDD". The
odd ball is suppose to be "MMDDYY". So when I set up my export specification
and specified date fields to be YMD and checked the box to use four-digit
years, as they all were suppose to be except that one, the oddball followed
all the others. The field size in the export spec. for that field is 6, so I
would get "200702", instead of "022807". So I figured I could just convert
the date to text somewhere in the process, and can't get that to work. I
believe Access would treat your second example as a date still and when it
was exported to the text file it would be the same as I'm getting now.
>>x = "022807"
y = dateserial(mid(x,5,2), left(x,2), mid(x,3,2))
[quoted text clipped - 15 lines]
>>>Nor can I do the reverse and convert a date field into text, like 02/28/07
into text "022807". Can anyone help?
--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200703/1

Mar 1 '07 #7
I stand corrected. I went back to the underlying table and switched the
oddball date field from a Date/Time to a Text data type, and then used
=Format(Date(), "mmddyy") as the default value and it does behave as text
then and exports just like I need it to. However, I discovered tonight that
the export specifications for those twelve tables still think that field is
a Date field and won't export it. So now I get to go redo all twelve of the
export specs.

"raskew via AccessMonster.com" <u28575@uwewrote in message
news:6e8c8d9f3dd8c@uwe...
Steve -

The second example if fact returns a number formated with a leading zero.
To
return it as text, use the Cstr() function, e.g.
z = cstr(format(y, "mmddyy"))
To test: try ? cdbl(z). It will return a data type mismatch because z is
now
in text.

Bob
Steve Cartnal wrote:
>>Thanks much Bob. I'll try those when I get a chance.

I could be wrong, but in the second example, wouldn't that still be a date
and not text? (My problem centered on exporting records containing several
date fields. All but one of the dates is to be formatted as "YYYYMMDD".
The
odd ball is suppose to be "MMDDYY". So when I set up my export
specification
and specified date fields to be YMD and checked the box to use four-digit
years, as they all were suppose to be except that one, the oddball
followed
all the others. The field size in the export spec. for that field is 6, so
I
would get "200702", instead of "022807". So I figured I could just convert
the date to text somewhere in the process, and can't get that to work. I
believe Access would treat your second example as a date still and when it
was exported to the text file it would be the same as I'm getting now.
>>>x = "022807"
y = dateserial(mid(x,5,2), left(x,2), mid(x,3,2))
[quoted text clipped - 15 lines]
>>>>Nor can I do the reverse and convert a date field into text, like
02/28/07
into text "022807". Can anyone help?

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200703/1

Mar 2 '07 #8

"chris" <ch****@motors-acceptance.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
On Mar 1, 12:34 am, "Steve Cartnal" <scart...@alltel.netwrote:
>I have tried every function that seemed applicable and can't seem to
convert
a simple text field containing numbers, for example "022807", into a
date.
Nor can I do the reverse and convert a date field into text, like
02/28/07
into text "022807". Can anyone help?

This may not be the best way, but it works. Try it and see if you
like it

Dim Date1 As String, Date2 As String, Date3 As String
Date1 = "022807"
Date2 = Left(Date1, 2) + "/" + Mid(Date1, 3, 2) + "/" + Right(Date1,
2)
Date3 = Replace(Date2, "/", "")
msgbox "Date1 = " & Date1 & vbCrLf & "Date2 = " & Date2 & vbCrLf &
"Date3 = " & Date3
Thanks Chris. I will try that.
Mar 6 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dana Shields | last post by:
I am attempting to upsize from access to SQL Server. I'm trying to convert my queries to SQL Server views; however, I'm having a lot of difficulty with the syntax differences. For instance, a...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
32
by: robert d via AccessMonster.com | last post by:
I'm looking at converting DAO to ADO in my app. All of my DAO connections are of the following structure: Dim wsName As DAO.Workspace Dim dbName As DAO.Database Dim rsName As DAO.Recordset ...
2
by: XML newbie: Urgent pls help! | last post by:
Does anyone have a snippet of code that will convert a string to a long array? I've nearly smashed my head against the wall trying to figure this out. I'm Using vb.net 2005 Pls reply asap. I...
12
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for...
2
by: TofuTheGreat | last post by:
I'm using "Now.ToOADate" for a record timestamp in a small database app (it's what I want to do so don't try to disuade me ;-D). Anyway. I store the value of Now.ToOADate in a string field in...
3
by: Jef Driesen | last post by:
How can I convert a date string to a number (e.g. a time_t value or a tm struct)? I know about the strptime function, but then I have to know the format string. And that is a problem. I'm trying...
1
by: PW | last post by:
Hi, When I run the following command, some fields are ending up blank when the clearly have values them in Excel. I have tried converting the columns to general and text. The ones that are...
6
by: sweatha | last post by:
Hi I have to select count(Date) from Appointment table having count<5. If count(Date)<5 then I should insert the data from my page. For that I have given the coding as protected void...
0
marktang
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,...
0
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...
0
jinu1996
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
agi2029
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.