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

checking text date and changing if necessary

My need is to check an input date, like 01/10/492 and be able to recognize it is not correct date, and then enter a default date in that location. I have read and tried a few things, and am presently trying this

Expand|Select|Wrap|Line Numbers
  1.  IIf(DateValue([Field2])=[#error],"01/01/1990",[field2])
  2.  
the idea being if the datevalue function returned error, (it shows #error as return on my query, ) , then I would enter default date, and if not would maintain the same data by using the false argument.

Thank you for any help, I am looking for a user group, cause I want to learn but I am finding the reading very hard.

Ken
Dec 26 '07 #1
11 2814
FishVal
2,653 Expert 2GB
Hi, Ken.

I'm very unsure it is even possible to catch an error within expression.
I would like to recommend you to do it via VBA.

Just write the following simple function in a public code module:
Expand|Select|Wrap|Line Numbers
  1. Public Function ConvertDate(varInput As Variant) As Date
  2.  
  3.     On Error GoTo ConvertionFailed
  4.     ConvertDate=......<date function you use>
  5.     Exit Function
  6. ConvertionFailed:
  7.     ConvertDate = #1/1/1990#
  8.  
  9. End Function
  10.  
Then replace the whole IIf() expression in your query with
Expand|Select|Wrap|Line Numbers
  1. ConvertDate([The Name of date field you are working with])
  2.  
P.S. Are you still working on that problem ?
Dec 26 '07 #2
ADezii
8,834 Expert 8TB
My need is to check an input date, like 01/10/492 and be able to recognize it is not correct date, and then enter a default date in that location. I have read and tried a few things, and am presently trying this

(CODE)
IIf(DateValue([Field2])=[#error],"01/01/1990",[field2])
(/CODE)

the idea being if the datevalue function returned error, (it shows #error as return on my query, ) , then I would enter default date, and if not would maintain the same data by using the false argument.

Thank you for any help, I am looking for a user group, cause I want to learn but I am finding the reading very hard.

Ken
If the Input Value is being entered into a Form Field named txtDate, then:
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtDate_AfterUpdate()
  2. If IsNull(Me![txtDate]) Then
  3.   Exit Sub
  4. ElseIf Not IsDate(Me![txtDate]) Then
  5.   Me![txtDate] = #1/1/1990#
  6. Else
  7.   Me![txtDate] = Me![txtDate]
  8. End If
  9. End Sub
Dec 26 '07 #3
Hi, Ken.

I'm very unsure it is even possible to catch an error within expression.
I would like to recommend you to do it via VBA.

Just write the following simple function in a public code module:
Expand|Select|Wrap|Line Numbers
  1. Public Function ConvertDate(varInput As Variant) As Date
  2.  
  3.     On Error GoTo ConvertionFailed
  4.     ConvertDate=......<date function you use>
  5.     Exit Function
  6. ConvertionFailed:
  7.     ConvertDate = #1/1/1990#
  8.  
  9. End Function
  10.  
  11.  
  12.  
Then replace the whole IIf() expression in your query with
Expand|Select|Wrap|Line Numbers
  1. ConvertDate([The Name of date field you are working with])
  2.  
P.S. Are you still working on that problem ?
Yes I am still working. I took your advice,and replaced the null and items that evaluatied <0 to get to this last type of input error. Believe it or not, the CDate returns a valid date, for things like 5/5/392. This causes me issues with the person using this data. I will try your VB solution.
My main problem is, I want to convert the table column to Date/Time data type. and when I try it, if there is a mismatch at all, it threatens to remove any row with a bad date. So I am trying to change all the date (in text format) with your help.
Dec 26 '07 #4
Yes I am still working. I took your advice,and replaced the null and items that evaluatied <0 to get to this last type of input error. Believe it or not, the CDate returns a valid date, for things like 5/5/392. This causes me issues with the person using this data. I will try your VB solution.
My main problem is, I want to convert the table column to Date/Time data type. and when I try it, if there is a mismatch at all, it threatens to remove any row with a bad date. So I am trying to change all the date (in text format) with your help.

Ok Fish, I ran the function, and it ran ok, but it actually changed all dates to the default date, so It thinks all are errors I guess.

I put in CDate(Field2) which is the field containing the dates. Following are dates on the test list.


ID Field2 Field3
1 01/01/1990 00:00 AM
2 7/9/2003 10:15:30 PM
3 7 /9/2003 09:15 AM
4 5/5/192 01:25:15 PM
5 01/01/1990 08:15
6 01/01/1990 00:00 AM
7 8/5/07 00:00 AM
8 10/03/200 07:15
9 03/35/44 08:15
10 3/17/40 00:00 AM

all these ended up with the 1/1/1990 default date. Also if you can address the issue of a an erroneous date like 10/03/200 returning good on a straight CDate conversion.

Thank you for your help.

Ken
Dec 26 '07 #5
FishVal
2,653 Expert 2GB
Hi, Ken.

Your code should look like the following:
Expand|Select|Wrap|Line Numbers
  1. Public Function ConvertDate(varInput As Variant) As Date
  2.  
  3.     On Error GoTo ConvertionFailed
  4.     ConvertDate = CDate(varInput)
  5.     Exit Function
  6. ConvertionFailed:
  7.     ConvertDate = #1/1/1990#
  8.  
  9. End Function
  10.  
P.S.
Or it may me even more simple (though maybe not so clear):
Expand|Select|Wrap|Line Numbers
  1. Public Function ConvertDate(varInput As Variant) As Date
  2.  
  3.     ConvertDate = #1/1/1990#
  4.     On Error Resume Next
  5.     ConvertDate = CDate(varInput)
  6.  
  7. End Function
  8.  
Dec 26 '07 #6
Hi, Ken.

Your code should look like the following:
Expand|Select|Wrap|Line Numbers
  1. Public Function ConvertDate(varInput As Variant) As Date
  2.  
  3.     On Error GoTo ConvertionFailed
  4.     ConvertDate = CDate(varInput)
  5.     Exit Function
  6. ConvertionFailed:
  7.     ConvertDate = #1/1/1990#
  8.  
  9. End Function
  10.  
P.S.
Or it may me even more simple (though maybe not so clear):
Expand|Select|Wrap|Line Numbers
  1. Public Function ConvertDate(varInput As Variant) As Date
  2.  
  3.     ConvertDate = #1/1/1990#
  4.     On Error Resume Next
  5.     ConvertDate = CDate(varInput)
  6.  
  7. End Function
  8.  
Hi FishVal, here is what I did.

Orig Data:
ID Field2 Field3
1 01/01/1990 00:00 AM
2 7/9/2003 10:15:30 PM
3 7 /9/2003 09:15 AM
4 5/5/192 01:25:15 PM
5 01/01/1990 08:15
6 01/01/1990 00:00 AM
7 8/5/07 00:00 AM
8 10/03/200 07:15
9 03/35/44 08:15
10 3/17/40 00:00 AM

Here is code in Module:

(CODE)
Public Function ConvertDate(varInput As Variant) As Date
Dim Field2 As String
On Error GoTo ConvertionFailed
ConvertDate = CDate(Field2)
Exit Function
ConvertionFailed:
ConvertDate = #1/1/1990#
End Function
(/CODE)

Results:

ID Field2 Field3 Expr1
1 01/01/1990 00:00 AM 1/1/1990
2 7/9/2003 10:15:30 PM 1/1/1990
3 7 /9/2003 09:15 AM 1/1/1990
4 5/5/192 01:25:15 PM 1/1/1990
5 01/01/1990 08:15 1/1/1990
6 01/01/1990 00:00 AM 1/1/1990
7 8/5/07 00:00 AM 1/1/1990
8 10/03/200 07:15 1/1/1990
9 03/35/44 08:15 1/1/1990
10 3/17/40 00:00 AM 1/1/1990
Dec 26 '07 #7
FishVal
2,653 Expert 2GB
Sure, this will not work.
Look at the last variant of code I've posted.
Dec 26 '07 #8
Sure, this will not work.
Look at the last variant of code I've posted.
Hi Fish,
I am not sure where we are going here. If you mean the new type function you sent. I don't understand it at all.
Ken I thought the first one should have worked. It did , only just a little too well....
Dec 26 '07 #9
Hi Fish,
I am not sure where we are going here. If you mean the new type function you sent. I don't understand it at all.
Ken I thought the first one should have worked. It did , only just a little too well....
It worked.....yippee
Ken
I chgs the variable back to the variable....varinput...
as you had ...

Results..
ID Field2 Field3 Expr1
1 01/01/1990 00:00 AM 1/1/1990
2 7/9/2003 10:15:30 PM 7/9/2003
3 7 /9/2003 09:15 AM 7/9/2003
4 5/5/192 01:25:15 PM 5/5/192
5 01/01/1990 08:15 1/1/1990
6 01/01/1990 00:00 AM 1/1/1990
7 8/5/07 00:00 AM 8/5/2007
8 10/03/200 07:15 10/3/200
9 03/35/44 08:15 1/1/1990
10 3/17/40 00:00 AM 3/17/1940

This was mainly pointing at case nbr 9 and it worked great...thank you
now if I can get case 4 and 8 it will be over.
Ken
Dec 26 '07 #10
missinglinq
3,532 Expert 2GB
To begin with, checking the AfterUpdate status of a textbox control to see if it's Null is a wasted exercise! The AfterUpdate will only fire if a value has been manually entered into the textbox. If the box is entered but a value hasn't been entered, or if a value has been entered and then deleted, the value will always be Null, so checking for Null before proceeding further is, in this particular case, useless.

The standard way of checking to see if data is a date is to use the IsDate() function. What this function actually does is check to see if the data looks like a date! If so, it returns True. Both

IsDate(#12/26/2007#)

and

IsDate("12/26/2007")

return True.

IsDate() is useless, however, to test to see if 01/10/492 is a valid date, for the simple reason that 01/10/492 is a valid date! The year 492 did exist! Used with either IsDate() example given above, 01/10/492 will evaluate as True.

The only way I see to solve this problem is either to use an Input Mask or to check to see if 3 digits have been entered for the year. This code does that, and if 3 digits have been entered for the year, a default date is substituted.
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtDate_AfterUpdate()
  2.  If Len(Me.txtDate) - InStrRev(Me.txtDate, "/") = 3 Then
  3.   Me.txtDate = #1/1/1990#
  4.  End If
  5. End Sub
Linq ;0)>

Post script:
The above also explains why the code used in the last post doesn't work in cases #4 and #8.
Dec 27 '07 #11
To begin with, checking the AfterUpdate status of a textbox control to see if it's Null is a wasted exercise! The AfterUpdate will only fire if a value has been manually entered into the textbox. If the box is entered but a value hasn't been entered, or if a value has been entered and then deleted, the value will always be Null, so checking for Null before proceeding further is, in this particular case, useless.

The standard way of checking to see if data is a date is to use the IsDate() function. What this function actually does is check to see if the data looks like a date! If so, it returns True. Both

IsDate(#12/26/2007#)

and

IsDate("12/26/2007")

return True.

IsDate() is useless, however, to test to see if 01/10/492 is a valid date, for the simple reason that 01/10/492 is a valid date! The year 492 did exist! Used with either IsDate() example given above, 01/10/492 will evaluate as True.

The only way I see to solve this problem is either to use an Input Mask or to check to see if 3 digits have been entered for the year. This code does that, and if 3 digits have been entered for the year, a default date is substituted.
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtDate_AfterUpdate()
  2.  If Len(Me.txtDate) - InStrRev(Me.txtDate, "/") = 3 Then
  3.   Me.txtDate = #1/1/1990#
  4.  End If
  5. End Sub
Linq ;0)>

Post script:
The above also explains why the code used in the last post doesn't work in cases #4 and #8.
Thank you very much for the insight. As I am still working on this I will try to understand and use what you have here. Thanks for taking the time. Ken
Dec 28 '07 #12

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

Similar topics

13
by: Eddie | last post by:
I need to validate a text input field. I just want to say if user enters 93101 or 93102 or 93103 or 93105 or 93106 or 93107 or 93108 or 93109 or 93110 or 93111 or 93116 or 93117 or 93118 or...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
0
by: FAQPoster | last post by:
An HTML version of this document is available at: http://www.mvps.org/access/netiquette.htm Feeling left out? Alone? Wondering why everyone's ignoring you? Or why you're being flamed for what...
1
by: James Edwards | last post by:
There seems to be a bug in the link-library dependency checking in VS.NET 2003. Under the Configuration->Linker->General->Additional Library Directories property, if you specify a directory name...
8
by: John Wildes | last post by:
Hello all I'm going to try and be brief with my question, please tell me if I have the wrong group. We are querying transaction data from a DB3 database application. The dates are stored as...
4
by: franco | last post by:
I am a beginner in programming. How can I display the system date - 5 December 2005 in my webpage.
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
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...
27
by: RobG | last post by:
I was investigating a function to determine whether daylight saving was being observed on a particular date (given the platform's regional settings) and came across a suggestion at merlyn.com to...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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,...
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...

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.