Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

TheresAlwaysAWay

Programmer
Mar 15, 2016
135
US
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.
 
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".
 
Hi,

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

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
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.
 
Code:
[b]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[/b]

Selec field1, field2... HasDate([YourTextField]) as HasDate from sometable
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top