Should be Visible for 180 days

B

Bob Vance

My controls should ve Visible for 180 days but after excatly 30 days they
are greyed out
bModify = True

Dim dDate As Date
Dim dtDiff
Dim nCountDaysOfYear As Long
Dim nLeapYearNow As Long, nLeapYearOfBillYear As Long
Dim nTotalDays As Long
dDate = Form_frmModifyInvoiceClient.lstModify.Column(1)

dtDiff = DateDiff("d", Format(dDate, "mm/dd/yyyy"),
Format(Now(), "mm/dd/yyyy"))

nLeapYearOfBillYear = DatePart("yyyy", Format(dDate,
"dd/mm/yyyy"))
nLeapYearOfBillYear = nLeapYearOfBillYear Mod 4

nLeapYearNow = DatePart("yyyy", Format(Now(), "dd/mm/yyyy"))
nLeapYearNow = nLeapYearNow Mod 4

If nLeapYearNow = 0 Or nLeapYearOfBillYear = 0 Then
nTotalDays = 180
Else
nTotalDays = 180
End If
If dtDiff > nTotalDays Then
cmdClose.SetFocus
subLockControls False, True
bLockFlag = True
Else
subLockControls True, False
End If
End If

End Sub
 
C

Clif McIrvin

Have you verified that
dDate = Form_frmModifyInvoiceClient.lstModify.Column(1)

dtDiff = DateDiff("d", Format(dDate, "mm/dd/yyyy"),
Format(Now(), "mm/dd/yyyy"))
*always* returns the expected value for dtDiff ? What about dDate?

Just a curiosity question ... why are you using Format in the DateDiff
funcion? Seeing as both your values are defined as type date it seems to
me that you are requiring four un-necessary type conversions in your
statement as coded.
 
J

John W. Vinson

My controls should ve Visible for 180 days but after excatly 30 days they
are greyed out
bModify = True

Dim dDate As Date
Dim dtDiff
Dim nCountDaysOfYear As Long
Dim nLeapYearNow As Long, nLeapYearOfBillYear As Long
Dim nTotalDays As Long
dDate = Form_frmModifyInvoiceClient.lstModify.Column(1)

dtDiff = DateDiff("d", Format(dDate, "mm/dd/yyyy"),
Format(Now(), "mm/dd/yyyy"))

nLeapYearOfBillYear = DatePart("yyyy", Format(dDate,
"dd/mm/yyyy"))
nLeapYearOfBillYear = nLeapYearOfBillYear Mod 4

nLeapYearNow = DatePart("yyyy", Format(Now(), "dd/mm/yyyy"))
nLeapYearNow = nLeapYearNow Mod 4

If nLeapYearNow = 0 Or nLeapYearOfBillYear = 0 Then
nTotalDays = 180
Else
nTotalDays = 180
End If
If dtDiff > nTotalDays Then
cmdClose.SetFocus
subLockControls False, True
bLockFlag = True
Else
subLockControls True, False
End If
End If

End Sub

One problem is that the DateDiff() function accepts *date/time* arguments;
you're giving it String arguments, by unnecessarily using the Format()
function. Another issue is that you're calculating nLeapYearOfBillYear and
then using it to do ABSOLUTELY NOTHING - setting nTotalDays to 180 if it's
zero and to 180 if it's not! DateDiff already takes leap years into account,
so it's not necessary to do this calculation at all.

Try setting dtDiff to DateDiff("d", dDate, Date()) and comparing that value to
180. You might not need any code at all, just a simple expression in a textbox
control source.
 
D

Dirk Goldgar

Bob Vance said:
My controls should ve Visible for 180 days but after excatly 30 days they
are greyed out
bModify = True

Dim dDate As Date
Dim dtDiff
Dim nCountDaysOfYear As Long
Dim nLeapYearNow As Long, nLeapYearOfBillYear As Long
Dim nTotalDays As Long
dDate = Form_frmModifyInvoiceClient.lstModify.Column(1)

dtDiff = DateDiff("d", Format(dDate, "mm/dd/yyyy"),
Format(Now(), "mm/dd/yyyy"))

nLeapYearOfBillYear = DatePart("yyyy", Format(dDate,
"dd/mm/yyyy"))
nLeapYearOfBillYear = nLeapYearOfBillYear Mod 4

nLeapYearNow = DatePart("yyyy", Format(Now(), "dd/mm/yyyy"))
nLeapYearNow = nLeapYearNow Mod 4

If nLeapYearNow = 0 Or nLeapYearOfBillYear = 0 Then
nTotalDays = 180
Else
nTotalDays = 180
End If
If dtDiff > nTotalDays Then
cmdClose.SetFocus
subLockControls False, True
bLockFlag = True
Else
subLockControls True, False
End If
End If

End Sub


What's with all this formatting of the dates that you're doing? Neither
DatePart() nor DateDiff() wants a formatted string as an argument; they
want date/time values. If you pass a string, it's going to have to be
converted to a date, and you are using "mm/dd/yyyy" format in one place and
"dd/mm/yyyy" format in other places.

Here's a corrected version, which may or may not give you what you want:

'------ start of corrected code ------

Dim dDate As Date
Dim nDaysDiff As Long
Dim nCountDaysOfYear As Long
Dim nLeapYearNow As Long, nLeapYearOfBillYear As Long
Dim nTotalDays As Long

dDate = Form_frmModifyInvoiceClient.lstModify.Column(1)

nDaysDiff = DateDiff("d", dDate, Date())

' *** NOTE: I"M NOT SURE WHAT YOU'RE TRYING TO
' DO WITH THE LEAP YEARS HERE. YOU'RE GIVING
' THE SAME VALUE TO nTotalDays IN BOTH CASES.
' YOUR FORMULA FOR DETERMINING LEAP YEAR
' WAS NOT CORRECT, SO I CHANGED IT.

If Day(DateSerial(Year(dDate), 2, 29)) = 29 _
Or Day(DateSerial(Year(Date()), 2, 29)) = 29 _
Then
nTotalDays = 180
Else
nTotalDays = 180
End If

If nDaysDiff > nTotalDays Then
cmdClose.SetFocus
subLockControls False, True
bLockFlag = True
Else
subLockControls True, False
End If
End If

'------ end of corrected code ------
 
B

Bob Vance

Sorry Guys I didn't code it , Thanks Dirk I suspect it was the dd mmm yyyy -
mmm dd yyy that was messing it up, its going now so I suppose I wont change
the extravagant code Regards Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top