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

Need to run code every minute, thinking I might have to use SQL Trigger?

49
Access 2010
SQL Sever 2005 express

Here is my code, works fine as is. But I need to run it once a minute, if not more often. From what I have found out by surfing the web I have one of three choices:

1) Use the timer in the forms property.
2) Use the PC scheduler and call access then autoexec.
3) Rewrite the code below into SQL and apply it to a trigger.

I do not like any of the above options because:

1) Forms timer is not that reliable from what I read, please tell me if I'm wrong.
2) PC scheduler opening and closing Access every minute doesn’t sound that great.
3) I have never written a trigger. And the SQL code requires comparing the previous record to the current record, sounds like my dog chasing its tail. I have seen code like this before, and it will be a stretch for me. Also lines 45-47, and 50-52 write data back out in a specific way to machines/PLC's on the floor. I am not sure how, or if, SQL can do this?


Expand|Select|Wrap|Line Numbers
  1. Private Sub ChkDownTimeCode_Click()
  2.  
  3. Dim RecordCount As Integer
  4. Dim CurrRecord As Integer
  5. Dim TagNamestr As String
  6. Dim TagValueint As Integer
  7. Dim StartProd As Integer
  8. Dim EndProd As Integer
  9. Dim SQLStg As String
  10. Dim dbs As DAO.Database
  11. Dim DownTimeNotZero As DAO.Recordset
  12.  
  13.  
  14. Set dbs = DBEngine.Workspaces(0).Databases(0)
  15.  
  16. SQLStg = "SELECT dbo_Link_Data.Asset_Number, dbo_Link_Data.DT_Cat_VALUE, " & _
  17.          "dbo_Link_Data.DT_Code_VALUE, dbo_Link_Data.Downtime_VALUE, " & _
  18.          "dbo_Link_Data.Produced_VALUE, dbo_Link_Data.QUALITY, dbo_Link_Data.TIMESTAMP1, " & _
  19.          "dbo_MachineTags.DT_Code, dbo_MachineTags.DT_Cat " & _
  20.          "FROM dbo_MachineTags INNER JOIN dbo_Link_Data ON dbo_MachineTags.AssetNumber = " & _
  21.          "dbo_Link_Data.Asset_Number " & _
  22.          "WHERE (((dbo_Link_Data.DT_Code_VALUE)<>0) AND ((Hour([TIMESTAMP1]))=Hour(Time())) " & _
  23.          "AND ((Minute([TIMESTAMP1])-Minute(Time()))<2)) " & _
  24.          "OR (((dbo_Link_Data.DT_Cat_VALUE)<>0) AND ((Hour([TIMESTAMP1]))=Hour(Time())) " & _
  25.          "AND ((Minute([TIMESTAMP1])-Minute(Time()))<2));"
  26.  
  27. Set DownTimeNotZero = dbs.OpenRecordset(SQLStg, dbOpenSnapshot)
  28.  
  29. RecordCount = DownTimeNotZero.RecordCount
  30. CurrRecord = 1
  31.  
  32. If RecordCount > 0 Then
  33.     While RecordCount <> CurrRecord
  34.         CurrMachine = DownTimeNotZero("Asset_Number")
  35.         StartProd = DownTimeNotZero("Produced_VALUE")
  36.         'While is moving to last record for machine to get end production
  37.         While RecordCount <> CurrRecord And CurrMachine = DownTimeNotZero("Asset_Number")
  38.             CurrRecord = CurrRecord + 1
  39.             DownTimeNotZero.MoveNext
  40.         Wend
  41.         EndProd = DownTimeNotZero("Produced_VALUE")
  42.         If (EndProd - StartProd) >= 3 Then  'Reset downtime codes to zero (0)
  43.             TagNamestr = DownTimeNotZero("DT_Cat")  'Machine Tag Name
  44.             TagValueint = 0
  45.             channel = Application.DDEInitiate("kepdde1", "_ddedata")
  46.             Application.DDEPoke channel, TagNamestr, TagValueint
  47.             Application.DDETerminate channel
  48.             TagNamestr = DownTimeNotZero("DT_Code") 'Machine Tag Name
  49.             TagValueint = 0
  50.             channel = Application.DDEInitiate("kepdde1", "_ddedata")
  51.             Application.DDEPoke channel, TagNamestr, TagValueint
  52.             Application.DDETerminate channel
  53.         End If
  54.         DownTimeNotZero.MoveNext
  55.     Wend
  56. End If
  57.  
  58. End Sub
  59.  
Basically I would like to know if the box I think I am in is correct and I will start to learn how to write a trigger/SQL. Or, if there is a different escape hatch I have not uncovered yet?

Thanks,

Andy2
Mar 8 '12 #1
8 2746
Rabbit
12,516 Expert Mod 8TB
What is the code doing and why does it need to run every minute?
Mar 8 '12 #2
AndyB2
49
How the data is created; data logger is polling any change to “Tags” from PLCS on the floor. So if we produce a wigged it is logged. Downtime reason codes entered by the operator are also logged. Just the operator sometimes forgets to change their codes back to zero(0) when they startup production.

This code grabs all data logged in the last 2 minutes with a downtime code not zero(0) then check to see if the production over that time is greater than 3. If that is true the code will reset the downtime codes to zero (0) for the operator.
I am tempted to just do something like:

Expand|Select|Wrap|Line Numbers
  1. While 1 = 1 do
  2.   Set var1 = minute(time())
  3.   If var1 <> minute(time()))
  4.     Put code here
  5.   End if
  6. Wend
  7.  
Mar 8 '12 #3
AndyB2
49
Realizing the above code needs to run in its own instance of access. Not the best solution, but could work if there isn't a better one.
Mar 8 '12 #4
Rabbit
12,516 Expert Mod 8TB
And what's the down side if the operator forgets to reset the down time code and your process doesn't run?
Mar 8 '12 #5
AndyB2
49
We track downtime by machine/operator. If we are producing product with a downtime code not zero (0) it makes it hard to figure productivity.

Example 1: Operator 1 is scheduled to produce 100 widgets an hour. His actual downtime for the first hour is 15 min’s (8:30 > 8:45) and he produced 80 widgets, 80/75 (75% of a std. hour) = 106.6% eff.

Example 2: Same operator now forgets to reset his downtime and the data shows the downtime is 30 min’s (8:30 > 9:00) now the math is 80/50 (50% of a std. hour) = 160% eff.
Mar 8 '12 #6
Rabbit
12,516 Expert Mod 8TB
You have a few options.

1) Schedule a SSIS package to run every minute to run an update query. This will only work if SQL Express comes with SSIS.

2) Schedule a VBS script on Windows to run an update query. You will have to use this option if you don't have SSIS.

3) Calculate the end of the down time from the timestamp field in the production table in a query. This has the benefit of running only when you need to see efficiency reports.

Doesn't the table that stores production data have a date/time stamp?
Mar 8 '12 #7
AndyB2
49
Thanks

1) A quick search of SSIS looks like it is available on 2008 and not 2005. At lease when I search MS/SQL 2005 SSIS, it sends me to 2008.

2) I need to see if I can find an example of a VBS script to run an Access update query.

3) As far as doing a calculation with the timestamp, that could happen. But I am building the backend to a website where multiple people will be running the efficiency reports concurrently multiple times a day. I would rather do the number crunching as away from the front end as possible.
Mar 8 '12 #8
NeoPa
32,556 Expert Mod 16PB
I've never had problems dealing with a form's timer process, but then I always ensure there is always time to process what is required before it's next triggered. Basically, timer code should avoid dealing with the operator (as, if they fail to respond, the process can stall) and if there is any possible risk of a timer triggering before the previous one has concluded then always turn the timer off while processing and turn it back on only when completed.

That said, if it's a SQL update you're looking at (on a BE SQL database) then I expect SQL Server can handle that much more efficiently than Access can. I've never done a T-SQL trigger either, but I've seen nothing to cause me to think it's overly complicated. You may be surprised when you look into it.
Mar 8 '12 #9

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

Similar topics

18
by: Jeff Clark | last post by:
I have so asked this question. Let it so be answered.
4
by: Jozef | last post by:
Hello, I'm trying to check for and add a field to a table to a back end database through code. The problem I've been faced with is changing permissions, because I have to use administer...
8
by: Scott M | last post by:
I have a procedure in my Dispose method that records my application was closed in a database. However, when I kill a process or an unhandled exception occurs this does not seem to run. Is there...
1
by: Mark Smith | last post by:
Hi, I have the capture from http stream which is chunked and gzipped. I need code to de-chunk it (i.e. decode it), and then gunzip it--just like a web browser woudl do. Could someone please help...
6
by: Tark Siala | last post by:
hi i spend more time to write the code, like VB6, VB2005, SQL Stored Procedure code. any one know good Application to Automaticaly read Database (Access or SQL), then write VB Code and Stored...
3
by: martin1 | last post by:
Hi, All, is there anyway to refresh window-based application every minute? it is like web-based application refresh by setting up <meta-equiv="refresh" content ="60">. Thanks
4
by: not_a_commie | last post by:
I need code to peg the CPU for 10 seconds to test some background threads. Sleep obviously doesn't do that. How do I code a busy loop that won't get removed by the compiler? Thanks.
1
by: =?Utf-8?B?TmljayBO?= | last post by:
Internet Explorer is failing on my machine about every minute. This just started happening and as far as I know nothing has changed on the computer. No new software or hardware. Here is a Error...
1
by: davidkurniawan | last post by:
Thanks b4 I need code for Hibernate, Log off, Shutdown Computer using API programming in VB.net for XP Vista and another Version Windows Operating System Is it Possibble ? david kurniawan...
6
by: Andy B | last post by:
What would i use to look for changes in a file every minute?
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...

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.