Updating a field based on a certain criteria

toukey1

New member
Local time
Today, 08:43
Joined
Nov 6, 2006
Messages
7
Is there a way in a query (using SQL) to go to the first record to update a field based on a criteria ?
For example if my amount field shows two amounts for $300.00 I want to go to the first record that has $300.00 to update a field in that record.

Is that possible?
 
Is your primary key an autonumber?
 
In response to KeithG's question, I am using it based on results from a query
 
How can I use Find First in the query below?

SELECT Pos.osamt, Pos.ClientNo, Pos.FirstName, Pos.LastName, Pos.Address1, Pos.Address2, Pos.City, Pos.Country, Pos.TransDate, Pos.InvoiceNo, Pos.PolicyNo, Pos.TransAmt, Pos.TaxAmt, Pos.ReceiptAmt, Pos.Currency
FROM Pos LEFT JOIN Neg ON (Pos.osamt = abs(Neg.osamt)) AND (Pos.ClientNo = Neg.ClientNo)
WHERE (((Neg.osamt) Is Null));

The above query works EXCEPT when there are 2 debits and 1 credit for the same amount. It applies the 1 credit to both debits. I need it to apply it to only one debit.
 
and, more existentially, what is the "first" record you want to find? Firstname, alphabetically? Lastname? Receiptamt - ascending or descending? or is it CLientNO? The field that dictates what is "first" will dictate your solution...
 
meloncolly said:
and, more existentially, what is the "first" record you want to find? Firstname, alphabetically? Lastname? Receiptamt - ascending or descending? or is it CLientNO? The field that dictates what is "first" will dictate your solution...
And shouldn't we be asking why the first , why are there two?
 
My apologies for not making myself clear. What I am trying to achieve is NOT to have on my Client Statement report (by client) records where debits and credits can be matched and removed from the report as clients find it confusing. For example, I don't want to see say

Amount Inv. No.
$120.00 12
($120.00) 16
$200.00 4
($200.00) 10

So, with the little programming experiece I have I created a query (POS) to extract all the positive amounts from a query that I already had existing and another query (NEG) to extract all the negative amounts.

I then created 2 other queries (uniquepos and uniqueneg) to match them up and return those that were not matched.

It all worked fine until I had a client with 2 debit amounts for say $200.00 and 1 credit for ($200.00). What the code did was apply the credit to both debits. How I want it to work is to apply the credit to ONLY 1 of the deibts where the amounts are equal (whether it's the first or last).

My SQL statements are listed below.

POS query

SELECT qryclientstat.ClientNo, qryclientstat.FirstName, qryclientstat.LastName, qryclientstat.Address1, qryclientstat.Address2, qryclientstat.City, qryclientstat.Country, qryclientstat.TransDate, qryclientstat.InvoiceNo, qryclientstat.PolicyNo, qryclientstat.TransAmt, qryclientstat.OsAmt, qryclientstat.TaxAmt, qryclientstat.ReceiptAmt, qryclientstat.Currency
FROM qryclientstat
WHERE (((qryclientstat.OsAmt)>0));

NEG query

SELECT qryclientstat.ClientNo, qryclientstat.LastName, qryclientstat.FirstName, qryclientstat.Address1, qryclientstat.Address2, qryclientstat.City, qryclientstat.Country, qryclientstat.TransDate, qryclientstat.InvoiceNo, qryclientstat.PolicyNo, qryclientstat.TransAmt, qryclientstat.OsAmt, qryclientstat.TaxAmt, qryclientstat.ReceiptAmt, qryclientstat.Currency
FROM qryclientstat
WHERE (((qryclientstat.OsAmt)<0));

UNIQUEPOS query

SELECT Pos.osamt, Pos.ClientNo, Pos.FirstName, Pos.LastName, Pos.Address1, Pos.Address2, Pos.City, Pos.Country, Pos.TransDate, Pos.InvoiceNo, Pos.PolicyNo, Pos.TransAmt, Pos.TaxAmt, Pos.ReceiptAmt, Pos.Currency
FROM Pos LEFT JOIN Neg ON (Pos.osamt = abs(Neg.osamt)) AND (Pos.ClientNo = Neg.ClientNo)
WHERE (((Neg.osamt) Is Null));

UNIQUENEG query

SELECT Neg.osamt, Neg.ClientNo, Neg.FirstName, Neg.LastName, Neg.Address1, Neg.Address2, Neg.City, Neg.Country, Neg.TransDate, Neg.InvoiceNo, Neg.PolicyNo, Neg.TransAmt, Neg.TaxAmt, Neg.ReceiptAmt, Neg.Currency
FROM Neg LEFT JOIN Pos ON Neg.osamt = Pos.osamt * -1
WHERE (((Pos.osamt) Is Null));

UNION query

SELECT * FROM uniquepos UNION select * FROM uniqueneg;
 
Maybe I've lost the plot, but you seem to matching debits and credits on Client No and amount, shouldn't it be on something a little more unique than that? You are having a problem because 2 debits have the same amount, can't there be even more, say 5 debits of $200 and 3 credits of $200.

Brian
 
I was just using "first" as an example. If there are 5 debits and 3 credits then I want to match the 3 credits to 3 debits and not apply the 3 credits to all 5 of the debits.

Is that possible? This entire scenario is very rare.
 

Users who are viewing this thread

Back
Top Bottom