Microsoft KB Archive/253785

= PRB: Error When Setting DateTimePicker Control Programmatically =

Article ID: 253785

Article Last Modified on 8/27/2002

-

APPLIES TO


 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q253785



SYMPTOMS
When attempting to set the date of a DateTimePicker control programmatically, the following error message might appear:

OLE IDispatch exception code 0 from DTPicker: Invalid Property Value.



CAUSE
When using the DateTimePicker control's Calendar interface to change the date, the control handles adjusting the Day, Month, and Year properties to create a valid date. If a value is assigned that creates an invalid date, the error message above is displayed.



RESOLUTION
Use the GOMONTH function to return a valid date, which can then be used to set the Day, Month, and Year properties of the DateTimePicker control as follows: olddate=exampledate exampledate=exampledate+(number_of_days_to_change) IF MONTH(exampledate)<>MONTH(olddate) olddate=GOMONTH(olddate,-1) THISFORM.DTPicker1.DAY=DAY(olddate) THISFORM.DTPicker1.MONTH=MONTH(olddate) THISFORM.DTPicker1.YEAR=YEAR(olddate) ENDIF



STATUS
This behavior is by design.



Steps to Reproduce Behavior
  Create a program file named DTP_DEMO using the following code: PUBLIC OX OX=CREATEOBJECT('MYFORM') OX.SHOW READ EVENTS

DEFINE CLASS myform AS FORM CAPTION = "DateTimePicker Form" HEIGHT = 150 LEFT = 0 TOP = 0 WIDTH = 230 NAME = "myform"

ADD OBJECT DTPicker1 AS OLECONTROL WITH ; OLECLASS="MSComCtl2.DTPicker.2",; TOP = 5, ; LEFT = 5, ; HEIGHT = 25, ; WIDTH = 100, ; NAME = "DTPicker1"

ADD OBJECT commandbutton1 AS COMMANDBUTTON WITH ; TOP=45, ; LEFT=115, ; HEIGHT=25, ; AUTOSIZE=.T., ; CAPTION="Add One Day", ; NAME="commandbutton1"

ADD OBJECT commandbutton2 AS COMMANDBUTTON WITH ; TOP=75, ; LEFT=115, ; HEIGHT=25, ; AUTOSIZE=.T., ; CAPTION="Subtract One Day", ; NAME="commandbutton2"

ADD OBJECT commandbutton3 AS COMMANDBUTTON WITH ; TOP=125, ; LEFT=125, ; HEIGHT=25, ; CAPTION="\MONTH(olddate)
 * !*         olddate=GOMONTH(olddate,1)
 * !*         THISFORM.DTPicker1.DAY=DAY(olddate)
 * !*         THISFORM.DTPicker1.MONTH=MONTH(olddate)
 * !*         THISFORM.DTPicker1.YEAR=YEAR(olddate)
 * !*      ENDIF

PROCEDURE commandbutton2.CLICK olddate=exampledate exampledate=exampledate-1 THISFORM.DTPicker1.DAY=DAY(exampledate) THISFORM.DTPicker1.MONTH=MONTH(exampledate) THISFORM.DTPicker1.YEAR=YEAR(exampledate) ENDPROC
 * !*      IF MONTH(exampledate)<>MONTH(olddate)
 * !*         olddate=GOMONTH(olddate,-1)
 * !*         THISFORM.DTPicker1.DAY=DAY(olddate)
 * !*         THISFORM.DTPicker1.MONTH=MONTH(olddate)
 * !*         THISFORM.DTPicker1.YEAR=YEAR(olddate)
 * !*      ENDIF

PROCEDURE commandbutton3.CLICK RELEASE THISFORM ENDPROC

PROCEDURE UNLOAD CLEAR EVENTS ENDPROC

ENDDEFINE  Save and run DTP_DEMO. Click the Command button captioned Subtract One Day, and note the error message that appears. Uncomment the commented lines of code in the commandbutton1.click and commandbutton2.click methods. Save and run DTP_DEMO.</li> Click the Command button captioned Subtract One Day, and note that the error message does not appear.</li></ol>

(c) Microsoft Corporation 2000, All Rights Reserved. Contributions by John Desch, Microsoft Corporation.

<div class="references_section">