Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

In Query, how do I take previous field value and plug in current field 1

Status
Not open for further replies.

Odis

Technical User
Jan 25, 2001
78
US
I've looked for this and can't find exactly what I want. I don't know VB and would like to try and do this with the Build Expression.

First record has value "CC234" in Field A and next record is Null in Field A. I want to take "CC234" from previous record Field A and plug it into the current record Field A.

BEFORE
Record 1 Field A = CC234
Record 2 Field A =

AFTER
Record 1 Field A = CC234
Record 2 Field A = CC234

Thanks!

 
Suprisingly, there's no such thing as a "previous record" in a relational database unless your SQL involves an ORDER BY clause that produces a unique ordering of the records.

When you create an SQL SELECT statement the records will of course, be returned with some apparent ordering but that ordering is not guaranteed. Thus, when looking at a specific record, there is no way to tell what the "previous record" was or the "next record" will be.

Do you have an "Order By" clause that will result in a unique ordering of the records?

Also ...

What would you want to happen if you had
[tt]
Record 1 Field A = CC234
Record 2 Field A = NULL
Record 3 Field A = NULL
Record 4 Field A = NULL
[/tt]
?
 
I do have an Order BY on a Seq number field and the records you show with null values is exactly what I am getting. I want the null fields to contain the previous field that had a value in it like CC234.
 
Are there multiple values which field A could be depending on their "order" or is it the case that you simply want every record in the database to have CC234 as its value for Field A?
 
You may try this SQL:
UPDATE yourTable
SET [Field A] = DLookUp("[Field A]","yourTable","[Seq number]=" & DMax("[Seq number]","yourTable","[Seq number]<" & [Seq number] & " AND Not([Field A] Is Null)"))
WHERE [Field A] Is Null

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I tried plugging my values into the code you provided, but I'm getting a syntax error about not enough operators. I checked my code very carefully (could still have missed something). Could you verify the code you gave and check it for amy syntax? I pretty much put my values in between the quotes.
 
Table Name = Financial Req Ver 2
Field Name = Consolidate Fin Req V2



UPDATE [Financial Req Ver 2]
SET [Consolidate Fin Req V2] = DLookUp([Consolidate Fin Req V2],[Financial Req Ver 2], (Seq= & DMax Seq,[Financial Req Ver 2], Seq< & Seq & AND Not([Consolidate Fin Req V2] Is Null)))
WHERE [Consolidate Fin Req V2] Is Null
 
I'm pretty sure you need all the " that were in the original sample:

Code:
SET [Field A] = DLookUp("[Field A]","yourTable","[Seq number]=" & DMax("[Seq number]","yourTable","[Seq number]<" & [Seq number] & " AND Not([Field A] Is Null)"))
WHERE [Field A] Is Null

your code fixed would be:
Code:
UPDATE [Financial Req Ver 2]
SET [Consolidate Fin Req V2] = DLookUp("[Consolidate Fin Req V2]", "[Financial Req Ver 2]", "Seq=" & DMax ("Seq", "[Financial Req Ver 2]", "Seq<" & Seq & " AND Not([Consolidate Fin Req V2] Is Null)"))
WHERE [Consolidate Fin Req V2] Is Null



Leslie

Have you met Hardy Heron?
 
Thanks Sooo much! I cut and pasted your exact code and it worked perfectly! I need to do another similar lookup, but I'm going to give it a shot by tweaking your code.

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top