473,327 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

SQL and Date

102 64KB
Background: Front End-MS Access 2010/VBA;Back End-MS SQL Server 2008

Here is the scenario: All table entries have a date field rr_date and is populated when the entry is first written. This date is the current date plus 7 years. It is the date when the table entry is purge.

The rr_date is defined to SQL as:
rr_date datetime

Here is the code that does the write:

Expand|Select|Wrap|Line Numbers
  1. 'set error handler
  2. On Error GoTo ErrorHandler
  3.  
  4. 'set current date plus seven years
  5. Dim datex As Date
  6. datex = DateAdd("yyyy", 7, Date)
  7.  
  8.  
  9. Dim rs As DAO.Recordset
  10. Dim db As Database
  11. Dim strSQL As String
  12.  
  13. Set db = CurrentDb
  14. Set rs = db.OpenRecordset("dbo_SignIn", dbOpenDynaset, dbSeeChanges)
  15.  
  16.  
  17. With rs
  18.  
  19.   .AddNew
  20.      .Fields("FName") = "John"
  21.      .Fields("LName") = "Doe"
  22.      .Fields("rr_date") = datex
  23.   .Update
  24. End With
  25.  
  26. MsgBox "Success- Your record was added to table dbo_SignIn.", vbInformation, gblDB
  27.  
  28. rs.Close
  29. db.Close
  30.  
  31. Set rs = Nothing
  32. Set db = Nothing
  33.  
When the purge program runs it only finds one record to purge when there are four records to purge. What am I doing wrong?
Here the code that purges:
Expand|Select|Wrap|Line Numbers
  1. 'set message record retention puge is starting'
  2. Me.frmMsg.Value = Now() & " Starting Record Retention purge ..."
  3. Forms![dboSignIn].Form.Refresh
  4.  
  5.  
  6. ' See how many records need to be purged
  7. Dim rs As DAO.Recordset
  8. Dim db As Database
  9. Dim strSQL As String
  10.  
  11. Set db = CurrentDb
  12.  
  13. strSQL = "Select * FROM dbo_SignIN WHERE rr_date < date()" ';"
  14. 'Debug.Print strSQL
  15.  
  16. Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
  17.  
  18. Me.frmMsg.Value = Me.frmMsg.Value & vbNewLine & Now() & " Records to be purged = " & rs.RecordCount & "."
  19. Forms![dboSignIn].Form.Refresh
  20.  
Mar 23 '15 #1
1 1085
Rabbit
12,516 Expert Mod 8TB
First, your code doesn't purge anything. It only gives a count.

Second, record set counts are inaccurate unless you first tell it to move to the end of the record set.

Third, if you're just using the recordset for a count, then run just a count query, there's no need to return all the data. Then you can just return the count field instead of moving the record set to the end before returning a record count.
Mar 23 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
2
by: Tjerk | last post by:
Hello all, I have the script below to change an image depending on the date upto january it worked fine but then it just stopped working does anybody have an idea how I can make it work again or...
9
by: Thomas R. Hummel | last post by:
Hello, I am importing data that lists rates for particular coverages for a particular period of time. Unfortunately, the data source isn't very clean. I've come up with some rules that I think...
30
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
3
by: captain | last post by:
Below is the sql for data with same date need to extract + or - 5 days data of same date also. How to also get data of + and - days related to same date. SELECT IM.Area, IM.Location,...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
3
by: JJ | last post by:
Here's the code. $link="http://xbox360cheat.org"; $close_date=$_POST; #last content change check if ($close_date == 0) $close_date = date("Y-m-d H:m:s", mktime(12, 0, 0, date("m"), date...
9
lilp32
by: lilp32 | last post by:
I am relatively new to Access (using 2010) and this seems like it should be simple yet I can't seem to figure it out. I have aggregate count data from 30+ counting devices (101, 102, etc) which...
0
Guido Geurs
by: Guido Geurs | last post by:
These are some examples how to format Date and Time data. Attached are doc's and other example, explaining how to work with Date and Time data. Option Explicit Private Sub Form_Load() Dim...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.