473,385 Members | 1,597 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,385 software developers and data experts.

datediff() using fields from different records

30
I am trying to calculate the session time for logon and logoff datetime fields, but the fields are in different records and different columns. Is this possible with a subquery? Here's a sample of the non-sequential fields and no sequencing field:

User ID LogonhostDate LogoffhostDate
test1 228 9/4/2006 8:52:38 PM
test1 229 9/4/2006 9:02:14 PM
test1 230 9/4/2006 9:06:59 PM
test1 231 9/4/2006 11:56:01 PM
test1 232 9/5/2006 12:01:56 AM
test1 233 9/5/2006 12:46:06 AM

I am not sure how to, or even if it;s necessary, to get the datetime fields in the same record. Any help is much appreciated.
tdb
Oct 26 '06
59 7706
tdb
30
Is there a way to automatically update the table when the DoCmd.RunSQL part of the function runs?
Nov 19 '06 #51
NeoPa
32,556 Expert Mod 16PB
Is there a way to automatically update the table when the DoCmd.RunSQL part of the function runs?
I'm not sure what you're asking, as DoCmd.RunSQL does update the table.
Nov 19 '06 #52
tdb
30
I'm not sure what you're asking, as DoCmd.RunSQL does update the table.
When I run the build sessions function, for each record the user has to click OK on a popup window to append the table. This is for over 1000 records.

Is there a way to skip this step and just let the function run?
Nov 20 '06 #53
MMcCarthy
14,534 Expert Mod 8TB
When I run the build sessions function, for each record the user has to click OK on a popup window to append the table. This is for over 1000 records.

Is there a way to skip this step and just let the function run?
You just need to add a line at the beginning of the code to turn the warnings off and a line at the end to turn them back on as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Function buildSessions()
  3.  
  4. Dim db As Database
  5. Dim rs1 As DAO.Recordset
  6. Dim rs2 As DAO.Recordset
  7. Dim strRS As String
  8. Dim strSQL As String
  9. Dim slogoff As Date
  10. Set db = CurrentDb
  11.  
  12.   DoCmd.SetWarnings False 
  13.  
  14.   strRS = "SELECT [Computerinfo].[User], [Computerinfo].[ID], [Computerinfo].[LogonhostDate], [Computerinfo].[LogoffhostDate]
  15. FROM Computerinfo ORDER BY [Computerinfo].[LogonhostDate];"
  16.   Set rs1 = db.OpenRecordset(strRS)
  17.  
  18.   strSQL = "SELECT [Computerinfo].[User], [Computerinfo].[ID], [Computerinfo].[LogonhostDate], [Computerinfo].[LogoffhostDate]
  19. FROM Computerinfo ORDER BY [Computerinfo].[LogoffhostDate];"
  20.   Set rs2 = db.OpenRecordset(strSQL)
  21.  
  22.   rs1.MoveFirst
  23.   Do Until rs1.EOF
  24. If Not IsNull(rs1!LogonhostDate) Then
  25.   rs2.MoveFirst
  26.   Do Until rs2.EOF
  27. If rs2!User = rs1!User And rs2!LogoffhostDate > rs1!LogonhostDate Then
  28.   slogoff = rs2!LogoffhostDate
  29.   rs2.MoveLast
  30. End If
  31. If Not rs2.EOF Then
  32.   rs2.MoveNext
  33. End If
  34.   Loop
  35.   DoCmd.RunSQL "INSERT INTO tblSessions (user, logonTime, logoffTime) " & _
  36.   "VALUES ('" & rs1!User & "',#" & rs1!LogonhostDate & "#,#" & slogoff & "#);"
  37. End If
  38. rs1.MoveNext
  39.   Loop
  40.  
  41.   rs1.Close
  42.   rs2.Close
  43.   Set rs1 = Nothing
  44.   Set rs2 = Nothing
  45.   Set db = Nothing
  46.  
  47.   DoCmd.SetWarnings True
  48.  
  49. End Function
  50.  
Nov 20 '06 #54
tdb
30
Works great! This forum is a valuable resource. Thank you all for your help!
Nov 20 '06 #55
tdb
30
Something I just noticed is that the table appends to the end every time I run the function so that there are duplicate sets of sessions - is there a way to first remove all the records in the table, then build it again when the function is run?
Nov 20 '06 #56
NeoPa
32,556 Expert Mod 16PB
Use it all the time :
Expand|Select|Wrap|Line Numbers
  1. DELETE *
  2. FROM [YourTable]
Often with a WHERE clause too.
Nov 20 '06 #57
MMcCarthy
14,534 Expert Mod 8TB
Use it all the time :
Expand|Select|Wrap|Line Numbers
  1. DELETE *
  2. FROM [YourTable]
Often with a WHERE clause too.
As this is an action query it can be run with DoCmd.RunSQL
Nov 20 '06 #58
tdb
30
As this is an action query it can be run with DoCmd.RunSQL
I have tried putting this into the function in various places, tried changing the * to the [table].[field] for all fields, and tried putting it in another DoCmd.RunSQL statement. Nothing seems to work. Is there a specific place to put this statement in a function?
Nov 22 '06 #59
MMcCarthy
14,534 Expert Mod 8TB
I have tried putting this into the function in various places, tried changing the * to the [table].[field] for all fields, and tried putting it in another DoCmd.RunSQL statement. Nothing seems to work. Is there a specific place to put this statement in a function?
Ok, just under DoCmd.SetWarnings False

DoCmd.RunSQL "DELETE * FROM tblSessions;"
Nov 22 '06 #60

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

Similar topics

4
by: CJM | last post by:
I have an ASP page that lists files and folders in a directory. I'm using a cookie to record the last time this page was visited, and I intend to show links that are created/modified from that date...
4
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS...
1
by: PMBragg | last post by:
ORINGINAL Post >Thank you in advance. I'm trying to pull all inventory items from December >of the previous year back to 4 years for my accountant. I know this can be >done, but I'm drawing a...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
1
by: info | last post by:
Can some one show me how to use the datediff function where the dates are being supplied through an access db. For instance, a recordset would contain these fields: DateAssigned, DateDue,...
5
by: mcbill20 | last post by:
Hello all. I have a really basic question that I hope someone has a better answer for. I apologize in advance-- I know this is probably a really basic question but I am used to Oracle rathern than...
2
by: stephenmcnutt | last post by:
I'm trying to do something that should be trivial. I'm a teacher at an elementary school, and I'm setting up an ASP form page for teachers to vote each afternoon on which dismissal line behaved...
6
by: Wheeler2008 | last post by:
Hi All, I am currently running a query on a MS Access table, in which I want to be able to compare and total the difference in dates between records. I only have one date field within the table. ...
9
geraldinegrieve
by: geraldinegrieve | last post by:
I have a table in access that holds data on vehicle there are 3 fields holding dates on MOT, Tax and Insurance renewal I am looking for 3 different messages on opening if date of any is within next 2...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
Oralloy
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 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.