SQL help - why doesn't my code work! Waaaah!

Message Bookmarked
Bookmark Removed
Clever SQL-type Access-using VBasic-savvy people, help me out here, please. I'm stumped.

Have a look at this:
------------------------------------------------------------
' Defines the new query
strSQL = "SELECT Pricebook_Reference.Pricebook_Name_2, MTC_Lookup.Pes, " & _
"MTC_Lookup.PROFILE_CLASS_ID, Prices_Table.[Unit Charge] " & _
"INTO tblCheckPriceTemp " & _
"FROM MTC_Lookup INNER JOIN (Pricebook_Reference INNER JOIN Prices_Table " & _
"ON Pricebook_Reference.Pricebook_Reference = Prices_Table.Pricebook_Reference) " & _
"ON MTC_Lookup.New_Tariff_Code = Prices_Table.New_Tariff_Code" & _
"WHERE (((Pricebook_Reference.Pricebook_Name_2)=[forms].[frmprices]![txtContract]) " & _
"AND ((MTC_Lookup.Pes)=[forms].[frmprices]![txtPES]) " & _
"AND ((MTC_Lookup.PROFILE_CLASS_ID)=[forms].[frmprices]![txtProfile]) " & _
"AND ((Prices_Table.[Unit Charge])=" & oldprice(repeat) & "));"

' puts the new sql into the old query
Dim cat As New ADOX.Catalog
Dim cmd As ADODB.Command
Dim dbs
Dim rst

cat.ActiveConnection = CurrentProject.Connection
Set cmd = cat.Views("qryCheckPrices").Command
cmd.CommandText = strSQL
Set cat.Views("qryCheckPrices").Command = cmd
Set cat = Nothing

-------------------------------------------------------------------

See? All it's supposed to do is put the SQL code into the qryCheckPrices query. Nice and easy. But no. It comes up with the error "Item cannot be found in the collection corresponding to the requested name or ordinal", and when I try and debug it highlights this line:

Set cmd = cat.Views("qryCheckPrices").Command

which I don't understand at all.

Anyone have ANY idea what I've done wrong? I know this is what IS departments are for but they're very very busy people and i'd rather not disturb their manic rushing.

Johnney B (Johnney B), Tuesday, 22 June 2004 08:17 (twenty-two years ago)

The semicolon at the end of the SQL looks a bit suspect...

mark grout (mark grout), Tuesday, 22 June 2004 08:32 (twenty-two years ago)

The weird thing is, I've used this method before, and the code's pretty much identical. Putting a semicolon at the end has been fine before - I'll try it without it.

(later . . . . . )

No, exactly the same. Besides, it would have trouble with the line AFTER if it was the SQL that was iffy, surely?

I dunno.

Johnney B (Johnney B), Tuesday, 22 June 2004 08:40 (twenty-two years ago)

Well, my first thought was that it's not an SQL problem. This is VB, yeah?

mark grout (mark grout), Tuesday, 22 June 2004 08:46 (twenty-two years ago)

Yeh, VB. The SQL I'm sure is fine (I've just copied it from a query in Access) - it's just the VB that's doing my head in.

Johnney B (Johnney B), Tuesday, 22 June 2004 08:49 (twenty-two years ago)

Should have called the thread "VB help" then. ;-)

I have done some VB, but all I can say is to delete the 'command' part and see what the suggestion box gives you etc..

mark grout (mark grout), Tuesday, 22 June 2004 09:04 (twenty-two years ago)

You need to add the view indexed by "qryCheckPrices" before you can refer to it. Use cat.Views.Append I think.

Sam (chirombo), Tuesday, 22 June 2004 09:09 (twenty-two years ago)

Yes mark, I guess you're right. I am nothing but a failure. (hangs head in shame)

Uh, Sam . . . . and what does that involve? Sorry, you're gonna have to spell it out to me - I'm not as hot as I'd like on this kinda thing.

Johnney B (Johnney B), Tuesday, 22 June 2004 09:26 (twenty-two years ago)

I'm afraid this is all specific to ADO and I don't know much about ADO. But your cat object looks like it has a number of views hanging off it, all defined by a particular name (or number, called the ordinal). You only want one view, referred to by "qryCheckPrices". I expect that when you define a Catalog object it has zero views by default, so you need to add that view to the cat object first, and name it, before you can do anything with it. My VB environment provides me with nice lists of all the methods I can use when I type a full stop after an object name, which makes this sort of thing easy to guess. So I think cat.views.append("qryCheckPrices") or something is what you want. Google it.

Sam (chirombo), Tuesday, 22 June 2004 09:35 (twenty-two years ago)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/admscviewcreationexample.asp

Sam (chirombo), Tuesday, 22 June 2004 09:37 (twenty-two years ago)

My VB environment provides me with nice lists of all the methods I can use when I type a full stop after an object name, which makes this sort of thing easy to guess.

Yeah, mine too.

mark grout (mark grout), Tuesday, 22 June 2004 09:40 (twenty-two years ago)

This is the part of VB I'm shaky on but I'm thinking that Sam is on the right track here.

VengaDan Perry (Dan Perry), Tuesday, 22 June 2004 10:33 (twenty-two years ago)

one year passes...
i want to eliminate rows with duplicate entries in columns SymbolA and SymbolB. this select statement gets the rows (as far as i can tell):
SELECT *
FROM CorrelationsFinalTest t1
WHERE EXISTS
(SELECT SymbolA
FROM CorrelationsFinalTest t2
WHERE t2.SymbolA = t1.SymbolA AND t2.SymbolB = t1.SymbolB)

but if i change "select *" to "delete" SqlServer complains incorrect syntax near t1. can anyone help? i'm sure there's a better, more efficient way to eliminate dupe rows, i'm not a sql programmer and i'd appreciate any help.

W i l l (common_person), Friday, 8 July 2005 15:50 (twenty years ago)

I don't think you can nest a SELECT within a DELETE.

Create a SELECT that gets all the data with the duplicates removed. Then create a new table with the same structure as this one and use INSERT INTO newtable SELECT ... to populate it. Then rename (and eventually delete) the original table and move the new table into its place.

(note I don't know anything about SQL Server specifically)

grraham (noodles is a cunt), Friday, 8 July 2005 15:59 (twenty years ago)

I don't think you can alias the delete table (e.g. "t1")

I'm a bit rusty, but if the tables aren't huge, (this may not be the most efficient way but) this should work:

delete from CorrelationsFinalTest
where CorrelationsFinalTest.(keyfield) in
select ( keyfield
FROM CorrelationsFinalTest t1
WHERE EXISTS
(SELECT SymbolA
FROM CorrelationsFinalTest t2
WHERE t2.SymbolA = t1.SymbolA AND t2.SymbolB = t1.SymbolB))

I would normally suggest creating a temporary table to hold the delete criteria (i.e. the nested select where exists stuff) and then delete where table 1 mmatches the temp table. Seems safer to me, but as I said, I'm rusty.

(xpost ..)

geyser muffler and a quarter (Dave225), Friday, 8 July 2005 16:04 (twenty years ago)

ok, so how can i form a SELECT DISTINCT that only checks on those two columns, ignoring the rest?

xpost the table has 3.5 million lines :\

W i l l (common_person), Friday, 8 July 2005 16:06 (twenty years ago)

How many duplicates are you getting when you run that Select statement? I know it's a matter of principle to write a single statement, but manually deleting is sometimes faster ....

geyser muffler and a quarter (Dave225), Friday, 8 July 2005 16:12 (twenty years ago)

Try this for identifying the identical rows:

SELECT t1.keyfield FROM CorrelationsFinalTest t1, CorrelationsFinalTest t2 WHERE t1.SymbolA = t2.SymbolA AND t1.SymbolB = t2.SymbolB AND t1.keyfield != t2.keyfield

Forest Pines (ForestPines), Friday, 8 July 2005 16:12 (twenty years ago)

there are way more duplicates than i could manually delete in the time i have remaining at this job.

dave225, your suggestion gives me "Error in list of values in IN clause." i wonder if this is some SqlServer bullshit. i found a page that gives three solutions for eliminating dupes using oracle's variety of sql but they generate syntax errors when i try 'em in SqlServer.

W i l l (common_person), Friday, 8 July 2005 16:22 (twenty years ago)

Actually, you might be better off junking the aliasing all together. Just use the table names when referencing fields. I've had problems in SQLServer2000 w/ doing UPDATEs using aliasing. Once I just junked the aliases, everything was smooooooth.

But, yeah, it might be better to just create a view containing the IDs of the rows you need to delete, and then reference that view in the DELETE.

David R. (popshots75`), Friday, 8 July 2005 16:27 (twenty years ago)

one year passes...
Hmm. SQL Server 2000:

Anyone know how to convert a binary value that's contained in a varchar variable to a binary variable? I've written a procedure that takes info from sysprocesses, makes it more user friendly and can be viewed from the intranet, and I want to convert the job_id to job_name (using msdb..sysjobs).

The string looks like this:

'SQLAgent - TSQL JobStep (Job 0x57926A9F88B2D7468BB07BDF51E51C63 : Step 18)'

So I used substring to get it down to '0x57926A9F88B2D7468BB07BDF51E51C63' but if I convert that to binary it comes out as 0x30783537393236413946383842324437! Any idea how to convert a string directly to binary as it is?

I have a work-around involving dynamic SQL and a cursor but it would be better if I could just use a CAST/CONVERT statement and do it in one go.

Colonel Poo (Colonel Poo), Wednesday, 30 August 2006 12:50 (nineteen years ago)

one year passes...

Okay, so I've got a list of site references and dates.

site_no date
123456 2000-01-04
123456 2000-01-08
123456 2000-01-09
123456 2000-01-10
456789 2001-05-18

I want to turn these into a series of start and end date ranges, like this.

Site_No startdate end date
123456 2000-01-04 2000-01-05
123456 2000-01-08 2000-01-11
456789 2001-05-18 2001-05-19

Anyone got any ideas how I could do this in SQL? I'd rather do it without looping if at all possible.

The Wayward Johnny B, Friday, 4 January 2008 14:34 (eighteen years ago)

What do the dates mean/relate to?

Mark G, Friday, 4 January 2008 14:36 (eighteen years ago)

They're just arbitrary dates, these don't actually relate to anything! In real life I want to use this principle to work out where there are gaps in billing - so, in this example, site 123456 needs to be billed from the 04/01 to the 05/01 and 08/01 to 11/01.

The Wayward Johnny B, Friday, 4 January 2008 14:40 (eighteen years ago)

You have dates 04, 08, 09, 10th for 12345

How would that specific example relate to ranges?

Mark G, Friday, 4 January 2008 14:43 (eighteen years ago)

For site 123456, I want two records, one saying 04/01 - 05/01, and one saying 08/01 - 11/01. Start and end dates, if you like.

The Wayward Johnny B, Friday, 4 January 2008 14:46 (eighteen years ago)

Oh I see, one for Date 1 to the first day break and then one to the next.

Blimey, I'll have to think.

Mark G, Friday, 4 January 2008 14:58 (eighteen years ago)

I don't think this would be very easy to do.

That is my useless answer so far.

Colonel Poo, Friday, 4 January 2008 15:01 (eighteen years ago)

Siteno Date

123456 2000-01-04
123456 2000-01-08
123456 2000-01-09
123456 2000-01-10
456789 2001-05-18

Select SiteNo, DateFrom, DateTo
from
(Select SiteNo, Date as DateFrom
from table
left outer join table as table2
on table.siteno = table2.siteno
and dateadd(1, table.date) = table2.date
where table2.date is null)

inner join

(Select SiteNo, Date as DateFrom from table left outer join table as table2 where table.siteno = table2.siteno and dateadd(1, tabale.date) = table2.date where table2.date is null)

etc, I dunno. Something like that.

Mark G, Friday, 4 January 2008 15:06 (eighteen years ago)

The first record is fairly easy, I'm getting records that don't have the corresponding next day record.

The second one is harder, as you want 08 rather than 10.

I guess a pre-pass type view, then a "min" function from linking that view to the original table.

Hope that helps in your path.

Mark G, Friday, 4 January 2008 15:08 (eighteen years ago)

I think I see what you're doing. I get the idea . . kinda. I'll have a play with that idea tho. Cheers mate!

(tho if you get any brainwaves, do tell!)

The Wayward Johnny B, Friday, 4 January 2008 15:15 (eighteen years ago)

SELECT SireRef, DATEADD(dd, 1, DateFrom) AS DateTo
FROM (SELECT table1.SireRef, table1.Date AS DateFrom
FROM dbo.tmp_Date AS table1 LEFT OUTER JOIN
dbo.tmp_Date AS table2 ON table1.SireRef = table2.SireRef AND DATEADD(dd, 1, table1.Date) = table2.Date
WHERE (table2.Date IS NULL)) AS derivedtbl_1

That one gives you, one per break, the site code and the break date.

Mark G, Friday, 4 January 2008 15:52 (eighteen years ago)

So, relink your orig table to that, select where origtabledate < breakdate, then :

Select SiteRef, min(origtable.date) as DateFrom, DateTo from
(blahdiblah)
Group by SiteRef, DateTo

Bob's yer.

Mark G, Friday, 4 January 2008 15:54 (eighteen years ago)

(change that "select where orig...." to "inner join" etc, you know.

Mark G, Friday, 4 January 2008 15:55 (eighteen years ago)

RIGHT! HERE IS THE ANSWER!!!

SELECT SireRef, MIN(DateFrom) AS DateFrom, DateTo
FROM (SELECT dbo.tmp_Date.SireRef, dbo.tmp_Date.Date AS DateFrom, MIN(eee.DateTo) AS DateTo
FROM dbo.tmp_Date INNER JOIN
(SELECT SireRef, MIN(DATEADD(dd, 1, DateFrom)) AS DateTo
FROM (SELECT table1.SireRef, table1.Date AS DateFrom
FROM dbo.tmp_Date AS table1 LEFT OUTER JOIN
dbo.tmp_Date AS table2 ON table1.SireRef = table2.SireRef AND DATEADD(dd, 1, table1.Date) = table2.Date
WHERE (table2.Date IS NULL)) AS derivedtbl_1
GROUP BY SireRef, DateFrom) AS eee ON eee.SireRef = dbo.tmp_Date.SireRef AND dbo.tmp_Date.Date < eee.DateTo
GROUP BY dbo.tmp_Date.Date, dbo.tmp_Date.SireRef) AS yeah
GROUP BY SireRef, DateTo

I thangyew!

Mark G, Friday, 4 January 2008 16:07 (eighteen years ago)

Awesome! Cheers sir, much appreciated.

http://www.linksoflondon.com/dynamic/eshop/product_images/thumbnail_cache/360x360/607-thumbprint-star-gold-charm-image-1.jpg

The Wayward Johnny B, Friday, 4 January 2008 16:12 (eighteen years ago)


You must be logged in to post. Please either login here, or if you are not registered, you may register here.