简体   繁体   English

如何在VBA中编写此单元格引用的Excel公式以在MS Access中使用:(b2-a1)

[英]How can I write this cell-referenced Excel formula in VBA to use in MS Access: (b2-a1)

This is a very simple calculation to do in Excel but I am stumped on how to do this within Access using 2 fields in a table in different records. 这是在Excel中执行的非常简单的计算,但是我对如何使用不同记录的表中的2个字段在Access中执行此操作感到困惑。 One application I have needs to find the difference between an operation STOPTime and the next STARTTime. 我需要一个应用程序来查找操作STOPTime和下一个STARTTime之间的差异。 Any help will be greatly appreciated. 任何帮助将不胜感激。 I am new to VBA. 我是VBA的新手。

Sub samPle()
Dim startTime As Date
Dim endTime As Date

startTime = Time
'''Your Code


endTime = Time

Debug.Print DateDiff("n", startTime, endTime) ' difference in minutes

End Sub

There are a number of ways to approach the problem depending in your data, which makes posting data for questions about SQL very important. 有多种方法可以根据您的数据来解决问题,这使得发布有关SQL问题的数据非常重要。 For example: 例如:

Table: 表:

ID  EventTime           EventType
1   17/02/2013 08:52:00 start
2   17/02/2013 09:52:00 stop
3   17/02/2013 11:52:00 start
4   17/02/2013 15:52:00 stop

SQL: SQL:

SELECT a.id,
       a.eventtime                    AS Start,
       a.eventtype,
       (SELECT TOP 1 eventtime
        FROM   times b
        WHERE  eventtype = "stop"
               AND eventtime > a.eventtime
        ORDER  BY eventtime,
                  id)                 AS Stop,
       Datediff("n", [start], [stop]) AS RunTime
FROM   times AS a
WHERE  (( ( a.eventtype ) = "start" ))
ORDER  BY a.eventtime;

Result: 结果:

ID  Start               EventType   Stop                RunTime
1   17/02/2013 08:52:00 start       17/02/2013 09:52:00     60
3   17/02/2013 11:52:00 start       17/02/2013 15:52:00     240

Re comment 重新评论

SELECT a.id, 
       a.eventtime                         AS Start, 
       a.eventtype, 
       (SELECT TOP 1 eventtime 
        FROM   times b 
        WHERE  eventtype = "stop" 
               AND eventtime > a.eventtime 
        ORDER  BY eventtime, 
                  id)                      AS Stop, 
       Datediff("n", [start], [stop])      AS RunTime, 
       (SELECT TOP 1 eventtime 
        FROM   times b 
        WHERE  eventtype = "start" 
               AND eventtime > a.eventtime 
        ORDER  BY eventtime, 
                  id)                      AS NextStart, 
       Datediff("n", [start], [nextstart]) AS StartDiff, 
       Datediff("n", [stop], [nextstart])  AS DownTime 
FROM   times AS a 
WHERE  (( ( a.eventtype ) = "start" )) 
ORDER  BY a.eventtime; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM