Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

inserting a default date in a textbox in a userform

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
HRCarlsberg - 10 Jan 2005 09:33 GMT
How can I insert a date as a default in a textbox in a userform? It is
important, that the date is shown in a certain format (dd.mm.yyyy). The user
should be able to change the date to another date, but not to anything else
(numbers, text).....

Can anyone please help me? Thank you very much!
Jay Freedman - 10 Jan 2005 17:55 GMT
>How can I insert a date as a default in a textbox in a userform? It is
>important, that the date is shown in a certain format (dd.mm.yyyy). The user
>should be able to change the date to another date, but not to anything else
>(numbers, text).....
>
>Can anyone please help me? Thank you very much!

Putting today's date into the textbox (assumed to be named tbxDate) is
pretty simple:

Private Sub UserForm_Initialize()
   tbxDate.Text = Format(Now, "dd.MM.yyyy")
End Sub

Note that Now is the current system date/time, and the Format function
will make a string from it using the pieces specified in the format
string. The capitalization of the MM is important because lower case
mm is interpreted as the minutes field of Now, not the month.

Restricting the user to entering valid dates is a bit trickier,
particularly because your format with periods as separators isn't
normally recognized by VBA as a valid date format. The idea is to
write an Exit routine for the textbox, which VBA automatically calls
when the focus is about to leave the box:

Private Sub tbxDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   If (Not IsDate(tbxDate.Text)) And _
       (Not IsDate(Replace(tbxDate.Text, ".", "/"))) Then
       MsgBox prompt:="Please enter a valid date", _
           Title:="Date Error"
       With tbxDate
           .SelStart = 0
           .SelLength = Len(.Text)
       End With
       Cancel = True
   Else
       tbxDate.Text = Format(tbxDate, "dd.MM.yyyy")
   End If
End Sub

If the code sets the Cancel parameter to True, the focus doesn't
leave; the cursor stays in the textbox. I used the Replace function to
change any periods in the Text string to slashes so the IsDate
function will recognize your custom format in addition to all the
formats it already knows (e.g., "Jan. 10, 2005").

The .SelStart and .SelLength statements are just a little something
extra: instead of leaving the cursor at the end of the entry in the
textbox, they highlight the entire contents so the user can just start
typing to replace it.

--
Regards,
Jay Freedman
Microsoft Word MVP         FAQ: http://word.mvps.org
HRCarlsberg - 10 Jan 2005 20:37 GMT
THANK YOU VERY MUCH! I'm looking forward to try your resolution tomorrow!

"Jay Freedman" skrev:

> >How can I insert a date as a default in a textbox in a userform? It is
> >important, that the date is shown in a certain format (dd.mm.yyyy). The user
[quoted text clipped - 51 lines]
> Jay Freedman
> Microsoft Word MVP         FAQ: http://word.mvps.org
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.