Logo 
Search:

MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

The amazing and convoluted date formats

  Asked By: Qadriyah    Date: Feb 28    Category: MS Office    Views: 650
  

If you put these statements in a macro (I'm using an Excl project) .
dateInitDate = Date$
datInitDate.Value = DateAdd("d", 0, Date$)
datComplDate.Value = DateAdd("d", 90, Date$)

and then F8 to step thru the macro and mouse over any values you've stepped
past (to display their values at that time)...I cannot get the 1/9/2005 and
01-09-2005 to resolve to one format or the other:

UNTIL I PUT THEM IN A DIM STATEMENT as Dates.

But now these two subroutines don't update fields correctly (each triggered
by exiting one of the textboxes). Add 2 textboxes with names datInitDate and
datComplDat and see how changing values in a field is affected by exiting
the field--I get an error (they used to update):

Private Sub datInitDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim Dt, Dt2 As Date
On Error Resume Next
Dt = datInitDate
If Dt > DateSerial(1950, 1, 1) Then
' if properly formatted, puts date in Init Date (now the format here fails)
datInitDate = Format$(Dt, "mm-dd-yyyy")
' and 90 days later in Compl Date
Dt2 = DateAdd("d", 90, datInitDate)
datComplDate= Format$(Dt2, "mm-dd-yyyy")
Else
datInitDate.Text = ""
Beep
End If
End Sub

Private Sub datComplDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim Dt2 As Date
On Error Resume Next
Dt2 = DateValue(datComplDate)
If Dt2 > DateSerial(1950, 1, 1) Then
datComplDate = Format$(Dt2, "mm-dd-yyyy")
Else
datComplDate = ""
Beep
End If
End Sub

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Alyssa Kelley     Answered On: Feb 28

In the line...
> Dim Dt, Dt2 As Date

... Dt and Dt2 are *not* both dates. Dt will be a variant type and Dt2
will be a specific date  type. You have to specify a
type for *each* item seperated by a comma or it defaults.

That's one of the reasons that a lot of programmers write each
definition on a different line... to be sure and clear about what's
what.

Dim Dt as Date, Dt2 as Date
... is the same as ...
Dim Dt as date
Dim Dt2 as date

Dim Dt, Dt2 as Date
... is the same as
Dim Dt as Variant
Dim Dt2 as date

 
Didn't find what you were looking for? Find more on The amazing and convoluted date formats Or get search suggestion and latest updates.




Tagged: