473,560 Members | 3,069 Online
Bytes | Software Development & Data Engineering Community
+ 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 32430
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******@allte l.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.c om
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*********@Se eSig.Invalidwro te 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******@allte l.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.c om" <u28575@uwewrot e in message
news:6e8679e8c8 906@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.c om
http://www.accessmonster.com/Uwe/For...ccess/200703/1

Mar 1 '07 #5
On Mar 1, 12:34 am, "Steve Cartnal" <scart...@allte l.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.c om
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.c om" <u28575@uwewrot e in message
news:6e8c8d9f3d d8c@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
specificati on
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.c om
http://www.accessmonster.com/Uwe/For...ccess/200703/1

Mar 2 '07 #8

"chris" <ch****@motor s-acceptance.comw rote in message
news:11******** **************@ p10g2000cwp.goo glegroups.com.. .
On Mar 1, 12:34 am, "Steve Cartnal" <scart...@allte l.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
17383
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 query with select mid(tblTable.FieldName,1,10)should look something like select substring(tblTable.FieldName,1,10). This is really the simplest of...
5
2505
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 applications. I am testing an upgrade of all of the sites and have converted the main root site...although not necessarily fixed any issues. I move on...
32
12487
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 Set wsName = DBEngine.Workspaces(0) Set dbName = wsName.OpenDatabase(CurrentProject.FullName) Set rsName = dbName.OpenRecordset("SQL Statement")
2
2327
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 thaanku all in advance. God bless u.
12
2579
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 example: 21/05/2006
2
7035
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 the database (16 character field on a SQL server to be exact). Is there a way of converting the text value from that field back into a readable...
3
7134
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 to autoformat the contents of text entries in a GUI. For numbers, I'm converting the text representation to the appropriate type (using atoi, atof,...
1
1703
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 ending up blank (Null, actually) contain: lu24769884l lu24769884xxl
6
12868
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 Button3_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("user...
0
7636
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7557
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7603
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7922
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6194
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5176
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1171
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.