Solved

Query with too many variables

Posted on 2013-01-03
5
458 Views
Last Modified: 2013-01-14
I am trying to write an update query but I can't get the initial select query right.  I am hoping I am making this more difficult than it needs to be but I am stuck.

I have two tables - Temp (shortened for here) and History.  They have all the same fields (I perform calculations in temp before appending it to history) and the link is ticker.  I need to write a query to return values where ticker in the two tables is equal,  quarter from history references the field on a form, and category from one table does not equal category from the other.

In other words, I want the values where
History.Ticker
History.Quarter (Form!Front!PreviousQuarter)
History.Category
is compared to
Temp.Ticker
Temp.Category
and values where the category is different will be returned.
0
Comment
Question by:jmerulla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 38742033
Try this:

SELECT * 
FROM History, Temp
WHERE History.Ticker = Temp.Ticker AND History.Quarter = Forms!Front!PreviousQuarter AND History.Category <> Temp.Category

Open in new window

0
 
LVL 30

Expert Comment

by:hnasr
ID: 38742322
One way is to select records using INNER JOIN, then apply the where clause.

UPDATE HISTORY INNER JOIN Temp ON History.Ticker = Temp.Ticker
SET HISTORY.fld1 = Temp.fld1, HISTORY.fld2 = Temp.fld2
WHERE History.Quarter = Form!Front!PreviousQuarter AND History.Category <> Temp.Category

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 38744065
Can you provide some sample data for the two tables and what you expect the result of the update to look like?
0
 
LVL 2

Author Comment

by:jmerulla
ID: 38747570
Here is what I have and the query doesn't return anything -

SELECT [Temporary Add to History per Quarter2].*, *
FROM [Temporary Add to History per Quarter2], [Fund History]
WHERE ((([Fund History].Quarter)=[Forms]![Front]![Previous Qtr]) AND (([Fund
History].[Full Name Category])<>[Temporary Add to History per Quarter2]![Full Name
Category]) AND (([Temporary Add to History per Quarter2].Ticker)=[Temporary Add to
History per Quarter2]![Ticker]));

The temp file is there and the field on the form is correct.

In the end, I want it to return a table like this -

Ticker      Quarter      Recommendation      Full Name Category
AOGIX            2012-9-30      1                  Aggressive Allocation
So I can make an update query to change recommendation from 1 to 2.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 38747620
A couple of corrections and simplifying the syntax a bit:

SELECT temp.*, hist.*
FROM [Temporary Add to History per Quarter2] temp, [Fund History] hist
WHERE (hist.[Full Name Category] <> temp.[Full Name 
Category]) AND (temp.Ticker = hist.[Ticker]) AND (hist.Quarter = [Forms]![Front]![Previous Qtr]) 

Open in new window


If that still returns no records, then as requested above please provide some sample data meaning:

- Sample data representative of what you see in both tables
- Expected results based on the data given

(Mask any sensitive data as needed)

Or better yet, upload a sample database  (.mdb or accdb file) with the query, tables and data involved (again, masking any sensitive data).
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

717 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