473,408 Members | 1,841 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,408 software developers and data experts.

Changing DATE Format HELP

I have a technical Date problem that's really difficult for me, I have a "custom made" Date format MM.DD.YY this is actually extracted from SAP and theirs no other format option offered such as ShortDate, LongDate etc. so now I making a sql query that must convert SAP date to a date Format that is recognizable to MS Access and I choose the ShortDate MM/DD/YY using ADO. I read about the Format() command using ADO online and experiment on it and hoping I get something right.

Expand|Select|Wrap|Line Numbers
  1. Public Sub ConvertDate()
Expand|Select|Wrap|Line Numbers
  1. Dim cnn1 As ADODB.Connection
  2. Dim rst1 As ADODB.Recordset
  3. Dim sql As String
  4. Dim mm As Variant
  5. Dim dd As Variant
  6. Dim yy As Variant
  7.  
  8. Set cnn1 = New ADODB.Connection
  9.  
  10. cnn1.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  11. "Data Source=" & CurrentProject.Path & _
  12. "\nope.mdb"
  13.  
  14.  
  15.  
  16. Set rst1 = New ADODB.Recordset
  17. rst1.Open "Sheet1", cnn1, adOpenDynamic, adLockPessimistic, adCmdTable
  18. sql = "SELECT * FROM Sheet1" & Format(rst1.Fields("MyDate").Value, "mm/dd/yy")
  19.  
  20. cnn1.Execute sql
  21. Debug.Print rst1.Fields("Mydate").Value
  22. End Sub



This one's causes a big error that's beyond my understanding and I was wondering if anyone could help me with it and a little explanation would be nice. It is very important that I solve this, so…pls…pls help ME!

Thanks in advance!
Jun 3 '08 #1
15 4593
Stewart Ross
2,545 Expert Mod 2GB
Hi Cephalon. To avoid getting into such a tangle you do need to be more systematic about what you are trying to do. Firstly, you need something that Access will recognise as a date. To do so, take the current date string from SAP and replace the '.' separators with the recognised '/' separators:
Expand|Select|Wrap|Line Numbers
  1. newdate = replace(SAPstring, ".", "/")
then convert this to a date - an actual date, not a string that is formatted to look like a date:
Expand|Select|Wrap|Line Numbers
  1. newdate = CDate(replace(SAPstring, ".", "/"))
and finally add this as a calculated field to a correct SQL statement (not an incorrect one as you show in your code)
Expand|Select|Wrap|Line Numbers
  1. sql = "SELECT *, CDate(replace(rst1.Fields("My Date"), ".", "/")) AS newdate FROM Sheet1" 

With newdate as a date and not a string you can use it in subsequent calculations, group values using the date functions to extract months or whatever, and format it in whatever way is best according to requirements.

Format is a very powerful and flexible function, but you were really starting with a string that looked something like a date, not a date as such. Worry about formatting when you have an actual date to work on.

-Stewart

ps It would really be appreciated if you would use code tags to delineate your code - it makes it so much easier to read. I have added them for you in this case.
Jun 3 '08 #2
Stewart Ross
2,545 Expert Mod 2GB
By the way, although I corrected the syntax of your SQL statement, as it stands it is not going to do anything for you - nothing useful at all. What do you expect to happen when you execute the SQL statement? You are creating a view of the data which is not subsequently being used. It does not persist outside of the routine in which you have placed it.

It seems to me that you do not need to use code at all. Just use an Access query on Sheet1 with the calculated field added to provide the new date.

-Stewart
Jun 3 '08 #3
Hi Stewart!

first of, thank you for the help. I copied your code as instructed and made some changes.

Expand|Select|Wrap|Line Numbers
  1. Public Sub ConvertDate()
  2. Dim cnn1 As ADODB.Connection 
  3. Dim rst1 As ADODB.Recordset
  4. Dim sql As String
  5. Dim newdate As Date
  6. Dim SAPstring As String
  7.  
  8. 'take the current date string from SAP and replace the '.' separator
  9. 'with the recognise '/' separators:
  10. 'newdate = replace(SAPstring, ".", "/")
  11. Set cnn1 = New ADODB.Connection
  12.  
  13. cnn1.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  14.             "Data Source=" & CurrentProject.Path & _
  15.             "\nope.mdb"
  16.  
  17. Set rst1 = New ADODB.Recordset
  18. rst1.Open "Sheet1", cnn1, adOpenDynamic, adLockPessimistic, adCmdTable
  19.  
  20. 'then convertthis date - an actual date, not a string that is formatted to look like a date:
  21. newdate = CDate(Replace(SAPstring, ".", "/"))
  22.  
  23. 'and finally add this as calculated field to correct SQL statement
  24. sql = "SELECT*, CDate(replace(rst1.Fields("MyDate"), ".", "/")) AS newdate FROM Sheet1"
  25.  
  26.  
  27. cnn1.Execute sql
  28. Debug.Print rst1.Fields(1).Value
  29.  
  30. End Sub
  31.  
  32.  
But when I tried running the code I get the compile error: Syntax error
on the sql part:
sql = "SELECT*, CDate(replace(rst1.Fields("MyDate"), ".", "/")) AS newdate FROM Sheet1"
did I missed somthing? pls. help

I apologize for my mistake on not putting code tags, I hope I made it right this time. About your question
What do you expect to happen when you execute the SQL statement?

this changing part is the first step, in the future when I get this right I'am hoping to populate the MyDate field with morethan 500 data and get their MAXDATE.
but for now I'm solving problem one at a time.




Thanks in advanced :)
Jun 4 '08 #4
Stewart Ross
2,545 Expert Mod 2GB
Hi Cephalon. There should have been a space between the SELECT and the asterisk in your code, but you missed it out:

Expand|Select|Wrap|Line Numbers
  1. SQL = "SELECT *, CDate(replace(rst1.Fields("MyDate"), ".", "/")) AS newdate FROM Sheet1"
I would also comment that I personally would not use a code solution as a first choice for what you want to achieve. Queries built in the Access editor or in SQL can do what you want in terms of finding max dates and so on without any form of coding. Code solutions are best when there are multiple sequential actions involved.

I use recordset-based code solutions when there is no practicable way to achieve something in SQL. Code solutions are very flexible, of course, but in the longer term other people have to maintain them - and SQL/Access queries are much more visible and maintainable than trying to find how something was solved in code.

It is interesting when reading posts on this site to find how many people have 'inherited' code written by others which they do not understand and are unwilling to rewrite or modify as a result. I can't say the same for SQL solutions, though.

-Stewart
Jun 4 '08 #5
Stewart Ross
2,545 Expert Mod 2GB
In addition to my previous post please comment out line 21 of your code in your last reply (post 4) - the one that begins newdate = .... You have included the line in your code but it was actually intended as an example to show you each of the steps involved in reaching a workable solution.

The only line you really need to include is the one for the SQL string, not the intermediate steps that got us there.
-Stewart
Jun 4 '08 #6
Thanks Stewart!

I tried what you said about putting space between SELECT and the * and I encountered a new compile error: Expected: end of statement and when I debug the code it keeps pointing on the ("MyDate") field.
Expand|Select|Wrap|Line Numbers
  1. Dim cnn1 As ADODB.Connection
  2. Dim rst As ADODB.Recordset
  3. Dim SQl As String
  4. Dim newdate As Date
  5. Dim SAPstring As String
  6.  
  7.  
  8. Set cnn1 = New ADODB.Connection
  9.  
  10.     With cnn1
  11.         .Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  12.             "Data Source=" & CurrentProject.Path & "\nope2.mdb"
  13.  
  14.     End With
  15. 'MsgBox "Provider:" & cnn1.Provider
  16.  
  17. Set rst = New ADODB.Recordset
  18.     With rst
  19.         .Open "Table1", cnn1, adOpenDynamic, adLockOptimistic, adCmdTableDirect
  20.     End With
  21. 'take the cuurent date string from SAP and replace the '.' separator
  22. 'with the recognise '/' separators.
  23. 'newdate = replace(SAPstring,".","/")
  24.  
  25. 'then convert this date - an actual date, not a string is formatted to look like a date.
  26. 'newdate = CDate(Replace(SAP, ".", "/"))
  27.  
  28. 'and finally add this as calculated field to correct sql statement
  29. SQL = "SELECT *, CDate(replace(rst1.Fields("MyDate"), ".", "/")) AS newdate FROM Table1"
  30.  
  31.  
  32.  
  33. cnn1.Execute SQl
  34. Debug.Print "Result: " & rst.Fields("MyDate")
  35.  
I feel so dumb now I tried looking for substitution like changing ("Mydate") to (1) but I got bigger errors, knowing its a big bad idea, I would like to ask if you have any idea why my compiler is sending me the error.

thank you for your patience.
Jun 5 '08 #7
Stewart Ross
2,545 Expert Mod 2GB
Hi Cephalon. In your original post, post 1, you defined an ADO recordset called rst1 and referred to a field within its fields collection called "MyDate" - rst1.fields("MyDate"). However, in the current version of your code you are defining a slightly differently named recordset variable, rst,not rst1. This inconsistency will cause a compile error if (as you should) you have Option Explicit on, which will make sure that all variables are defined before use.

Even if you are not using Option Explicit - and you really should! - the attempt to qualify the undefined variable rst1 with the .fields property reference will cause a compile error as that property will not be recognised for a variant type which is not defined as a recordset.

(I am assuming that the name of the field is indeed "MyDate" as in your original post, as I have no way of knowing the real name of your field if it isn't.)

You may see from experiencing these errors why I have advised you that an Access query-based SQL solution is in so many ways a simpler and better approach in the circumstances. However, fix the current error and see where this gets you.

I would be interested to know what it is you are coding after this first step is completed - as I think it should be possible to come up with an SQL alternative that would be equivalent without using the code approach at all.

-Stewart
Jun 5 '08 #8
NeoPa
32,556 Expert Mod 16PB
Cephal0n, I'm pleased to see you taking Stewart's advice on board (particularly about the CODE tags). His advice is usually sound, and the point he makes about considering a solution in SQL rather than a more complicated and hard to maintain one in VBA is particularly so.

It's never easy to take on fundamental changes when you're a bit lost, but if you make sure you keep a good backup then it can be tried without risk. You may even learn some fundamental concepts that will help you in your future database work ;)
Jun 6 '08 #9
rajanji
31
Hi,

Plz send the code to change the Date Format in VB.Net....


Thanks and Regards,

Rajan Arora
Jun 7 '08 #10
youmike
69
tried what you said about putting space between SELECT and the * and I encountered a new compile error: Expected: end of statement and when I debug the code it keeps pointing on the ("MyDate") field.
There's a semicolon missing at the end of the SQL Statement
Jun 8 '08 #11
Stewart Ross
2,545 Expert Mod 2GB
Hi youmike. The presence or absence of a semicolon at the end will not stop the SQL statement from running, but thanks for checking the statement too...

-Stewart
Jun 9 '08 #12
Stewart Ross
2,545 Expert Mod 2GB
Hi Rajan. Hijacking this thread to ask a different question is not appropriate, and in any event it is very unclear what you require. I cannot answer your post at present.

If you use the SEARCH facilities you should be able to find other posts which may answer your query within the VB forum.
Plz send the code to change the Date Format in VB.Net....
MODERATOR
Jun 9 '08 #13
Hi guys! I apologize for the delayed reply. Anyway I'm still working on my database.

Hi Stewart thank you for the logical opinion on how I should work on my coding
and when to use a more effective and useful solution. to be honest I really don't
know what came over me and why I keep pursuing coding it in VBA instead of using
simple sql. anyway I decide to use the REPLACE and FORMAT in my queries to get the Max
Date quicker and faster. I have a last question, is it possible to hide a query from
the Objects > Query, to avoid any changes made on my query design and for security purposes.

Although I made a decision on the using sql queries instead of vba, I will not give up
on the code you provided, I'm still working on the errors and hope I get it right soon.


thanks for the Big Help......All!
Jun 10 '08 #14
NeoPa
32,556 Expert Mod 16PB
If you Right-Click on the database object (Query; Table; Form; whatever) and select Properties, then there is a CheckBox to set the Hidden attribute.
Jun 10 '08 #15
Thanks NeoPa!

I tried what you said and it works!
Jun 11 '08 #16

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

Similar topics

1
by: Shaiboy_UK | last post by:
Hi All, Sorry if this is the wrong newsgroup to post into, on this topic, if so, please point me in the right direction..... Currently working on a ASP for a friend, which requires the date...
1
by: jt | last post by:
I posted this yesterday, but I am not seeing this out yet: I am having problems with updating a date field in a certain format. The data is stored in an Oracle database. The date is...
2
by: Chris | last post by:
Hello, i'm using a form with 2 dates in the format mm/dd/yyyy in it. I wanna calculate the days between the two dates. I tried it like this: <script language="JavaScript"> var date1 = new...
2
by: bh | last post by:
Changing a Date Time Value I have a form with a date time picker or it and a button. When the button is pressed it passes the date/time into a stored procedure that queries an SQL database. ...
20
by: andreas | last post by:
When I copy a vb.net project using date formats from one PC with a windows date format f.e. dd/mm/yyyy to another PC having a format yy/mm/dd then I get errors. How can I change for a while in the...
0
by: Kyote | last post by:
In my project I have an underlying Access DB. One of the column's is currently set as a text field but I need to change it to a date/time field. I can open the Access DB in MS Access and change...
5
mgpsivan
by: mgpsivan | last post by:
Hi, i have form in Asp.net(2003) in that i've to change the format of the date by the following statement Format(Date.Today, "dd/mm/yyyy") but when i give this code the month part displays as...
0
by: Just4U | last post by:
Hello, My config : Windows 2003 x64 (US VERSION) + sp2, IIS 6 All regional settings to French (even for default user) In my ASP pages dates are in the good format (french:dd/mm/yyyy), but...
1
by: morrbo | last post by:
hi all, just wanted to pose a quick question about changing the date format in month calendar, i'm using vb2005. i wish to change the date format from the standard (1st dec 07 as an example)...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...

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.