View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
K Dales[_2_] K Dales[_2_] is offline
external usenet poster
 
Posts: 1,163
Default Use ADO to add or update date in Access

rs.Find is not really a function, but an object method, so it doesn't return
anything in the usual sense. Instead it will move your recordset's cursor
either to the found record (if there is a match) or to the end of file (EOF)
if there is no match. So the way to use it would be:
rs.Find("RACFID=" & Range("A" & r).Value) ' This performs the search
If rs.EOF Then AddNew
' if we are at the end of file then the RACFID was not found so add a new
one
' otherwise: nothing. If it was found you rs is now pointing to the
record you want updated. So there is no ELSE clause, the IF is a one liner!
' so now set (or reset) the values of the fields
.Fields("RACFID") = Range("A" & r).Value
.Fields("Employee_Type") = "Senior_Developer"
.Fields(Range("C1").Value) = Range("C" & r).Value
...
.Update ' will actually execute the changes on the database

So in the end it really is a simple addition to the code (see what I meant
about reusing the .Fields()= section?). The only "tricky" things that might
cause problems:
1) Is the RACFID a numeric field in the database? I wrote the above
assuming it is, if it is text you will need to force single quotes around it,
rs.Find("RACFID='" & Range("A" & r).Value & "'")
2) If the .Find or .Update fail you might need to change the CursorType or
CursorLocation for rs. I don't often use KeySet cursors so I am not so
familiar with how it handles updates - but I think if your current code to
add a record is working on the database, you should also be able to change an
existing record. This is often a confusing and tricky thing, though.

I hope this gets it working for you.
- K Dales


"ptrively" wrote:

Thanks for the quick reply. I've been reading through the MSN database to
try and figure out the best way to do the search. I believe where I'm
having the problem is I can't determine what the find function returns,
this is what I believe the code should look like...But it is not working,
all of the fields on the MSN website list as optional, so I'm at a bit of
a loss.

If ActiveSheet.Name = "Sr Developer" Then
Do While Len(Range("A" & r).Formula) 0
' repeat until first empty cell in column A

If rs.Find(Range("A" & r).Value) = False Then
With rs

.AddNew ' create a new record
' add values to each field in the record
.Fields("RACFID") = Range("A" & r).Value
.Fields("Employee_Type") = "Senior_Developer"
.Fields(Range("C1").Value) = Range("C" & r).Value
.Fields(Range("D1").Value) = Range("D" & r).Value
.Fields(Range("E1").Value) = Range("E" & r).Value
.Update
' add more fields if necessary...
End With
r = r + 1 ' next row
Loop
Else
With rs
.Update ' create a new record
' add values to each field in the record
.Fields("RACFID") = Range("A" & r).Value
.Fields("Employee_Type") = "Senior_Developer"
.Fields(Range("C1").Value) = Range("C" & r).Value
.Fields(Range("D1").Value) = Range("D" & r).Value
.Fields(Range("E1").Value) = Range("E" & r).Value
.Update
' add more fields if necessary...
End With
r = r + 1 ' next row
Loop
End If