View Full Version : Update Null Record with Previous Record's Field Value


feathers212
10-17-2008, 07:35 AM
I have two tables (one that is linked to an external data source that I have no edit control over [SAP] and another that is hosted within the access database I am working with). The "internal" table is just capturing additional records that are not captured by the "external" table database (both have the same fields, just different recordsets).
When reporting, I want this information to be combined together. I have created 1 delete and 2 append queries that merge the two tables into one "All_Records" table which is sorted by Date and then Time (2 of the fields).
Before I am able to use this data to continue developing my reporting, I need to find a resolution to a problem that I am having. Due to the nature of the "External" table data, some of the records have null values in their "Last_Name" field. I need to have these null values updated such that their new values are equal to the value of the previous non-null recordset.

Example:
All_Records table before
Last_Name Count
Jones 2
<null> 5
<null> 3
Wallace 4
<null> 1

All_Records table after
Last_Name Count
Jones 2
Jones 5
Jones 3
Wallace 4
Wallace 1

I'm stuck on how to approach this problem. Anyone?

Thanks :)

Mutdogus
10-17-2008, 07:51 AM
if you are using an ADO recordset you could, while looping with your records already sorted in order.

do until rs.eof
if isnull(rs.fields("YourFieldHere").value) then
'Do nothing
else
LastName = rs.fields("YourFieldHere").value
end if
loop

Thus making the LastName value stay the same when the records value is null.

feathers212
10-17-2008, 10:43 AM
Thanks for the idea Mutdogus. I used the following and it gave me what I wanted (recordset was already sorted in order):

Dim dbMaterialTrans As Object
Dim rst As Object
Dim L_Name As String

Set dbMaterialTrans = CurrentDb
Set rst = dbMaterialTrans.OpenRecordset("All_Transactions")

Do While Not rst.EOF
If Not IsNull(rst.Fields("Last_Name").Value) Then
L_Name = rst.Fields("Last_Name").Value
Else
rst.Edit
rst.Fields("Last_Name").Value = L_Name
rst.Update
End If
rst.MoveNext
Loop