473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

VBA SQL query to convert data in a field.

I receive data from another program which I am unable to change. I am trying to create an VBA SQL query in Access '03 that will convert a field that has a date which is in an unrecognizable format. It consists of DayMonthYear and Time. I just need to get the date to a new field called RevisedDateTimeDate. Any suggestions would be greatly appreciated.


Expand|Select|Wrap|Line Numbers
  1. DateTime       RevisedDateTimeDate
  2. 15OCT08 0840       10/18/08
  3. 08JAN08 1635       1/8/08
  4. 03JUL08 0119       7/3/08
  5. 03JUL08 1000       7/3/08
  6. 17AUG08 1314       8/17/08
  7. 08JAN08 1635       1/8/08
  8.  
Oct 16 '08 #1
18 3877
NeoPa
32,556 Expert Mod 16PB
Try :
Expand|Select|Wrap|Line Numbers
  1. SELECT CDate(Left([DateTime],2) & ' ' &
  2.               Mid([DateTime],3,3) & ' 20' &
  3.               Mid([DateTime],6,3) &
  4.               Mid([DateTime],9,2) & ' ' &
  5.               Mid([DateTime],11,2) AS RevisedDateTimeDate
Oct 16 '08 #2
Thank you for the quick reply, I am getting a Run-time error 3075 for a Syntax error (missing operator). Any idea as to what I did wrong?
Oct 17 '08 #3
NeoPa
32,556 Expert Mod 16PB
Not unless you show me what you did :S
Oct 17 '08 #4
Good point. Still learning, but I thought I could put it in a Docmd.runsql statement as is. Not sure what else needed to be added.

Thanks
Oct 17 '08 #5
NeoPa
32,556 Expert Mod 16PB
I'll wait for you to post it then shall I?
Oct 17 '08 #6
Stewart Ross
2,545 Expert Mod 2GB
Hi Freeflyer. Just in case you didn't do so, could I just check that you added a FROM section with your table name to NeoPa's SQL (making it a fully valid SQL statement in the form SELECT ... FROM yourtable).

You did not tell us what the name of the table was, and accordingly I guess it was not part of NeoPa's skeleton code.

-Stewart
Oct 17 '08 #7
Here is what I put into the code:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "SELECT CDate(Left([DateTime],2) & ' ' & Mid([DateTime],3,3) & ' 20' & Mid([DateTime],6,3) & Mid([DateTime],9,2) & ' ' & Mid([DateTime],11,2) AS RevisedDateTimeDate FROM tblConvMTRL"
I did put the from statement in there and was getting the same error message.
Oct 17 '08 #8
NeoPa
32,556 Expert Mod 16PB
You cannot execute a SELECT query. DoCmd.RunSQL is exclusively for Action queries (Update, Append, Make Table etc).
Oct 17 '08 #9
NeoPa
32,556 Expert Mod 16PB
Expand|Select|Wrap|Line Numbers
  1. SELECT CDate(Left([DateTime],2) & ' ' & 
  2.               Mid([DateTime],3,3) & ' 20' & 
  3.               Mid([DateTime],6,3) & 
  4.               Mid([DateTime],9,2) & ' ' & 
  5.               Mid([DateTime],11,2) AS RevisedDateTimeDate 
  6. FROM tblConvMTRL
This should work in a QueryDef.

Again, as I have no idea of the context you need this in I can only show you relative to what you've already shared with us.

You will find generally that the less effort you put into explaining your problem, the less help you get. Not only will members immediately be irritated by that attitude, but also it is impossible to help when we don't have the information required.
Oct 17 '08 #10
I don't mean to be difficult, this is all new to me. I have bought books and tutorials to learn. I can do this all with queries in Access, but I want to do the entire program in Script.Having queries on top of queries has gotten quite confusing.

The only thing I was hoping to do is have a script that would convert that field to a recognizable date. I truly don't know what to do with the information you provided. What more information can I provide?
Oct 17 '08 #11
NeoPa
32,556 Expert Mod 16PB
Good answer :)

In answer to your question, and possibly some explanation as to why it is so difficult without the information, I need some context to know what you intend to do with it.

In Access there are so many different situations where SQL is used, many of which are very different in context. Working within a form can require SQL in various situations and a few guises. Working directly in a QueryDef (Stored Access Query) generally only deals with basic SQL, but is different from many requirements with forms.

Are you intending to display this data on a form?
Is this data coming from the Record Source of a Form or Report?
Where it is displayed (if on a form or report) is it in a bound control or an unbound one?
Oct 17 '08 #12
NeoPa
32,556 Expert Mod 16PB
As an afterthought, can you explain what you mean by "script"?

If you mean VBA that's fine. Otherwise I need some explanation of what type of scripting you are talking about.
Oct 17 '08 #13
As an afterthought, can you explain what you mean by "script"?

If you mean VBA that's fine. Otherwise I need some explanation of what type of scripting you are talking about.
Yes, VBA code to convert the DateTime field to the RevisedDateTimeDate field in the table only. This way I can pull information by Month and Date later. This unfortunately will be working on a very large table which is why I thought it would be better to just make the conversion to a new field in the table than to just show it in a form.

The data at this moment resides all in the same table. I will be generating reports off this data as well as using forms to display the information. It just seemed easier to me to have everything in the right format in the table.

Hopes this makes a little more sense.
Oct 17 '08 #14
NeoPa
32,556 Expert Mod 16PB
Ah, so you have a [DateTime] field in the table [tblConvMTRL] with the existing data in it, and you have a [RevisedDateTimeDate] field in the same table which currently has no data in it. Is that right?
Oct 19 '08 #15
Ah, so you have a [DateTime] field in the table [tblConvMTRL] with the existing data in it, and you have a [RevisedDateTimeDate] field in the same table which currently has no data in it. Is that right?
Yes, that is correct.
Oct 19 '08 #16
NeoPa
32,556 Expert Mod 16PB
Do you need to run this code again and again, or is it a one-off task?

If it's a one-off task then a simple UPDATE query will do for you.

Otherwise you need to decide how you will trigger the code.

The SQL for the UPDATE query will be the same in either case :
Expand|Select|Wrap|Line Numbers
  1. UPDATE [tblConvMTRL]
  2.  
  3. SET [RevisedDateTimeDate]=CDate(Left([DateTime],2) & ' ' &
  4.                                  Mid([DateTime],3,3) & ' 20' & 
  5.                                  Mid([DateTime],6,3) & 
  6.                                  Mid([DateTime],9,2) & ' ' & 
  7.                                  Mid([DateTime],11,2)
Oct 19 '08 #17
This will work. thank you for your time and assistance. Sorry it took me a while to get my point across.
Oct 20 '08 #18
NeoPa
32,556 Expert Mod 16PB
No worries. Pleased you're sorted now :)
Oct 20 '08 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: ccr | last post by:
Please view in a text editor so that the columnar text lines up. I used Terminal 9pt font to compose this post and copied/pasted to my newsreader program. I am writing in the hope that one of...
2
by: jaYPee | last post by:
Anyone know how can I convert this field to ms sql server? IIf(=0 Or IsNull(),"--",) AS FinalGrade, SchYrSemCourseJoin.Final, IIf(=0 Or IsNull(),"--",) AS UnitAlias this is the full sql...
2
by: Learner | last post by:
Hello, I am trying to store the data entered in a webform in the database. I have few Int and one SmallDateTime filed in my table in SQL Server 2005 database. I have made a storedproc to store the...
1
by: BigLebowski | last post by:
Hi all, I have to insert in a data field a value composed by a date and a time, that are stored in another table my problem is that the date is in a field (a data field) and the time in another...
1
by: neeraj | last post by:
Hi All Can any give me the code for convert "DataColumn" data type of "DataTable". Even if data table already populated (have data) Actually I am creating one search module which searches the...
7
by: Techhead | last post by:
I have a date/time field with a sql format of "datetime" The actual date/time data format is MM/DD/YYYY^hh:mm:ss:pm or "1/25/2007 12:00:16 AM" Both the date and time are combined on the same field...
8
by: =?Utf-8?B?UmljYXJkbyBRdWludGFuaWxsYQ==?= | last post by:
i need to convert data from string to nibble wich (nibble is a four bits representation) As example i have the following code string data1 = "12345678"; so ¿how can i convert this data...
1
by: AccessHunter | last post by:
Please Help.... I have a query with 2 tables, LegalFile and Cases. LegalFile has the field CaseID(Number Field) and Cases has CaseNbr(Text Field). I am trying to find entries that are in...
3
by: SMusic | last post by:
Can anyone find me a solution. I want to import csv file into mysql database. I have the code to insert.But the rows that contains comma in data field is not getting inserted and is skipped.All the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.