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
As long your very first record has values this code should the job
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
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.
Experts, sorry for my tardiness. I will be testing this soon and revert back with any questions.
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.
Please refer to my earlier comment.
PS - you don't have to use the Excel column names as your table's column names.
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.
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