Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Retrieving day of the week from date picker

151 views
Skip to first unread message

david

unread,
Aug 14, 2005, 9:43:02 PM8/14/05
to
Is there a way to retrieve the day of the week (eg. 'Wednesday', 'Friday',
etc) from a date picker control box?

If the date selected is the 15th of August, 2005, and the day is Monday, I'd
like to be able to retrieve 'Monday' and display it in an expression box.

Is there an easy way to do this?

Thanks

Scott L. Heim [MSFT]

unread,
Aug 15, 2005, 9:32:30 AM8/15/05
to
Hi David,

There is not a way to do this without writing some custom script; however,
if you don't mind doing so here are sample steps:

** NOTE: I would encourage you to complete these steps completely then
migrate this to your application.

1) Create a new, blank InfoPath solution
2) From the Tools menu choose Form Options, select the Advanced tab and
make sure the Programming Language is set to VBScript.
3) Add a Date Picker control to your form (named field1) and a text box
control (named field2)
- Right-click on the Date Picker control and choose Properties
- Click the Data Validation button
- From the Events box choose OnAfterChange and then click Edit - you should
see the following:

Sub msoxd_my_field1_OnAfterChange(eventObj)

' Write code here to restore the global state.

If eventObj.IsUndoRedo Then
' An undo or redo operation has occurred and the DOM is read-only.
Exit Sub
End If

' A field change has occurred and the DOM is writable. Write code here to
respond to the changes.

End Sub

- Just before the "End Sub" line of code add the following:

If eventObj.Operation = "Insert" Then
Dim objField2
Set objField2 = XDocument.DOM.selectSingleNode("//my:myFields/my:field2")

Dim intDay
Dim strDay

intDay = DatePart("w", eventObj.Site.text)
Select Case intDay
case 1
strDay = "Sunday"
case 2
strDay = "Monday"
case 3
strDay = "Tuesday"
case 4
strDay = "Wednesday"
case 5
strDay = "Thursday"
case 6
strDay = "Friday"
case 7
strDay = "Saturday"
End Select
objField2.text = strDay
End If

- The entire code procedure should now look like this:

Sub msoxd_my_field1_OnAfterChange(eventObj)

' Write code here to restore the global state.

If eventObj.IsUndoRedo Then
' An undo or redo operation has occurred and the DOM is read-only.
Exit Sub
End If

' A field change has occurred and the DOM is writable. Write code here to
respond to the changes.
If eventObj.Operation = "Insert" Then
Dim objField2
Set objField2 = XDocument.DOM.selectSingleNode("//my:myFields/my:field2")

Dim intDay
Dim strDay

intDay = DatePart("w", eventObj.Site.text)
Select Case intDay
case 1
strDay = "Sunday"
case 2
strDay = "Monday"
case 3
strDay = "Tuesday"
case 4
strDay = "Wednesday"
case 5
strDay = "Thursday"
case 6
strDay = "Friday"
case 7
strDay = "Saturday"
End Select
objField2.text = strDay
End If
End Sub

- Save the code and close the code editor
- Preview the form and select a date from the Date Picker - the text box
should display the correct day!

I hope this helps! :-)

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

RDB

unread,
Apr 16, 2007, 10:28:02 AM4/16/07
to
I don't know if Scott is still around; hopefully someone can provide a little
guidance on this issue. The script Scott posted below works fine on a basic
form. However, if I create a form that submits to a database, I can't
validate calendar entries using the script below. I receive the following
error message when any date is selected in the date picker:

"A run-time error has occurred.
Do you want to debug?

The following error occurred:

Object required: 'objField2'
File:script.vbs
Line:56"

I've been able to duplicate this error in a completely new form as
well...and I'm able to get this script to work in a basic database where
there is no database connection. Any ideas?

Ol

unread,
Feb 27, 2009, 5:10:08 PM2/27/09
to
0 new messages