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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check a Date in Word

Status
Not open for further replies.

congelator

Instructor
Nov 10, 2002
15
0
0
CH
I need your help before i become crazy !!
How can I do to check, in Microsoft Word, if the text entered in the vba date field is really a date and not just text ?
 
Use the IsDate VBA function. Here is an excerpt from the VBA help file:

IsDate Function Example
This example uses the IsDate function to determine if an expression can be converted to a date.

Dim MyDate, YourDate, NoDate, MyCheck
MyDate = "February 12, 1969": YourDate = #2/12/69#: NoDate = "Hello"
MyCheck = IsDate(MyDate) ' Returns True.
MyCheck = IsDate(YourDate) ' Returns True.
MyCheck = IsDate(NoDate) ' Returns False.

Clive [infinity]
 
Thanks Clive,
I tried but it doesn't work as I try to do. In fact, I have a date field. If someone write a date, the program goes on. If it's not a date, I want to show a msgbox saying something

If you can help me, that would be great because I can't find it even in books I have... B-(
 
Can you not do something like this:
Code:
if not IsDate(whateverYouWantToCheck) then
'do something
end if
Clive [infinity]
 
Hi Clive,

Thanks for your help. I finally find a way to check the date's field with differents others checks. Here is the code if that could be usefull... Sorry for the French comments but I do speak French...

In "ThisDocument"

'**************************************************************************
'*** VERIFICATION TBOX_DATE_1 ET TBOX_DATE_2 SI LE TEXTE ENTRE EST UNE DATE OU NON ***
'**************************************************************************

Public Function Date_OK(date_a_tester As String, ByRef date_correcte As String) As Boolean

'*** CHECK IF USER PUT THE DAY IN 1 OR 2 DIGITS DEPENDING OF THE . POSITION ***
'si le . est avant la 2ème position ou après le 3ème
If InStr(date_a_tester, &quot;.&quot;) < 2 Or InStr(date_a_tester, &quot;.&quot;) > 3 Then
'la date est fausse
Date_OK = False
Close
End If
'si le . est en 2ème position
If InStr(date_a_tester, &quot;.&quot;) = 2 Then
'ajout du 0 suivi du chiffre (date_a_tester) entré par l'utilisateur
date_a_tester = &quot;0&quot; + date_a_tester
End If

'*** CHECK IF USER PUT THE MONTH IN 1 OR 2 DIGITS DEPENDING ON THE . POSITION ***

'si le . est avant la 5ème position ou après la 6ème
If InStr(date_a_tester, &quot;.&quot;) < 5 Or InStr(date_a_tester, &quot;.&quot;) > 6 Then
Date_OK = False
Close
End If

Dim B As String
B = date_a_tester

If InStr(4, date_a_tester, &quot;.&quot;) = 5 Then
B = Left(date_a_tester, 3) + &quot;0&quot; + Right(date_a_tester, Len(date_a_tester) - 3)
Else: B = date_a_tester
End If

date_a_tester = B

'*** CHECK IF USER PUT THE YEAR IN 1 OR 2 DIGITS DEPENDING ON THE . POSITION ***

'si le . est avant la 5ème position ou après la 6ème
If Not (Len(date_a_tester) = 8 Or Len(date_a_tester) = 10) Then
Date_OK = False
Close
End If

' si la longueur de date_a_tester est = à 8 (au lieu de 10), après 6 chiffres, on ajoute
' le chiffre 20 (pour 2000) et on ajoute les 2 derniers caractères
If Len(date_a_tester) = 8 Then
date_a_tester = Left(date_a_tester, 6) + &quot;20&quot; + Right(date_a_tester, 2)
End If

'*** CHECK THE ENTERED DIGITS ***
Dim A As Boolean
A = True
'le 1er chiffre doit avoir une longueur de 1 et être = à 0 ou 1 ou 2 ou 3 (jusqu'à 31 jours !) .
A = A And (Mid(date_a_tester, 1, 1) = &quot;0&quot; Or Mid(date_a_tester, 1, 1) = &quot;1&quot; Or Mid(date_a_tester, 1, 1) = &quot;2&quot; Or Mid(date_a_tester, 1, 1) = &quot;3&quot;)
'le 1er chiffre ne peut être sup. à 32
A = A And Mid(date_a_tester, 1, 2) < 32
'le 3ème chiffre doit avoir une longueur de 1 et être un .
A = A And Mid(date_a_tester, 3, 1) = &quot;.&quot;
'le 6ème chiffre doit avoir une longueur de 1 et être un .
A = A And Mid(date_a_tester, 6, 1) = &quot;.&quot;
'le 4ème chiffre doit avoir une longueur de 1 et être = à 0 ou 1 (pas de mois > 12 !).
A = A And (Mid(date_a_tester, 4, 1) = &quot;0&quot; Or Mid(date_a_tester, 4, 1) = &quot;1&quot;)

'*** CHECK THE DATE ***
date_correcte = date_a_tester
B = date_correcte
'insertion, dans la date_a_tester, à la 3ème pos. d'un / qui doit avoir une longueur de 1
Mid(B, 3, 1) = &quot;/&quot;
'insertion, dans la date_a_tester, à la 6ème pos. d'un / qui doit avoir une longueur de 1
Mid(B, 6, 1) = &quot;/&quot;
' vérification de la date en fonction du format de date
Date_OK = A And IsDate(B)

End Function

AND FOR THE CONTROL :

Private Sub txtbox_date_1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim date_correcte As String

If Not ThisDocument.Date_OK(txtbox_date_1, date_correcte) Then
MsgBox &quot;VOUS AVEZ ENTRE UN&quot; & vbCr & &quot;MAUVAIS FORMAT DE DATE&quot;, 48, &quot;ATTENTION&quot;
Close
Else: txtbox_date_1.Value = date_correcte
End If
End Sub
 
But Clive's solutions would have worked just fine, with a lot less work...
Rob
[flowerface]
 
Your're right Rob but I could write any date format in that field that's why I used such a code :-( to check date field and date format...

Ced
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top