473,503 Members | 11,237 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Update a groupBy query in code

105 New Member
Hey everyone,

I have a question that's been troubling me for a bit. I work for Alabama Power. I have 1 table (tblOutages). Every morning I import a list of Power Outages for the state of Alabama into that table. I have a yes/no (chkOutage) field set-up in the table to set chkOutage = TRUE on every record that is imported.

Here's my question, and it's a bit long. :) I would be much appreciative if anyone could help!

At the end of the import I am trying to run a query that pulls all of the records that have the chkOutage field set to TRUE. In code, I have that set-up as sqlStatement1. That part is done. Now (for the difficult part)....in the table, each outage has a CASE NUMBER field and a REFERENCE NUMBER field. The REFERENCE NUMBER is unique to the outage (no other outage has the same reference number), but the CASE NUMBER is not unique (other outages can be assigned to the same case number via location within the state). For example, a certain CASE NUMBER might have multiple REFERENCE NUMBERS (kind of like an ID for the record)...

:)

So...having said that...Every outage also has a CMI (CUSTOMER MINUTES INTERRUPTED - basically, it holds how many minutes that the certain REFERENCE NUMBER was without power). I want to run a GROUPBY query that pulls all REFERENCE NUMBERS in the table, maybe as a 'COUNT' and GROUP them by CASE NUMBER, but they MUST have the chkOutage field set to TRUE AND the CMI 'SUM' must be < 25,000. I want to make this sqlStatement1.

Then for EVERY INDIVIDUAL RECORD that's accounted for in the GROUPBY query, I want to run an UPDATE query on other certain fields that go along with those individual records.

This has got me banging my head against the wall for a couple days now.

Here's my function that I run at the end of the import, currently it says that I am missing 1 parameter...

Expand|Select|Wrap|Line Numbers
  1. Public Function CheckOutagesForReview()
  2.  
  3. Dim sqlStatement1 As String
  4. Dim sqlStatement2 As String
  5. Dim dbs_curr As Database
  6. Dim records1 As Recordset
  7. Dim records2 As Recordset
  8.  
  9. Set dbs_curr = CurrentDb
  10. sqlStatement1 = "SELECT tblOutages.CaseNumber, Count(tblOutages.RefNumber) AS CountOfRefNumber, Sum(tblOutages.CMI) AS SumOfCMI, tblOutages.chkOutages FROM tblOutages GROUP BY tblOutages.CaseNumber, tblOutages.chkOutages HAVING (((tblOutages.chkOutages)=True));"
  11. Set records1 = dbs_curr.OpenRecordset(sqlStatement1, dbOpenDynaset, dbSeeChanges, dbOptimistic)
  12.  
  13. sqlStatement2 = "SELECT * FROM tblOutages WHERE (((tblOutages.CaseNumber) In (SELECT CaseNumber FROM qryCheckOutages WHERE sqlStatement1.CaseNumber=tblOutages.CaseNumber)));"
  14. Set records2 = dbs_curr.OpenRecordset(sqlStatement2, dbOpenDynaset, dbSeeChanges, dbOptimistic)
  15.  
  16. 'CMI Check
  17. With records1
  18.     If records1!SumOfCMI < 25000 Then
  19.         While Not records2.EOF
  20.             records2.Edit
  21.             records2!Printed = True
  22.             records2!Status = 4
  23.             records2!Findings = "Outage did not meet minimum requirements for review"
  24.             records2.Update
  25.         Wend
  26.     End If
  27. End With
  28.  
  29. 'Tree Growth and Substation lock out check
  30. While Not records2.EOF
  31.     With records2
  32.         If records2!Cause = 38 Or (records2!Switch = records2!FeederNumber) Then
  33.             records2.Edit
  34.             !Printed = False
  35.             !Status = 1
  36.             records2.Update
  37.         Else
  38.             records2.Edit
  39.             !Printed = True
  40.             !Status = 4
  41.             !Findings = "Outage did not meet minimum requirements for review"
  42.             records2.Update
  43.         End If
  44.     End With
  45. Wend
  46.  
  47. End Function  
Apr 16 '08 #1
1 2161
gpl
152 New Member
It sounds very complicated, I would approach this by trying to simplify the problem.

Firstly, when you have identified the groups that need updating, load the keys into an intermediate table. You should then be able to run your update with reference to these keys.

Does that help ?
Graham
Apr 17 '08 #2

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

Similar topics

1
1254
by: Paul T. RONG | last post by:
Hello all, I have a problem, wishing somebody can help, this is the case, in the table order_detail: ProductNumber price quantity 001 1.00 5 002 ...
2
3053
by: Marcus | last post by:
I have noticed something odd on one of my queries. Maybe someone can help or explain. I am using the GroupBy function on all my rows except for pulling "Min" StartDate, "Max" EndDate, and "Min"...
4
2283
by: Bryan | last post by:
can some explain why in the 2nd example, m doesn't print the list which i had expected? >>> for k, g in groupby(): .... print k, list(g) .... 1 2 3
20
1882
by: Frank Millman | last post by:
Hi all This is probably old hat to most of you, but for me it was a revelation, so I thought I would share it in case someone has a similar requirement. I had to convert an old program that...
1
6739
by: Roman Bertle | last post by:
Hello, there is an example how to use groupby in the itertools documentation (http://docs.python.org/lib/itertools-example.html): # Show a dictionary sorted and grouped by value .... ...
13
4551
by: 7stud | last post by:
Bejeezus. The description of groupby in the docs is a poster child for why the docs need user comments. Can someone explain to me in what sense the name 'uniquekeys' is used this example: ...
3
1470
by: Steve Howell | last post by:
George Sakkis produced the following cookbook recipe, which addresses a common problem that comes up on this mailing list: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/521877 I...
10
1607
by: 7stud | last post by:
I'm applying groupby() in a very simplistic way to split up some data, but when I timeit against another method, it takes twice as long. The following groupby() code groups the data between the...
9
2986
by: patrick.waldo | last post by:
Hi all, I tried reading http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/334695 on the same subject, but it didn't work for me. I'm trying to learn how to make pivot tables from some...
0
7070
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
7267
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,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.