I'm using the date add function and for some reason I'm getting an error when I try to add a single year to a date but no error when I try to add more than a single year. can anyone figure out why this may be?
example: DateAdd("yyyy", 1, recSet7.Fields("DateofContract"))
23 4991
Hi Kosmos
this works for me
Result: 18/01/2008 18:12:11
What does the 'Date' argument part of your call evaluate to?
HTH
Steve
hmmm....well the code checks to see if the renewal date of a contract is older than or equal to the current date...if so it sets renewal date to the renewal date plus the number of years the contract will renew for. This field is called "TermOfContractInYears" I am running into a problem here whenever this field is equal to 1. The following is the pertinent code:
[PHP] recSet7.Open "tblContracts", con7, adOpenKeyset, adLockOptimistic
recSet7.MoveFirst
Do Until recSet7.EOF
If IsNull(recSet7.Fields("RenewalDate")) And recSet7.Fields("TermOfContractInYears") <> 0 And recSet7.Fields("ContinuousContract") = True Then
Do Until recSet7.Fields("RenewalDate") > Date
If recSet7.Fields("TermOfContractInYears") = 1 Then
recSet7.Fields("RenewalDate") = DateAdd("yyyy", 1, recSet7.Fields("DateofContract"))
recSet7.Fields("RenewalDate") = DateAdd("d", (-1), recSet7.Fields("RenewalDate"))
Else
recSet7.Fields("RenewalDate") = DateAdd("yyyy", recSet7.Fields("TermOfContractInYears"), recSet7.Fields("DateofContract"))
recSet7.Fields("RenewalDate") = DateAdd("d", (-1), recSet7.Fields("RenewalDate"))
End If
Loop
ElseIf Not IsNull(recSet7.Fields("RenewalDate")) And recSet7.Fields("TermOfContractInYears") <> 0 And recSet7.Fields("ContinuousContract") = True Then
If recSet7.Fields("RenewalDate") <= Date Then
Me.txtReference = recSet7.Fields("DatabaseReferenceNumber")
DoCmd.OpenQuery ("UpdateAddedToOutlook")
Do Until recSet7.Fields("RenewalDate") > Date
If recSet7.Fields("TermOfContractInYears") = 1 Then
recSet7.Fields("RenewalDate") = DateAdd("yyyy", 1, recSet7.Fields("DateofContract"))
recSet7.Fields("RenewalDate") = DateAdd("d", (-1), recSet7.Fields("RenewalDate"))
Else
recSet7.Fields("RenewalDate") = DateAdd("yyyy", recSet7.Fields("TermOfContractInYears"), recSet7.Fields("DateofContract"))
recSet7.Fields("RenewalDate") = DateAdd("d", (-1), recSet7.Fields("RenewalDate"))
End If
Loop
End If
End If
recSet7.MoveNext
Loop
recSet7.Close[/PHP]
Whenever TermOfContractInYears is greater than 1, it goes on to the other option and it works which is why I have narrowed down the problem to the number 1 but perhaps it is my code that is causing the problem?
Kosmos
What date is returned when you run - recSet7.Fields("DateofContract")
? and the contract term is one year?
I assume you have added in the 1 year contract check to isolate the bug? - If recSet7.Fields("TermOfContractInYears") = 1 Then
-
recSet7.Fields("RenewalDate") = DateAdd("yyyy", 1, recSet7.Fields("DateofContract"))
-
recSet7.Fields("RenewalDate") = DateAdd("d", (-1), recSet7.Fields("RenewalDate"))
-
Else
-
recSet7.Fields("RenewalDate") = DateAdd("yyyy", recSet7.Fields("TermOfContractInYears"), recSet7.Fields("DateofContract"))
-
recSet7.Fields("RenewalDate") = DateAdd("d", (-1), recSet7.Fields("RenewalDate"))
-
End If
Can you give an example of what the DateAdd function returns for a 1-year contract and show the Date of Contract?
Steve
9/7/2005 is the specific date for this contract. Yes I did the if statements to isolate the problem.
Yeah, but what date is the DateAdd function returning for 9/7/2005 ?
nothing it's freezing...sorry...meant to reply to that question as well
This works perfectly: - DateAdd("yyyy", 1,"09/07/2005"
Returns 09/07/2006
Howeever: - DateAdd("yyyy", 1,9/7/2005
Returns 30/12/1900 00:00:55
Maybe a matter of formatting the date before using DateAdd?
Steve
hmm well that would certainly make sense considering the loop would never end if it were going back in time. I'll mess around with it and let you know what I come up with. Thanks.
Good luck & keep the thread alive
Steve
Thanks...unfortunately that doesn't really work. The date will be different each time and so I have to pull it from a table in date format which will be 9/7/2005 and not 09/07/2005. This is confusing.
oh and I also tried it as a text box instead of a date box and did 09/07/2005 but this did not work either.
OK
You could try to preformat the date into a string. then use the string variable in the function.
It may be that the DateAdd function is misinterpreting the date - I have seen this happen with dates in both Access & SQL functions...
HTH
Steve
OK, last try!
Just add double-quotes round the date field, thus:- -
Dim tmp As String
-
tmp = recSet7.Fields("DateofContract")
-
DateAdd("yyyy", 1, tmp)
Works here!
Steve
Is Access switching the day and month around?
Steve
hmm well perhaps that would work...I found another solution, however, that brings me to believe that perhaps the problem lied in the fact that "DateOfContract" is a required field (not sure why this would cause a problem) but this works as well...
[PHP] recSet7.Open "tblContracts", con7, adOpenKeyset, adLockOptimistic
recSet7.MoveFirst
Do Until recSet7.EOF
If IsNull(recSet7.Fields("RenewalDate")) And recSet7.Fields("TermOfContractInYears") <> 0 And recSet7.Fields("ContinuousContract") = True Then
recSet7.Fields("RenewalDate") = recSet7.Fields("DateOfContract")
Do Until recSet7.Fields("RenewalDate") > Date
If recSet7.Fields("TermOfContractInYears") = 1 Then
recSet7.Fields("RenewalDate") = DateAdd("yyyy", 1, recSet7.Fields("RenewalDate"))
Else
recSet7.Fields("RenewalDate") = DateAdd("yyyy", recSet7.Fields("TermOfContractInYears"), recSet7.Fields("RenewalDate"))
End If
Loop
recSet7.Fields("RenewalDate") = DateAdd("d", (-1), recSet7.Fields("RenewalDate"))
ElseIf Not IsNull(recSet7.Fields("RenewalDate")) And recSet7.Fields("TermOfContractInYears") <> 0 And recSet7.Fields("ContinuousContract") = True Then
If recSet7.Fields("RenewalDate") <= Date Then
Me.txtReference = recSet7.Fields("DatabaseReferenceNumber")
DoCmd.OpenQuery ("UpdateAddedToOutlook")
Do Until recSet7.Fields("RenewalDate") > Date
If recSet7.Fields("TermOfContractInYears") = 1 Then
recSet7.Fields("RenewalDate") = DateAdd("yyyy", 1, recSet7.Fields("RenewalDate"))
Else
recSet7.Fields("RenewalDate") = DateAdd("yyyy", recSet7.Fields("TermOfContractInYears"), recSet7.Fields("RenewalDate"))
End If
Loop
End If
End If
recSet7.MoveNext
Loop
recSet7.Close[/PHP]
Thanks for all the help!
by the way I tried the tmp thing as well...this did not work..so perhaps the misinterpretation is because I am doing
recSet1.Fields("A") = DateAdd("yyyy", #, recSet1.Fields("B"))
maybe you can only do
recSet1.Fields("A") = DateAdd("yyyy", #, recSet1.Fields("A"))
??
Either should work, so long as a valid date is returned by DatAdd
Steve
This works perfectly: - DateAdd("yyyy", 1,"09/07/2005"
Returns 09/07/2006
However: - DateAdd("yyyy", 1,9/7/2005
Returns 30/12/1900 00:00:55
I expect that's because in the second case you're using a numeric expression which would evaluate to around .00064125. That is, 9 divided by 7 (about 1.3) divided by 2,005. This very small number is presumably interpreted, in a date/time context, as about 55 seconds after midnight on the "first" day.
That's why I suggested using a string...
Steve
That's why I suggested using a string...
Yeah, just thought I'd fill in a bit of background there.
People often think that things have some sort of magical status just because they resemble something else (such as a date). But in many cases the programming language will simply interpret it as a numeric expression, or a variable name or whatever.
Yes...but I was using a recSet and pulling my information from a date/time field in the form of a short date. But perhaps that's what happened in the conversion...perhaps Microsoft programmers forgot to keep it as a string :)
Yes...but I was using a recSet and pulling my information from a date/time field in the form of a short date. But perhaps that's what happened in the conversion...perhaps Microsoft programmers forgot to keep it as a string :)
Maybe. But I was just referring to the posted example, which used a literal. It wasn't much use an an example, as it had a numeric expression which looked like a date and thus merely confused everyone.
NeoPa 32,556
Expert Mod 16PB
Literal DateTimes and Their Delimiters (#).
( Literal DateTimes and Their Delimiters (#).)
A look through here should explain a lot of these misconceptions.
Important point for this thread - if it is a date field then it doesn't need to be formatted into a string and it doesn't matter what your date formats are either.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Mark |
last post by:
Hi - using asp and ms Access - this works ok:
set Products = Server.CreateObject("ADODB.Recordset")
Products.ActiveConnection = myConString
Products.Source = "SELECT Products.productid,...
|
by: Samuel |
last post by:
I have a user complaining about a random error (sporadic, cannot be reliably
reproduced):
=================================
Microsoft VBScript runtime error '800a01fb'
An exception occurred:...
|
by: Chris Fairall |
last post by:
Hi all
I am using Visual Studio 2003 Enterprise Architect, and for them most
part, everything has been working fine.
However, in the last few days, I have been experiencing a weird error,
and I...
|
by: Geraldine Hobley |
last post by:
Hello,
I'm having a problem adding a manifest to a vb project in order to add visual styles to my vb.net application. I followed the steps outlined her...
|
by: Dacuna |
last post by:
I have a dataset that I created programmatically and bind to a datagrid.
When I add a row and I .show the form I get an error "Error creating window
handle"
This only happens if I have the code...
|
by: ECathell |
last post by:
I am getting an unspecified error when adding a windows form to a project. New project, old project. Doesn't matter. Also happens for user control. All the message box says is Unspecified Error.
...
|
by: Codemonkey |
last post by:
Hi,
When I first installed Visual Studio 2003, I noticed I was getting the
follow error when showing a form:
A first chance exception of type 'System.ArithmeticException' occurred in...
|
by: =?Utf-8?B?QW5keSBZdQ==?= |
last post by:
Hi,
I'm trying to return exceptions from a WCF Service using FaultExceptions. I
got the service compiled and running. But I get an error while adding a
service reference to it. The error reads:
"...
|
by: Chris |
last post by:
I am getting a viewstate error when adding dynamically user controls.
SYSTEM_EXCEPTION:Failed to load viewstate.
The control tree into which viewstate is being loaded must match the control
tree...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |