|
-
Aug 23rd, 2007, 12:45 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Cdate Data Type Mismatch
VB6 SP6
I am try to convert a date string to date and insert it into a table (modDate Date). I am getting a data mis-match error but in debug, it appears that the data has been converted.
Code:
Dim ModDate1 As Date
ModDate1 = CDate(Format(flDrawings.ModDate, "MM/DD/YYYY"))
"VALUES ('" & IIf(IsNull(Name$), "a", RTrim(Name$)) & "','" & IIf(IsNull(Revision$), Null, RTrim(Revision$)) & "','" & IIf(IsNull(Descr$), Null, RTrim(Descr$)) & "'," & ModDate1 & ")"
Immediate Results
INSERT INTO engfiles(drwName,Rev,Descr,ModDt)VALUES ('WIDGETII','a','',7/12/2007)
If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
Do illiterate people get the full effect of Alphabet Soup?
ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool
-
Aug 23rd, 2007, 04:13 AM
#2
Re: Cdate Data Type Mismatch
why would you format the date data then convert it to a date as the formatting will be lost and the date will be as per you systems date format
which part of the code gives the error?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 23rd, 2007, 07:56 AM
#3
Re: Cdate Data Type Mismatch
As westconn1 correctly said, the Date data type does not store formatting - only the value (and even tho it seems to be displayed as text, it is certainly not stored that way).
Depending on what format you are using (and the system date format) this is likely to just change the date. 7/12/2007 could be July 12th or Dec 7th, and after running that code you cannot be sure which will be stored in your variable.
If you want to store the formatted value to a variable, store it to a String variable.
Next up, to use a date in an SQL statement you need to put some sort of delimiter around the value (otherwise you are basically using the mathmatical calculation (7/12)/2007 , which is 2.9e-4 !).
To see which delimiter you need, read the article How do I use values (numbers, strings, dates) in SQL statements? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)
-
Aug 23rd, 2007, 08:01 AM
#4
Re: Cdate Data Type Mismatch
What is the backe end DB. If Access then the date value (7/12/2007) needs to be surrounded by pould signs (#7/12/2007#). If SQL Server then surround with single qoutes ('7/12/2007'). Oracle needs the To_Date Function.
MS Access
Code:
VALUES ('" & IIf(IsNull(Name$), "a", RTrim(Name$)) & "','" & IIf(IsNull(Revision$), Null, RTrim(Revision$)) & "','" & IIf(IsNull(Descr$), Null, RTrim(Descr$)) & "',#" & ModDate1 & "#)"
SQL Server
Code:
VALUES ('" & IIf(IsNull(Name$), "a", RTrim(Name$)) & "','" & IIf(IsNull(Revision$), Null, RTrim(Revision$)) & "','" & IIf(IsNull(Descr$), Null, RTrim(Descr$)) & "','" & ModDate1 & "')"
Oracle
Code:
VALUES ('" & IIf(IsNull(Name$), "a", RTrim(Name$)) & "','" & IIf(IsNull(Revision$), Null, RTrim(Revision$)) & "','" & IIf(IsNull(Descr$), Null, RTrim(Descr$)) & ",To_Date('" & ModDate1 & "','MM/DD/YYYY'))"
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Aug 23rd, 2007, 08:44 AM
#5
Thread Starter
Hyperactive Member
Re: Cdate Data Type Mismatch
The data source for the date is from a fixed length file (ModDate As String * 10) which has been entered in as e.g. 8-22-2007.
The source DB is a VFP9 free table (modDate Date)
Code:
Dim ModDate1 as Date
ModDate1 = flDrawings.ModDate
"VALUES ('" & IIf(IsNull(Name$), "a", RTrim(Name$)) & "','" & IIf(IsNull(Revision$), Null, RTrim(Revision$)) & "','" & IIf(IsNull(Descr$), Null, RTrim(Descr$)) & "'," & ModDate1 & ")"
This is what I get on debug.print, it appears to be correct but errors.
INSERT INTO engfiles(drwName,Rev,Descr,ModDt)VALUES ('WIDGETII','a','',7/12/2007)
If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
Do illiterate people get the full effect of Alphabet Soup?
ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool
-
Aug 23rd, 2007, 08:46 AM
#6
Thread Starter
Hyperactive Member
Re: Cdate Data Type Mismatch
Also tried Gary's post with single quote
INSERT INTO engfiles(drwName,Rev,Descr,ModDt)VALUES ('WIDGETII','a','','7/12/2007')
If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
Do illiterate people get the full effect of Alphabet Soup?
ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool
-
Aug 23rd, 2007, 08:51 AM
#7
Re: Cdate Data Type Mismatch
For VFP, I would guess that date delimiter would be the same as Access (using #).
it appears to be correct but errors.
INSERT INTO engfiles(drwName,Rev,Descr,ModDt)VALUES ('WIDGETII','a','' ,7/12/2007)
As I explained before, that is definitely wrong - you need delimiters of some sort (otherwise you will be storing tiny numbers, rather than dates).
You also need to format date values before/whilst putting them into an SQL statement (either format them and store them to a string, or use the format function while appending them to the SQL).
-
Aug 23rd, 2007, 09:24 AM
#8
Re: Cdate Data Type Mismatch
 Originally Posted by si_the_geek
You also need to format date values before/whilst putting them into an SQL statement (either format them and store them to a string, or use the format function while appending them to the SQL).
Or store them as dates (so that things like BETWEEN work) and format them after retrieval.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Aug 23rd, 2007, 09:30 AM
#9
Thread Starter
Hyperactive Member
Re: Cdate Data Type Mismatch
Like this? Could you give me an example?
Code:
Dim ModDate1$
ModDate1$ = flDrawings.ModDate
'" & Format(ModDate1$, "MM/DD/YYYY") & "')
I tried
Code:
#" & Format(ModDate1$, "MM/DD/YYYY") & "#)
but got syntax error.
If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
Do illiterate people get the full effect of Alphabet Soup?
ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool
-
Aug 23rd, 2007, 10:12 AM
#10
Re: Cdate Data Type Mismatch
You could use this:
Code:
Dim ModDate1 as String
ModDate1 = Format(flDrawings.ModDate, "MM/DD/YYYY")
... & "',#" & ModDate1 & "#)"
or this:
Code:
Dim ModDate1 as Date
ModDate1 = flDrawings.ModDate
... & "',#" & Format(ModDate1, "MM/DD/YYYY") & "#)"
or this:
Code:
... & "',#" & Format(flDrawings.ModDate, "MM/DD/YYYY") & "#)"
(all of these are assuming that the delimiter is # , tho you may need to use ' instead).
 Originally Posted by Al42
Or store them as dates (so that things like BETWEEN work) and format them after retrieval.
Unless I read it incorrectly, the table values are Dates ("The source DB is a VFP9 free table (modDate Date) ").
Inside the SQL statement (which is a string), you cannot have actual dates - only strings that represent dates. To work correctly, they need to be formatted in a way that will be interpreted by the DBMS as you intended (which means formatting them as mm/dd/yyyy, or better yyyy-mm-dd).
If a command object was being used, using a Date value with a parameter object is correct - as it will basically do the formatting for you.
-
Aug 23rd, 2007, 11:18 AM
#11
Thread Starter
Hyperactive Member
Re: Cdate Data Type Mismatch
The source data is a fixed length file, the target is a VFP9 free table. (sorry I had source in there twice as the VFP table should have been target)
The date has been entered into the file in the format of "08-22-2007"
Code:
Public Type drwFile
Name As String * 200 'drawing name for the loop
Revision As String * 32 'revision value for this drawing
CrDate As String * 10 'date the drawing was created
ModDate As String * 10 'date the drawing was last changed/overwritten
If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
Do illiterate people get the full effect of Alphabet Soup?
ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool
-
Aug 23rd, 2007, 11:29 AM
#12
Re: Cdate Data Type Mismatch
Is the modDate field (in the database) a Date data type, or is it a String/Char?
-
Aug 23rd, 2007, 03:31 PM
#13
Thread Starter
Hyperactive Member
Re: Cdate Data Type Mismatch
It is a Date type. The source is string/char
If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
Do illiterate people get the full effect of Alphabet Soup?
ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool
-
Aug 23rd, 2007, 04:07 PM
#14
Re: Cdate Data Type Mismatch
In that case you need to use one of the formats that I mentioned, but you already have that from the source, with something that may or may not be issue (it has - rather than / ).
Depending on how VFP wants to receive dates, one of these will hopefully work for you:
... & "',#" & flDrawings.ModDate & "#)"
... & "','" & flDrawings.ModDate & "')"
... & "',#" & Replace(flDrawings.ModDate,"-","/") & "#)"
... & "','" & Replace(flDrawings.ModDate,"-","/") & "')"
If not, you should check the documentation to find out what it wants.
-
Sep 5th, 2007, 05:33 PM
#15
Thread Starter
Hyperactive Member
Re: Cdate Data Type Mismatch
Got it! The delimeter is Brackets
... "', {" & Replace(LstRefDat$, "-", "/") & "},
Last edited by Always_Confused; Sep 5th, 2007 at 05:37 PM.
If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
Do illiterate people get the full effect of Alphabet Soup?
ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool
-
Sep 6th, 2007, 09:46 AM
#16
Re: [RESOLVED] Cdate Data Type Mismatch
Excellent, thanks for sharing the solution. 
So that I can add it to the FAQ properly, can you just confirm that by VFP9 you mean MS Visual Foxpro 9?
-
Sep 6th, 2007, 10:43 AM
#17
Thread Starter
Hyperactive Member
Re: [RESOLVED] Cdate Data Type Mismatch
Yes VFP9 is Visual Foxpro v9. This should also be valid back to at least v6 as well.
For those looking for a client side or server side database far more robust than Access, look into Foxpro! Microsoft is still supporting Foxpro despite of what you hear and has just released Sedna to enhance the technologies to version 9. Version 9 is to be supported to 2015.
http://www.microsoft.com/downloads/d...ng=en#Overview
The DB stucture will be around for a long time and now has varchar fields. I have found it perfect for my situation as an intermediate step before taking the application to full blown SQL. And it requires no DB engine files to run in the background on individual installations as does MSDE and others.
Another plus, the vfpoledb driver is free to download via Microsoft and does not require you to purchase the application to develop the free tables or DB.
If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
Do illiterate people get the full effect of Alphabet Soup?
ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool
-
Sep 6th, 2007, 10:58 AM
#18
Re: [RESOLVED] Cdate Data Type Mismatch
Thanks for the confirmation - I'll add it to the FAQ now. 
While I see that VFP could be useful (and I'm sure many people will use it), there is a new version of SQL Server (CE) which does basically the same thing - with the added benefits that the upgrade path to a 'bigger' version of SQL Server is much simpler, and that it presumably runs on more devices (such as PDAs).
SQL Server CE is also a free download, and there is also has a free management tool for designing tables etc - there are links to both in the SQL Server 2005 thread at the top of the Database forum.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|