473,545 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Complex SQL statement in MS Access

Zwoker
66 New Member
Hi all,

I'm not sure whether this should be posted in the MS Access forum or one of the SQL forums (which one?), so I'll start here.

I'm self taught in the syntax of SQL based queries that I have been writing in VBA in my MS Access 2003 environment, which has proved to be an issue now that I want to do something a bit more complicated than the ordinary, but have no idea how. Googling for an example has failed to find anything close to what I want.

What I'm trying to do is write a single SQL statement to read all the records that match user-selected criteria in a master table, and as part of that, read just one record from a JOIN'ed transaction table, with the record I want being the most recent record for the joined keys. Fortunately there is a date field in the transaction table.

Now I believe that I need some sort of subquery with a TOP 1 in it, with an ORDER BY the transaction date (descending), but trying to integrate the subquery with the JOIN statement has been a complete disaster.

I separated the two activities, doing the initial SELECT over the master and getting all the records that matched the user criteria, and then looping through each of those records to get the most recent transaction record, but the run time for the process went from 35 seconds without the looped read of the transaction table, to over 30 minutes with it present.

Does anyone have a simple example of a JOIN and a subquery that is something like what I'm trying to do, that I can use as the foundation for my query?


Thanks.
Mar 20 '08 #1
4 2237
janders468
112 Recognized Expert New Member
I think that the easiest way to do this would be to split it into two queries, the first query should consist of all the records to retrieve from the master table based on whatever criteria. Join this query to the transaction table ordering by the date field descending, which will put the most recent date first and use the top keyword after the select statement just like you suggested.
So it would be

Select TOP 1 Field1, Field2...Fieldn
From TransactionTabl e Inner Join TransactionTabl e.KeyField = MasterTableQuer y.KeyField
Order By Transaction.Dat eField Desc

Hope that helps
Mar 20 '08 #2
Zwoker
66 New Member
Ok, I hadn't thought of doing it that way. I had it in my head that to make it run quickly I needed to do it as a single query.

Before I read the reply I had already compromised by doing it as a single query with the Joins, and the Where statements from the user selection criteria. I ordered the result by the join keys and the date field. Although this returned a very large number of records, I coded a simple "on change" of the keys in VBA and just kept the first record for each change. A non-clean solution, but one that still runs reasonably quickly.

I'll try doing the second query using the table from the first, as suggested, and see how it goes.

The one problem I am having is that using a connection string approach to my data, rather that just declaring external tables in MS Access, seems to stop me returning the result directly as a table - I have to keep it as a result set. This is because the SELECT INTO statement doesn't seem to like the fact that I'm reading from an external table with the connection string, but trying to write to a local table. Or am I missing something obvious?
Mar 24 '08 #3
janders468
112 Recognized Expert New Member
That doesn't seem like it should be a problem. What error does it give you when you try to make it into a table?
Mar 25 '08 #4
Zwoker
66 New Member
That doesn't seem like it should be a problem. What error does it give you when you try to make it into a table?
It gives me an Oracle error about the table not existing. Which makes sense, as I don't want (and am not allowed) to create the table there, but want to create it in my local MS Access database where the code is running from.

It might be as simple as the method I'm using to call the SQL that I have built for the read. It looks like this:

Expand|Select|Wrap|Line Numbers
  1. Set InitialRst = New ADODB.Recordset
  2. InitialRst.Open SQLstring, connForDB, adOpenStatic, adLockReadOnly
If the SQLstring contains a SELECT INTO there is no way for it to know that I want the table to be created in the local database (is there?) rather that in the remote Oracle database that the connection string points to.

Alternatively, since I don't know SQL very well, and the syntax for the Oracle version even less, it could be a simple issue with the SQL command I am building. Just how do I write the SQL to allow me to read from the external table, but write to a local (MS Access) one?
If you can give me a simple example it might solve the issue.

I can see I need to find some sort of online tutorial on this subject, which can show me the different options avaiable in using the SQL string I have built over the remote database.
Mar 26 '08 #5

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

Similar topics

39
7849
by: | last post by:
I am trying to run the following agregate function in a parameterized query on Access2000: Min(.*sqr(./.)/) The query saved OK, but an attempt to run it results in the message: The expression is typed incorrectly or it is too complex to be evaluated If the sintax correct? Perhaps it is, otherwise it would not save. What can be done...
3
5884
by: Marcus | last post by:
Hi I have a very complex sql query and a explain plan. I found there is a full table scan in ID=9 9 8 TABLE ACCESS (FULL) OF 'F_LOTTXNHIST' (Cost=84573 Card=185892 Bytes=7063896) How can I correlate which part of the SQL statement is running on full table scan. Please see below for the code and explain plan SQL Code
116
7421
by: Mike MacSween | last post by:
S**t for brains strikes again! Why did I do that? When I met the clients and at some point they vaguely asked whether eventually would it be possible to have some people who could read the data and some who couldn't but that it wasn't important right now. And I said, 'sure, we can do that later'. So now I've developed an app without any...
3
4864
by: Susie Swint | last post by:
I have the following IIf statement which worked in Access 95 but will not work in Access 2002. The error message I get is that the expression is typed incorrectly, or is too complex to be evaluated. Can you tell me what is wrong with the statement? Audits: IIf(. Is Not Null,.,IIf(. Is Not Null,.,IIf. Is Not Null,.,IIf(. Is Not Null,.,0))))...
2
1826
by: Ben de Vette | last post by:
Hi, I'm using the querybuilder when updating a record in a table (Access). However, I get a "Query is too complex" message. The Primary key is autonumbered. Why is it making such a complex update query string in stead of using the primary key? Thanks in advance,
6
1754
by: Jon Bilbao | last post by:
I´m trying a select clause in two steps because it´s too complex. First: SELECT Reference, Results.idEnsayo, Results.Num_taladro, min(Results.dTime) + 500 AS tIni, max(Results.dTime) - 500 AS tLast FROM Results INNER JOIN Ensayos ON Results.idEnsayo=Ensayos.idEnsayo WHERE (Ensayos.Reference=9) GROUP BY Reference, Results.idEnsayo,...
1
2789
by: Randy Volkart | last post by:
I'm trying to fix a glitch in a complex access database, and have a fairly complex problem... unless there's some obscure easy fix I don't know being fairly new with Access. Basically, the area I'm trying to fix includes a form which takes entered data, concatenates it into a VB string to form an SQL query, then launches a report with...
19
3608
by: kawaks40 | last post by:
Hi everyone :) I just recently started using access/sql. and right away I ran into this problem "SQL expression too complex" I google'd a lot on what it means, and the only workaround I've seen sofar, is to just rewrite my code/query. Situation: I am sorting forms into the way they are used (print,storage,readonly,etc...).
3
15871
by: Eric Davidson | last post by:
DB2 9.5 I keep geting the message. SQL0101N The statement is too long or too complex. SQLSTATE=54001 When one of my sql statements takes over 60 seconds to compile the sql statement. Is there any parameter that controls how long DB2 allows a statement to compile for.
0
7813
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7431
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7761
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5337
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3457
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1888
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.