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

Update query Inner Join question

I've tried each of the below, but no luck.

UPDATE tblEntity As tbl INNER JOIN search3220 As qry ON tbl.Entity_ID =
qry.Entity_ID SET tbl.Cat_ID = 289;

UPDATE tblEntity INNER JOIN search3220 ON tblEntity.Entity_ID =
seach3220.Entity_ID SET tblEntity.Cat_ID = 289;

The only issue I can imagine aside from syntax is that Entity_ID is not
unique in the query. tblEntity will only have one Entity_ID of 1700, for
example, while search3220 may have several Entity_IDs of 1700. Is this the
problem? Or is it a syntax issue?

Thanks in advance.
Nov 13 '05 #1
10 10486
I figured it out:

UPDATE DISTINCTROW tblEntity INNER JOIN search3220 ON tblEntity.Entity_ID =
search3220.Entity_ID SET tblEntity.Cat_ID = QryPrm("frmTools", "frmCatAdm",
"cbxNewCat");

Jet wants the DISTINCTROW statement for some reason...

The only issue now is that query search3220 is created dynamically on the
fly - it could be search2110 or whatever - I don't know what it is until
runtime. I could build a string with code and use something like
"db.Execute strSql" - but I need the performance of a compiled query.

Is there anyway to search the qureyDefs collection and insert the query name
into a compiled query, something similar to what I'm doing with the QryPrm
function?
Nov 13 '05 #2

"deko" <www.clearpointsystems.com@use_contact_form.com> wrote in message
news:O0*****************@newssvr14.news.prodigy.co m...
I figured it out:

UPDATE DISTINCTROW tblEntity INNER JOIN search3220 ON tblEntity.Entity_ID = search3220.Entity_ID SET tblEntity.Cat_ID = QryPrm("frmTools", "frmCatAdm", "cbxNewCat");

Jet wants the DISTINCTROW statement for some reason...

The only issue now is that query search3220 is created dynamically on the
fly - it could be search2110 or whatever - I don't know what it is until
runtime. I could build a string with code and use something like
"db.Execute strSql" - but I need the performance of a compiled query.

Is there anyway to search the qureyDefs collection and insert the query name into a compiled query, something similar to what I'm doing with the QryPrm
function?


Here is a barebones example of how to iterate through the QueryDef
collection.

This displays all Query names and SQL in the VBA IDE Immediate Window.
Public Sub ShowQueryDefs()

Dim db As DAO.Database
Dim qds As DAO.QueryDefs
Dim qd As DAO.QueryDef

Set db = CurrentDb()
Set qds = db.QueryDefs

For Each qd In qds
Debug.Print qd.Name
Debug.Print qd.SQL
Next qd

Set qd = Nothing
db.Close
Set db = Nothing

End Sub
To do what you need to do, modify the above to check each SQL string for
whatever it is you're looking for, then dynamically assemble a new SQL
string containing whatever it is you need, and then use "qd.sql =
SQLString".
Nov 13 '05 #3
> Here is a barebones example of how to iterate through the QueryDef
collection.

This displays all Query names and SQL in the VBA IDE Immediate Window.
Public Sub ShowQueryDefs()

Dim db As DAO.Database
Dim qds As DAO.QueryDefs
Dim qd As DAO.QueryDef

Set db = CurrentDb()
Set qds = db.QueryDefs

For Each qd In qds
Debug.Print qd.Name
Debug.Print qd.SQL
Next qd

Set qd = Nothing
db.Close
Set db = Nothing

End Sub
To do what you need to do, modify the above to check each SQL string for
whatever it is you're looking for, then dynamically assemble a new SQL
string containing whatever it is you need, and then use "qd.sql =
SQLString".


So, basically, I just create a QueryDef object using an SQL string - which
contains the name of the dynamically-created query. This sounds reasonable.
But I'm wondering if the overhead of doing this is worth whatever
performance gains a compiled query will provide.

Is there much of a performance difference between:

db.Execute strSomeSqlString

and

db.Execute "qryQueryName"

?

Thanks for the help!
Nov 13 '05 #4

"deko" <www.clearpointsystems.com@use_contact_form.com> wrote in message
news:S0*****************@newssvr14.news.prodigy.co m...
Here is a barebones example of how to iterate through the QueryDef
collection.

This displays all Query names and SQL in the VBA IDE Immediate Window.
Public Sub ShowQueryDefs()

Dim db As DAO.Database
Dim qds As DAO.QueryDefs
Dim qd As DAO.QueryDef

Set db = CurrentDb()
Set qds = db.QueryDefs

For Each qd In qds
Debug.Print qd.Name
Debug.Print qd.SQL
Next qd

Set qd = Nothing
db.Close
Set db = Nothing

End Sub
To do what you need to do, modify the above to check each SQL string for
whatever it is you're looking for, then dynamically assemble a new SQL
string containing whatever it is you need, and then use "qd.sql =
SQLString".


So, basically, I just create a QueryDef object using an SQL string - which
contains the name of the dynamically-created query. This sounds

reasonable.

No. The SQLString contains the SQL.

The SQL property of the QueryDef object contains SQL.

The Name property of the QueryDef object contains the Query's name.
If you want to create entirely new QueryDefs, use:

Public Sub Exam_CreateQuery()

Dim db As DAO.Database
Dim qds As DAO.QueryDefs
Dim qd As DAO.QueryDef
Dim SQLString As String

Set db = CurrentDb()
Set qds = db.QueryDefs
Set qd = New QueryDef

SQLString = "SELECT * FROM tblInventory;"

qd.Name = "ShowTblInventory"
qd.SQL = SQLString

qds.Append qd

Set qd = Nothing
db.Close
Set db = Nothing
End Sub
Nov 13 '05 #5
> No. The SQLString contains the SQL.

Yes, but the query name is part of the SQL.
The SQL property of the QueryDef object contains SQL.


So I can change the SQL property of an existing QueryDef object? I didn't
know I could that. I'd think this would be more efficient than creating a
new QueryDef, and also more efficient than assembling a string with code and
running it with "db.Execute strSqlstring" In fact, why not just have one
QueryDef object and simply change the SQL property whenever needed?

For example, there could be a thousand situations where I need to create an
SQL string based on user selection or some control setting, and I just
update the SQL property of a single QueryDef object? And this is faster
than "db.Execute strSqlstring"?
Nov 13 '05 #6

"deko" <www.clearpointsystems.com@use_contact_form.com> wrote in message
news:oj*****************@newssvr14.news.prodigy.co m...
No. The SQLString contains the SQL.
Yes, but the query name is part of the SQL.


Oh. I understand now. You meant the QueryDef name being added to the SQL
itself. Sorry.
The SQL property of the QueryDef object contains SQL.
So I can change the SQL property of an existing QueryDef object? I didn't


Yes.
know I could that. I'd think this would be more efficient than creating a
new QueryDef, and also more efficient than assembling a string with code and running it with "db.Execute strSqlstring" In fact, why not just have one
QueryDef object and simply change the SQL property whenever needed?
I believe that when the SQL property changes, Access must recompile it.
There is no way I know of (which doesn't mean there isn't one) to avoid
recompilation/query-optimization when the SQL of a QueryDef changes.

For example, there could be a thousand situations where I need to create an SQL string based on user selection or some control setting, and I just
update the SQL property of a single QueryDef object?
Yes.
And this is faster than "db.Execute strSqlstring"?
It's on a case by case basis. Access's tools for assessing query execution
costs aren't the best, IMO. One query you run will be quick regardless of
how it's initiated inside Access, another will be slow, also regardless of
how its initiated.

The cost of compilation of a single QueryDef is negligible, but if you put
that inside a loop and recompile over and over, heck, I don't know, I've
never tried anything like that. I'd go out of my way to avoid it. Changing
them that many times would imply executing them that many times, and the
cost would be huge. If I were forced to do it, it would be for queries that
returned single rows off unique indexed columns (preferably the Primary Key)
without any aggregation.

Nov 13 '05 #7
deko wrote:
I figured it out:

UPDATE DISTINCTROW tblEntity INNER JOIN search3220 ON tblEntity.Entity_ID =
search3220.Entity_ID SET tblEntity.Cat_ID = QryPrm("frmTools", "frmCatAdm",
"cbxNewCat");

Jet wants the DISTINCTROW statement for some reason...

The only issue now is that query search3220 is created dynamically on the
fly - it could be search2110 or whatever - I don't know what it is until
runtime. I could build a string with code and use something like
"db.Execute strSql" - but I need the performance of a compiled query.
What performance hit would you take? .1 second? .5 seconds?

The only performance hit I see is calling QryPrm(). Not sure what it
does internally but if your query is slow, that's your bottleneck.
Is there anyway to search the qureyDefs collection and insert the query name
into a compiled query, something similar to what I'm doing with the QryPrm
function?
Chris supplied the answer there. You store the SQL to a query.

Nov 13 '05 #8
> The cost of compilation of a single QueryDef is negligible, but if you put
that inside a loop and recompile over and over, heck, I don't know, I've
never tried anything like that. I'd go out of my way to avoid it. Changing them that many times would imply executing them that many times, and the
cost would be huge. If I were forced to do it, it would be for queries that returned single rows off unique indexed columns (preferably the Primary Key) without any aggregation.


I don't have any loops in mind, but I do have several modules that use code
to assemble SQL strings - complex Update and Select queries with multiple
joins and parameters from form controls - which seem to run pretty slowly
when executed with "db.Execute strSqlString".

Based on your suggestion, I'm considering this:

Dim db as DAO.Database
Dim qItem As QueryDef
Set db = CurrentDb
Set qdf = db.QueryDefs
For Each qItem In qdf
If qItem.Name = strQry Then Set qItem.SQL = strSql
qItem.Execute
Next qItem

This would allow me to dynamically create both the query name and the Sql
string on the fly with code, and still get the performance of a compiled
query. Does this sound correct?

But I have over 300 queries - it seems inefficient to have to loop through
all of them to get the one I want if I know the name of the query. Can the
above loop be improved if I know the name of the qItem I'm looking for?

Also - Intellisense shows an OpenRecordset method for qItem:

qItem.OpenRecordset

Does this mean I can do this:

Set rst = qItem.OpenRecordSet?

Thanks for the help!
Nov 13 '05 #9
This seems to be working:

Dim db As DAO.Database
Dim qItem As DAO.QueryDef
Dim qdf As DAO.QueryDefs

Set db = CurrentDb
Set qdf = db.QueryDefs

For Each qItem In qdf
Debug.Print qItem.Name
If qItem.Name = "_qryQueryName" Then
qItem.SQL = strSql
Debug.Print "Setting SQL property for " & qItem.Name
Exit For
End If
Next

Prefixing the query name with "_" puts it first in the list of qItems, so
the loop only iterates once, instead of 300+ times.

I'm still having trouble with:

qItem.Execute
qItem.OpenRecordset

I suppose I can always use:

db.Execute "_qryQueryName"
Set rst = db.OpenRecordset("_qryQueryName")

Thanks again for the help!
Nov 13 '05 #10

"deko" <www.clearpointsystems.com@use_contact_form.com> wrote in message
news:dl******************@newssvr21.news.prodigy.c om...

I don't have any loops in mind, but I do have several modules that use code to assemble SQL strings - complex Update and Select queries with multiple
joins and parameters from form controls - which seem to run pretty slowly
when executed with "db.Execute strSqlString".
Each table added to a query slows it down. As the complexity goes up, the
speed goes down. I'm sure you already know that part, what I'm trying to
say is I don't think you'll find using db.execute "SELECT . . ." and
db.execute existing_QueryDef will have nearly as big an impact as the actual
complexity of the query itself.

Unfortunately, as nice a critter as Access is, it isn't the smartest db
manager on the block, and it will likely never make the best optimization
decisions.


Based on your suggestion, I'm considering this:

Dim db as DAO.Database
Dim qItem As QueryDef
Set db = CurrentDb
Set qdf = db.QueryDefs
For Each qItem In qdf
If qItem.Name = strQry Then Set qItem.SQL = strSql
qItem.Execute
Next qItem

This would allow me to dynamically create both the query name and the Sql
string on the fly with code, and still get the performance of a compiled
query. Does this sound correct?

But I have over 300 queries - it seems inefficient to have to loop through
all of them to get the one I want if I know the name of the query. Can the above loop be improved if I know the name of the qItem I'm looking for?

Also - Intellisense shows an OpenRecordset method for qItem:

qItem.OpenRecordset

Does this mean I can do this:

Set rst = qItem.OpenRecordSet?
Yes. rst needs to be created first, "dim rst as DAO.Recordset" (and so
on). What you get is a "recordset" that can be iterated through. Sort of a
virtual datasheet (*sort of*). You can use the recordset methods, .edit,
..add, .update, etc. on it. Recordsets are a *big* discussion. (There are
also ADO and RDO versions, as well, which is why you *always* prefix with
the object model you are drawing on, otherwise, problems will occur.)

Since you have Access installed (hopefully 2000+), you also probably have
the following files, which I find invaluable.

DAO360.CHM <--Recordsets are defined in here (along with the rest of
Direct Access Objects 3.6).
JETSQL40.CHM <--SQL for Access is in here.
VBLR6.CHM <--VB 6.0 is in here.

I really can't live without these. :) They are my #1 Resource (followed
by Google's archive of these newsgroups, www.mvps.org/access/, and several
of the better books). Look through them, they will do you good. The search
function in them for A2000 is hopelessly useless, use the index instead.

DNJET.CHM (Microsoft Jet Database Engine Programmer's Guide), is also very
interesting, but unfortunately, it's for JET 3.5. It still has some useful
info in it.
Thanks for the help!


You are welcome. :)


Nov 13 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
4
by: Andy Proctor | last post by:
I hope there is an answer out there.... I have a simple database structured like this (non relevant tables and fields omitted) Members table memberID memberFname memberLname memberNokID
1
by: septen | last post by:
Hi, I have a select query of 12 tables. The SQL code is as follows: SELECT OptChannelRangeAve.ChannelRangeMin, OptChannelRangeAve.ChannelRangeMax, OptChannelRangeAve.SampleAveraging,...
4
by: deko | last post by:
I'm trying to update the address record of an existing record in my mdb with values from another existing record in the same table. In pseudo code it might look like this: UPDATE tblAddress SET...
1
by: G Gerard | last post by:
Hello I am trying to update a table (TableB) using records from a second table (TableA)
2
by: Antonio | last post by:
Can someone tell me why the following procedure updates ALL the records in the database with the field being updated for one record? private void updateRow(object source,...
8
by: Richard | last post by:
Hello! I have this piece of SQL code: UPDATE a SET Field1 = c.Field1 FROM a INNER JOIN b ON a.GUID1 = b.GUID1 INNER JOIN c ON b.GUID2 = c.GUID2 WHERE c.Type = 1
5
by: Ferasse | last post by:
Hi, I'm an occasional Ms-Access developer, so there is still a lot of stuff that I don't get... Right now, I'm working on a database that stores contractual information. One of the form that...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.