JohnLM 27 Posted Monday at 03:07 AM (edited) Specs: Windows 7, Delphi XE7, VCL Whenever my app comes across a date year like 1899, my app throws the error: "12/30/1899 8:00:00 AM' is not a valid date." I need a way to detect certain date(s), like the one above, and when found, I guess I will skip over it and just write "N/A" or something for those items, or else I will get the error mentioned above. Any suggestions of how to detect or avoid this error ? Edited Monday at 05:28 PM by JohnLM typo in subj title Share this post Link to post
weirdo12 26 Posted Monday at 03:18 AM Do you mean that you get that error when assigning a value to a date field that is part of a TDataSet? Share this post Link to post
JohnLM 27 Posted Monday at 04:52 AM I did some debugging and the error is EConvertError. And I was able to figure it out before consulting with Google. So, I need to set up a try/except statements. Then, I needed Google show me how to set up a try/except, and make some changes and now have the following codesnippet working. . . try dbDate := StrToDate(qry.FieldByName('mDate' ).AsString); thedate := FormatDateTime('yyyy-mm-dd ddd',dbdate); dateS := thedate; except on E: EConvertError do begin sb1.Panels[0].Text := 'Error: invalid date!'; // <-- a statusbar control end; end; But I'm not sure I need to add a Finally part in this While EOF() End block. But it does work without throwing up an error message and bailing out without any output. Share this post Link to post
JohnLM 27 Posted Monday at 04:56 AM And to be clear, I don't mean Google A-EYE, I mean performing a google search and reviewing the output results from the webpage returned. Share this post Link to post
Remy Lebeau 1636 Posted Monday at 06:54 AM (edited) If you use TryStrToDate() instead then you can avoid the try..except, eg: if TryStrToDate(qry.FieldByName('mDate' ).AsString, dbDate) then dateS := FormatDateTime('yyyy-mm-dd ddd', dbDate) else sb1.Panels[0].Text := 'Error: invalid date!'; Edited Monday at 06:56 AM by Remy Lebeau 1 Share this post Link to post
JohnLM 27 Posted Monday at 06:46 PM @Remy Lebeau - mdate is a TDate, not a String. So that won't work. Sorry for the confusion. Share this post Link to post
Remy Lebeau 1636 Posted Monday at 07:32 PM 43 minutes ago, JohnLM said: @Remy Lebeau - mdate is a TDate, not a String. So that won't work. Sorry for the confusion. ? There is no mdata in the code you provided. Do you mean dbdate? The code I presented is just a simplification of the code you provided, so it should compile. StrToDate() returns a TDateTime, and TryStrToDate() outputs a TDateTime. TDate is just an alias for TDateTime. Share this post Link to post
JohnLM 27 Posted Monday at 10:04 PM Let me try and explain my situation. . . #1. I am pulling the date (mDate) from an MS Access database, "db.mdb" via FireDac. The Data Type is a Date/Time field in that database. #2. But, I have a Delphi app that reads the .mdb file and displays it a certain way for me, and I add new entries through that app on a daily bases. In this app, I have the date formatted as "yyyy-mm-dd, ddd" for readability. I prefer that layout when using this app. screen snippet below. Note, the memo field in this Delphi app shows where I enter multi-line text of activities. I just added these entries for convenience and simplicity of this conversation. #3. Then, in another Delphi app (this is an experimental search db app I am testing) that I just started working on, to search-for-text in the myNotes field for certain text that I entered since its inception, this is the app where I am receiving that EConvertError message while doing the date conversions. I am using the mDate from the db.mdb in #1 in this app, #3. I am displaying it in a search results window like the screenshot below. In this app, #3, I am debugging different parts of this app for ideas on how to accomplish what I want. Using a regular memo control on the form, I am trying to decide on an output layout and what should show as the final results from the found text from the datase memo field from the database in #1, as seen in the output of #2 from the db memo field. So, I am showing a custom formated date as part of the output results in app #3. The issue and this discussion has to do this the mDate being converted in app #3. I hope this explains better my goals and what I am doing with this project at this time. Share this post Link to post
Remy Lebeau 1636 Posted Tuesday at 01:55 AM Why are you reading the mDate field as a String and not as a TDateTime to begin with? And are you populating your UI controls manually instead of using data-aware controls? If using a data-aware UI, you can configure a data field to *display* in a particular format, without having to actually pull the data and format it yourself. You say you have a date/time field, but then you talk about a Memo field. Which one are you actually parsing? I'm still not clear where your error is. Your screenshots don't match the code snippet you provided. 2 Share this post Link to post
JohnLM 27 Posted Tuesday at 05:41 PM (edited) TryStrToDate() is Resolved !! Again, sorry for the confusion. I am no database guru. The memo I was referring to are like this: #1. The first one is the originating source, the one from the ms access .mdb file, which contains the Data Type: Memo #2. The second memo type is from the Delphi app, which is a TDBMemo. I am using FireDAC, dbgrid, tfdconnect, tfdquery and dbsource to open/use/edit the ms access database. Anyway, I have resolved the TryStrToDate() issue in the #3 app (where the issue was). I had defined the dbDate as TDate (as you pointed out earlier). And when I changed it to TDateTime, it compiled, and the routine works successfully now. Thank you! Edited Tuesday at 05:42 PM by JohnLM added additional text Share this post Link to post