Word VBA- Date Validation in textbox in user form

mmmmmmm 1 Reputation point

I have a table that is setup like:

[textbox1] [textbox7] [textbox13] [textbox19] [textbox25] [textbox31]

Textbox 25 is set up so that if the date is after 11/12/2020 then what is entered into the text box will go onto doc1 and doc2 will be left empty and vice versa if the date is before than. I have this code below that lets users fill out textboxx25 if it is relevant to a user but it can be left blank.

(Doc1 and Doc2 are both public in a module and chosen using a file selector)

How could I setup the code below so that textboxx25 can be left blank in addition to the textbox having a rule that if a user types out the date in the textbox and its not in this format ( mm/dd/yyyy ) then the user will get a msgbox that says please format as (mm/dd/yyyy) and have to retype the date until its in the right format or just leave the textbox empty?

Private Sub CommandButton1_Click()

Dim s As String

s = Me.TextBoxx25.Text

If s = "" Then

ElseIf DateDiff("d", s, Me.TextBoxx25.Text, "11/12/2020") > 0 Then

Doc1.Variables("TextBoxx25").Value = " "

Doc1.Variables("TextBoxx1").Value = " "

Doc1.Variables("TextBoxx7").Value = " "

Doc1.Variables("TextBoxx13").Value = " "

Doc1.Variables("TextBoxx19").Value = " "

Doc1.Variables("TextBoxx31").Value = " "

Doc2.Variables("TextBoxx25").Value = Me.TextBoxx25.Text

Doc2.Variables("TextBoxx1").Value = Me.TextBoxx1.Text

Doc2.Variables("TextBoxx7").Value = Me.TextBoxx7.Text

Doc2.Variables("TextBoxx13").Value = Me.TextBoxx13.Text

Doc2.Variables("TextBoxx19").Value = Me.TextBoxx19.Text

Doc2.Variables("TextBoxx31").Value = Me.TextBoxx31.Text


Doc1.Variables("TextBoxx25").Value = Me.TextBoxx25.Text

Doc1.Variables("TextBoxx1").Value = Me.TextBoxx1.Text

Doc1.Variables("TextBoxx7").Value = Me.TextBoxx7.Text

Doc1.Variables("TextBoxx13").Value = Me.TextBoxx13.Text

Doc1.Variables("TextBoxx19").Value = Me.TextBoxx19.Text

Doc1.Variables("TextBoxx31").Value = Me.TextBoxx31.Text

Doc2.Variables("TextBoxx25").Value = " "

Doc2.Variables("TextBoxx1").Value = " "

Doc2.Variables("TextBoxx7").Value = " "

Doc2.Variables("TextBoxx13").Value = " "

Doc2.Variables("TextBoxx19").Value = " "

Doc2.Variables("TextBoxx31").Value = " "

End If



End Sub

'Thanks in advance!

0 comments No comments
{count} votes