Solved

add days to a date auto

Posted on 2002-04-18
10
145 Views
Last Modified: 2010-05-02
hi
I need to say have a date, how would i say add 10 days or 20 days to the date eg

my date is 15/01/2002
i want to add 20 days to it,
i need the new date

i need this functon to prompt a process after a certain time a record was captured by using the date the record was captured


thanx

Craig
0
Comment
Question by:CraigLazar
  • 2
  • 2
  • 2
  • +3
10 Comments
 

Expert Comment

by:lschuele
ID: 6951178
If you have a variable that is declared as a date, it is stored as a number.  To add 10 days to a date, simply add 10 to the variable.  Hours are stored as the decimal portion of the number, so to add 12 hours, add .5 to the date variable.

Run the following code as an example.

   Dim dTheDate As Date
   
   dTheDate = #1/1/2002#
   
   Debug.Print dTheDate
   
   dTheDate = dTheDate + 20
   
   Debug.Print dTheDate
0
 
LVL 4

Author Comment

by:CraigLazar
ID: 6951199
hi ischuele
thanx for the quick respponse

this code, will it work out the correct date say from a date that is at the end of the month to the new month
say from 25 of February to March ?

thanx
0
 
LVL 7

Accepted Solution

by:
Z_Beeblebrox earned 50 total points
ID: 6951218
It is better practice to use the DateAdd method.

NewData = DateAdd("d",20,OldDate)

Zaphod.
0
 
LVL 18

Expert Comment

by:deighton
ID: 6951227
yes
0
 

Expert Comment

by:lschuele
ID: 6951381
Use the DateAdd function to add days, months, years, and several other options.

And Beeble is also correct that it is better practice to use the function.  dTheDate = dTheDate + 20 uses an implicit conversion from integer to date.  Implicit conversions are bad practice, as occasionaly you will get unexpected results and type mismatch errors.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:nutwiss
ID: 6951929
err...dateadd()...oh...someone already said that...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6952387
lschuele,

>> And Beeble is also correct that it is better practice to use the function.  dTheDate = dTheDate + 20
uses an implicit conversion from integer to date.  Implicit conversions are bad practice, as occasionaly
you will get unexpected results and type mismatch errors. <<

Not sure if you are aware, but a date varaible is in fact a double.  The whole (integer) part is the number of days,
the fractional part is the time.  So it is no different from doing:
Dim x as Double
Dim y as Double

y = x + 20

Sure you can wrap this addition in a class and call it DoubleAdd, but would you?

Just my 2 cents worth,
Anthony
0
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6952427
My 2 cents :)

From a good programming practice standpoint, there is nothing wrong with adding two doubles together, nor is there anything wrong with adding two dates together (although I can't image why anyone would want to). The problems arises when you try to add a double (or an integer in this case) to a date. It is bad practice to add variable of different types together because doing so safely requires knowledge of the underlying structure of the variables in question, something which not all readers of a given piece of code will have.

Hopefully I am not starting a whole debate on good programming, I just think that not everyone who sees code that adds 20 to a date will know what that means, whereas dateadd("d", 20, SomeDate) is very self explanatory.

Zaphod.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6952438
Then we will have to agree to disagree.

Anthony
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6953690
you do that................hmmmm.....handbags.....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Vlookup to move data back to source. 4 84
passing parameter in sql procedure 9 57
Hide vba in gp 7 83
Can we place a tooltip on the actual vb6 form 5 36
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now