[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Server complains of an unfound column when using rs.update

Posted on 2004-11-12
10
Medium Priority
?
267 Views
Last Modified: 2013-12-25
Here's my problem...

I have a table with several fields

GroupName|OMTimestamp|intValue1|intValue2|intValue3|BusyHour

I want to mark the busiest hour for each combination of GroupName, Date.
So i open a recordset that groups by GroupName, Date and order it by the sum of (intValue1 + intValue2 + intValue3) as TotalCount descending.

Then i iterate through the recordset and every time i see a new groupname or date i set that busyHour to true (first one in a descending query is always the highest)
However, when i try to do a rs.update it tells me that the column TotalCount does not exist.  Of course it doesn't exist its a calculated value!

The weirdest part however, is that up until 2 days ago...this code worked FINE...

Here's the code just in case

rs.Open "SELECT GroupName, BH, REGNIVIC + LOCREQIC + CSSIIVIC + RGCNIVOG + RTEREQOG + TRANIVIC AS IS41Count FROM pt_OMS_IS41 " & _
        "WHERE OMTimestamp BETWEEN '" & Format(dteDate, "mm/dd/yyyy") & "' AND '" & Format(dteDate, "mm/dd/yyyy") & " 11:59:59 PM' " & _
        "ORDER BY GroupName, REGNIVIC + LOCREQIC + CSSIIVIC + RGCNIVOG + RTEREQOG + TRANIVIC DESC"
While Not rs.EOF
    If rs!GroupName <> i Then
        rs!BH = "Y"
        rs.Update
       
        i = rs!GroupName
    End If
   
    rs.MoveNext
Wend

What's causing this?
0
Comment
Question by:TriggerHappy
  • 5
  • 4
10 Comments
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12567198
Why not just use an update statement and keep it all in your back end?

Anyway, there is no need to select

REGNIVIC + LOCREQIC + CSSIIVIC + RGCNIVOG + RTEREQOG + TRANIVIC AS IS41Count

as you don't appear to use it.
0
 
LVL 9

Author Comment

by:TriggerHappy
ID: 12568112
I know how to fix it.  The question is WHY it's doing this and WHY did it only start happening 2 days ago?
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 1500 total points
ID: 12569131
why it is not working is simple, you should NOT be using the same recordset to BOTH retrieve the data, and then update the underlying table.  In this case, you are creating a calculated field in the original SQL, and then trying to update THAT recordset back to the underlying table, where that calcualted field does not exist.

You would be much better to read the data using the SQL as you currently have it, but then use an UPDATE query.

Create a Command Object, and then set the CommandText to:

strUpdate = "UPDATE pt_OMS_IS41 set BH = "Y" where GroupName = '" & rs!GroupName & "'"

and use the Command.Execute method to execute the SQL, to update the underlying table

AW
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12569202
oops, that will update ALL of the records for GroupName, so try this:

strUpdate = "UPDATE pt_OMS_IS41 set BH = "Y" where GroupName = '" & rs!GroupName & "' AND " & _
"REGNIVIC + LOCREQIC + CSSIIVIC + RGCNIVOG + RTEREQOG + TRANIVIC = " & rs!IS41Count

or you could extract the individual filed values (REGNIVIC, LOCREQIC...) in the original query, as you are not GROUPING the data, and use each field in the Where clause, to only update the spcific record that has those specifi values.

AW


0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12569220
what did you change between 2 days ago and today...you MUST have changed something.

AW
0
 
LVL 9

Author Comment

by:TriggerHappy
ID: 12571270
Nothing, Literally...Unless somebody did something with the SQL Server...That I don't have access to unfortunately.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12574829
well, it would appear that if this EXACTLY SQL worked for update in the past, then the table schema was changed to remove the field IS41Count from the field definitions.  That is the only thing that would lead to the error message that you are now getting.

It is a VERY bad design, by the way, to have a field in a table that is the result of a calculation on other fields in the same record of that table.  This violates one of the basic design tenets of a Relational Database.

AW
0
 
LVL 9

Author Comment

by:TriggerHappy
ID: 12575823
That column never has and never will exist and that's the problem.  
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12576576
if that is the case, then your statement that this INSERT that is now failing, succeeded in the past CANNOT POSSIBLY BE TRUE.  Sorry to be blunt, but it can't be correct.

AW
0
 
LVL 9

Author Comment

by:TriggerHappy
ID: 12585037
Well...Without any changes yet again, it works again...I'm at a loss for words...

I will award the points to Arthur based on good information about not doing this sort of thing in the future.

(Just to cover my hide here, this isnt my code, i was just called in to fix it when it broke down)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question