Question Locked

DateAdd function

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

preparing a smallish (but vital) database from scratch - and that's my knowledge as well as the database! someone is likely going to tell me this is blindingly obvious but I'm wading through Access 97 Help to no avail. anyone who can help, please read on...

I have a form where I load in a lot of details of a given transaction but I need to add 3 years to its date of completion in order to identify, for example, due date for disposal/archiving of the relevant documents.

I can manage to make it count 3 years from now (i.e. date of entry of records) but I have 4 year's backlog of data which needs to be loaded in and I can't make it add 3 years to these records - so everything back to 2003 has a disposal date of 2010 (which won't do at all).

any advice gratefully received...thanks!

5 replies

by tony hopkinson

update mytable set datedue = [dateadd]('yyyy',3,[datecomplete]) where [datedue] >= ???

or something like anyway.

There's an option called SQL view under query, where you can enter raw sql or see see the sql of something knocked up in the GUI.

HTHs

by maz

Here's a generic function to do it. You can extend it pretty easily if desired.

'====================================================
' LICENSE/COPYRIGHT - DO NOT REMOVE THESE COMMENTS!
'====================================================
' *
' * AddDate Module
' * Copyright (C) 2007 John D. Mazza III.
' *
' * Written by John Mazza
' *
' * This library is free software; you can redistribute it and/or
' * modify it under the terms of the GNU Lesser General Public
' * License Version 2.1 as published by the Free Software Foundation.
' *
' * This library is distributed in the hope that it will be useful,
' * but WITHOUT ANY WARRANTY; without even the implied warranty of
' * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
' * Lesser General Public License for more details.
' *
' * You should have received a copy of the GNU Lesser General Public
' * License along with this library; if not, write to the Free Software
' * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
' ****************************************************************************
'
'
' PURPOSE
' Contains generic function to add years to a date in an
' arbitrary table/field combination
'
' LANGUAGE
' Visual Basic 6.0 or VBA6
'
' INSTALLATION
' Copy this module to a new module in your MS Access project

' NOTE - make sure to add a reference in Tools/References to
' Microsoft DAO 3.5 or 3.6
'

' AddYears() SUB
' Use to add a number of years to dates in a specified table
'
' PARAM: Years - Number of years to add
' TableName - Name of the table
' OrgFld - Name of field holding original date value
' DstFld - Name of field holding destination date (may be same as OrgFld)
' IDFld - Name of field used as a key column. Must be INT type and unique
'
' WARNING: Does not verify that any of the parameters are valid for your
' database. MAKE SURE THEY ARE CORRECT BEFORE CALLING THIS FUNCTION
' OR DAMAGE TO YOUR DATA MAY OCCUR!

Public Sub AddYears(ByRef Years As Variant, ByRef TableName As Variant, _
ByRef OrgFld As Variant, ByRef DstFld As Variant, _
ByRef IDFld As Variant)
Dim db1 As Database
Dim rs1 As Recordset

Set db1 = CurrentDb

' Fail silently on any errors
On Error Resume Next

CheckParmsOK = False

If (IsNull(Years) Or IsEmpty(Years)) Then
CheckParmsOK = False
Else
CheckParmsOK = True
YrsAdd = Val(Years)
End If

If (IsNull(TableName) Or IsEmpty(TableName) Or Trim(TableName) = "") Then
CheckParmsOK = False
End If

If (IsNull(OrgFld) Or IsEmpty(OrgFld) Or Trim(OrgFld) = "") Then
CheckParmsOK = False
End If

If (IsNull(DstFld) Or IsEmpty(DstFld) Or Trim(DstFld) = "") Then
CheckParmsOK = False
End If

If (IsNull(IDFld) Or IsEmpty(IDFld) Or Trim(IDFld) = "") Then
CheckParmsOK = False
End If

If CheckParmsOK Then

Qry = "SELECT " & IDFld & "," & OrgFld & " from " & TableName & ";"

Debug.Print ("AddYears: " & Qry)

Set rs1 = db1.OpenRecordset(Qry, dbOpenSnapshot)

While Not rs1.EOF
OrgDate = rs1(OrgFld)
RecNo = rs1(IDFld)

If Not IsNull(OrdDate) Then
DtDue = DateAdd("yyyy", YrsAdd, OrgDate)
UpQry = "UPDATE " & TableName & " SET " & DstFld & " = #" & DtDue & "# " & _
"WHERE " & IDFld & " = " & Str(RecNo) & ";"

Debug.Print ("AddYears: " & UpQry)

db1.Execute (UpQry)
End If

rs1.MoveNext
Wend

rs1.Close
End If
End Sub

by tony hopkinson

Wouldn't want to run it too many times.
Where in here does it deal with IDFld not being unique?
On Error Resume Next in a database update routine.....
Not a good idea at all.
I'd rather the run time bleat noisily than my data get corrupted silently.

by midniteone

Hi

grabbed the 'short' fix and managed to mangle it into my database so I'm keeping the boss happy as he can see a working model and we can get some data loaded

fascinated by the long version and have promised myself I'll work through it once we get through the horrors of 'end of year' and closing down the accounts

thanks again guys - that was driving me just a little crazy...

This discussion is closed to new replies.