Link to home
Create AccountLog in
Avatar of pdvsa
pdvsaFlag for United States of America

asked on

Update a table field with value above it

Hello,

I need to update a table: tblImportToAccess.[TradeNo] and tblImportToAccess.[CoName]
with the value that is in the row above it.
User generated image
In the pic above, I need the nulls on [TradeNo] and [CoName] to populate with 123 and XYZ respectively for the first null but the second null I would need it to populate with 333 and ABC respectively (much like dragging down the cell above if it were in excel).

My dataset is much larger and there are different TradeNo and CoName.
I am not sure if this would best be done by way of an update query but if so then I have a part of it below and looking for the remainder.
I am a novice user of Access. I can easily do this in excel with a macro but I want to to perform this in access after I import the excel data.

UPDATE tblImportToAccess SET
WHERE (((tblImportToAccess.[TradeNo]) Is Null) AND ((tblImportToAccess.[CoName]) Is Null));

Thank you.
Avatar of Anders Ebro (Microsoft MVP)
Anders Ebro (Microsoft MVP)
Flag of Denmark image

It is important to understand that a database has no concept of row order or row number, unless defined by a query, that tells the database how rows are ordered, e.g. by stating "ORder by OrderDate"

Just use this code

Function UpdatePrevious() 
Dim rst As DAO.Recordset 
Dim tmpTradeNo As Long 
Dim tmpCoName As String 
Set rst = CurrentDb.OpenRecordset("tblImport") 
With rst 
tmpTradeNo = .Fields("TradeNo") 
 tmpCoName = .Fields("CoName") 
.MoveNext 
While Not .EOF 
If IsNull(.Fields("TradeNo")) Then 
.Edit 
.Fields("TradeNo") = tmpTradeNo 
.Update 
End If 
If Len(Nz(.Fields("CoName"), "")) = 0 Then 
.Edit 
.Fields("CoName") = tmpCoName 
.Update 
End If 
If Not IsNull(.Fields("TradeNo")) Then tmpTradeNo = .Fields("TradeNo") 
If Len(Nz(.Fields("CoName"), "")) > 0 Then tmpCoName = Nz(.Fields("CoName"), 0) 
.MoveNext 
Wend 
End With 
rst.Close 
End Function

Open in new window

As long your very first record has values this code should the job

Avatar of pdvsa

ASKER

Hi John and thanks for the response.  


I do have an error though.  

I had a update code to the "real' field names.   I posted the question and changed the names for better reading.  

The field names I changed were as follows:

[TradeNo] to the real field name of [Trade #] and [CoName] to [Buy CP].


I am not sure if that is why when I run I get a type mismatch error on tmpTradeNo = .Fields("Trade #") (7th line)

Not sure if I can have any spaces or if the "#" is what is causing the error. 


here is the code as I have it now after updating the field names to the "real" field names

Function UpdatePrevious()
    Dim rst As DAO.Recordset
    Dim tmpTradeNo As Long
    Dim tmpCoName As String
    Set rst = CurrentDb.OpenRecordset("tblImportToAccess")
    With rst
    tmpTradeNo = .Fields("Trade #")
     tmpCoName = .Fields("Buy CP")
    .MoveNext
    While Not .EOF
    If IsNull(.Fields("Trade #")) Then
    .Edit
    .Fields("Trade #") = tmpTradeNo
    .Update
    End If
    If Len(Nz(.Fields("Buy CP"), "")) = 0 Then
    .Edit
    .Fields("Buy CP") = tmpCoName
    .Update
    End If
    If Not IsNull(.Fields("Trade #")) Then tmpTradeNo = .Fields("Trade #")
    If Len(Nz(.Fields("Buy CP"), "")) > 0 Then tmpCoName = Nz(.Fields("Buy CP"), 0)
    .MoveNext
    Wend
    End With
    rst.Close

End Function

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of Mark Edwards
Mark Edwards
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Spaces and Special Characters are not the issue here...check if you have wrong datatype.

Best practice would be to fix the offending column names to remove the embedded spaces and special characters.  Otherwise, you always have to enclose these bad names in square brackets.

Avatar of pdvsa

ASKER

Experts, sorry for my tardiness.  I will be testing this soon and revert back with any questions.  

Avatar of pdvsa

ASKER

Hello, 


Mark, thank you. It is a little bit above me though.  


John, please see pic of the error I get.  Maybe it is simple.  

[Trade #] is short text in the table.  Just to recap what I said up above, I  changed your code from [TradeNo] to what it really is in the db which is [Trade #].  I know the name is not appropriate but I  am importing from Excel and need to stick to the field names.  Thank you. 


User generated image

Please refer to my earlier comment.


PS - you don't have to use the Excel column names as your table's column names.

Avatar of pdvsa

ASKER

Hi Pat, ok i understand however I need to keep the names as the ones found in excel.  I do not have control over the excel file names.  

That's not what I said.  You don't need to change the names in the excel file.  Link to the spreadsheet of importing.  Then you can use a query to append the data from the spreadsheet into properly named columns for you to use for the rest of the app.

Avatar of pdvsa

ASKER

Ahh I see what you are saying.  Sorry for my misunderstanding.  I have now updated my append query to change the bad names into good names.  I didnt think that I could change the names of the fields to whatever I wanted within the append.  I feel much better now that those bad names are gone.  Thank you so much....

You're welcome

Glad to help....