×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Need a true-false expression that will identify if a string contains a date

Need a true-false expression that will identify if a string contains a date

Need a true-false expression that will identify if a string contains a date

(OP)
There's probably a very simple answer to this but I haven't found it yet.

I'm hoping someone can help me write an expression that will evaluate to true when looking at a string that contains any date value. If there is no date value in the string it would evaluate to false.

As always, thanks in advance for your assistance.

RE: Need a true-false expression that will identify if a string contains a date

(OP)
One clarification. The string would contain the date value along with other information. It would not be a string field composed exclusively of a date. I'm trying to return a true by evaluating a field like this one. "Tom Jones, 2/1/1980, London, England", but return a false if it's "Tom Jones, London, England".

RE: Need a true-false expression that will identify if a string contains a date

Hi,

So will there ALWAYS be a SPACE and/or COMMA delimiting the date?

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Need a true-false expression that will identify if a string contains a date

(OP)
No, that was a poor example. It will be a name like Fred, then space/hyphen/space, then the date, then space/hyphen/space, like this: Fred - 2/19/2018 - Plus text to follow. The field will always begin with an name identifying who wrote a note, the date the note was inserted, and then the text of the note itself. Of course, the name and date will be different all the time. I'm sorry I wasn't more clear.

RE: Need a true-false expression that will identify if a string contains a date

CODE -->

Public Function HasDate(varText As Variant) As Boolean
  Dim aText() As String
  Dim I As Integer
  If Not IsNull(varText) Then
    aText = Split(varText, "-")
    For I = 0 To UBound(aText)
      If IsDate(Trim(aText(I))) Then
        'Debug.Print aText(I)
        HasDate = True
        Exit Function
      End If
    Next I
  End If
End Function 

Selec field1, field2... HasDate([YourTextField]) as HasDate from sometable

RE: Need a true-false expression that will identify if a string contains a date

This may be more robust.

CODE -->

Public Function HasDate2(strWord As Variant) As Boolean
  
  'Need Microsoft VBScript Regular Expressions
   Dim objRegExp As VBScript_RegExp_55.RegExp
   Dim objMatch As VBScript_RegExp_55.match
   Dim myPattern As String
   'Match for all emails
   If Not IsNull(strWord) Then
   myPattern = "\d{1,2}/\d{1,2}/\d{4}"
  'Create a regular expression object.
   Set objRegExp = New RegExp
  'Set the pattern by using the Pattern property.
   objRegExp.Pattern = myPattern
  'Set Case Insensitivity.
   objRegExp.ignorecase = False
  'Set global applicability. Not sure what that does
   objRegExp.Global = True
  'Test whether the String can be compared. Not sure what that does
    If (objRegExp.test(strWord) = True) Then
       Set ReturnMatches = objRegExp.Execute(strWord)   ' Execute search.
       If ReturnMatches.Count > 0 Then
         Debug.Print "has date"
         HasDate2 = True
       End If
     End If
  End If
 '
End Function 

RE: Need a true-false expression that will identify if a string contains a date

(OP)
Thanks, MajP. I won't be able to check it till tomorrow. I'll let you know.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close