PDA

View Full Version : [SOLVED:] Word Does Not Recognise Date As Value?



kibl1
08-07-2018, 02:02 PM
Only a quick question hopefully. I have set a userform to only show certain textboxes if entries are made to a preceeding textbox. This seems to work at first as if i type into the first textbox then the following one becomes invisible which is what i want. However as soon as i use the / charachter the next textbox reappears and i cannot quite workout why?

I was simply using
If TextBox1.Vaule = True Then
TextBox2.Visible = False

My ultimate goal was to force a date into a certain format dd/mmmm/yy. However i have no datepicker and no option to include one from the Additional Controls or Reference so looks like i will just have to trust that people will enter a date correctly.

macropod
08-07-2018, 03:47 PM
Try:

Private Sub TextBox1_Change()
Me.TextBox2.Visible = (Me.TextBox1.Text = "")
End Sub

gmayor
08-07-2018, 08:33 PM
If you want a VBA date picker, I quite like the one Trevor Eyre developed for Excel - https://trevoreyre.com/portfolio/excel-datepicker/
It can easily be modified to work with Word VBA. Import the userform from the zip to your project and then create a module to hold the following code examples to make it work.
You can then use the output from the form to fill your text box in whatever format you wish.


Option Explicit

Sub Test()
MsgBox BasicCalendar(Date)
'MsgBox AdvancedCalendar(Date)
'MsgBox AdvancedCalendar2(Date)
lbl_Exit:
Exit Sub
End Sub

Function BasicCalendar(StartDate As Date) As String
Dim oFrm As New CalendarForm
BasicCalendar = oFrm.GetDate( _
SelectedDate:=StartDate, _
TodayFontColor:=RGB(255, 0, 0))
lbl_Exit:
Set oFrm = Nothing
Exit Function
End Function

Function AdvancedCalendar(StartDate As Date) As String
Dim oFrm As New CalendarForm
AdvancedCalendar = oFrm.GetDate( _
SelectedDate:=StartDate, _
FirstDayOfWeek:=Monday, _
DateFontSize:=12, _
TodayButton:=True, _
OkayButton:=True, _
ShowWeekNumbers:=True, _
BackgroundColor:=RGB(243, 249, 251), _
HeaderColor:=RGB(147, 205, 2221), _
HeaderFontColor:=RGB(255, 255, 255), _
SubHeaderColor:=RGB(223, 240, 245), _
SubHeaderFontColor:=RGB(31, 78, 120), _
DateColor:=RGB(243, 249, 251), _
DateFontColor:=RGB(31, 78, 120), _
TrailingMonthFontColor:=RGB(155, 194, 230), _
DateHoverColor:=RGB(223, 240, 245), _
DateSelectedColor:=RGB(202, 223, 242), _
SaturdayFontColor:=RGB(0, 176, 240), _
SundayFontColor:=RGB(0, 176, 240), _
TodayFontColor:=RGB(0, 176, 80))
lbl_Exit:
Set oFrm = Nothing
Exit Function
End Function

Function AdvancedCalendar2(StartDate As Date) As String
Dim oFrm As New CalendarForm
AdvancedCalendar2 = oFrm.GetDate( _
SelectedDate:=StartDate, _
DateFontSize:=11, _
TodayButton:=True, _
BackgroundColor:=RGB(191, 225, 192), _
HeaderColor:=RGB(87, 98, 52), _
HeaderFontColor:=RGB(255, 255, 255), _
SubHeaderColor:=RGB(191, 225, 192), _
SubHeaderFontColor:=RGB(55, 86, 35), _
DateColor:=RGB(242, 248, 238), _
DateFontColor:=RGB(87, 98, 52), _
SaturdayFontColor:=RGB(55, 86, 35), _
SundayFontColor:=RGB(55, 86, 35), _
TrailingMonthFontColor:=RGB(106, 163, 67), _
DateHoverColor:=RGB(198, 224, 180), _
DateSelectedColor:=RGB(191, 225, 192), _
TodayFontColor:=RGB(255, 0, 0), _
DateSpecialEffect:=fmSpecialEffectRaised)
lbl_Exit:
Set oFrm = Nothing
Exit Function
End Function

gmaxey
08-07-2018, 08:48 PM
Or you can download the ready made Word template here which was developed in collaboration with Trevor.

https://gregmaxey.com/word_tip_pages/date_picker_form.html

kibl1
08-12-2018, 04:33 PM
Thank you. I have implemented the first solution and the other two are in my favourites for the future version 2 of this document. :)