Replace text using recordset

B

Billy B

I am trying to loop through a table (tblPlants) using a recordset and update
part of the text fields text value. The variables strOld and strNew
represent the root paths that I am trying to find and replace in the PicPath
field of the table. I am getting an error message at the replace line of the
loop. Thank you.

Within the procedure...

With rsFnd
Do While Not rstfnd.EOF
If rsfnd!PicPath <> "" Then
replace(rsFnd!PicPath, strOld, strNew, 1, 3, vbTextCompare)
As String
rsFnd.MoveNext
Loop
End With

rsFnd.Close
 
D

dhauck

is your recordset readonly?
the "As String" syntax seems wrong, out of place
what is the error msg?
 
B

Billy B

Here is my recordset statement:
rsFnd.Open strSQLFnd, conn, adOpenForwardOnly, adLockPessimistic

The error for the code I posted is:
Statement invalid outside type block

If I remove the 'As String' I get the error message:
Expected =
 
M

Marshall Barton

Billy said:
I am trying to loop through a table (tblPlants) using a recordset and update
part of the text fields text value. The variables strOld and strNew
represent the root paths that I am trying to find and replace in the PicPath
field of the table. I am getting an error message at the replace line of the
loop. Thank you.

Within the procedure...

With rsFnd
Do While Not rstfnd.EOF
If rsfnd!PicPath <> "" Then
replace(rsFnd!PicPath, strOld, strNew, 1, 3, vbTextCompare)
As String
rsFnd.MoveNext
Loop
End With

rsFnd.Close


You need to set the field to the modified value and save it
back to the table.

You are using two recordset variables, Since you only
mentioned one recordset, I assume that one of the recordset
variable names is a typo. With that caveat, the code would
look more like:

With rsFnd
Do While Not .EOF
If !PicPath <> "" Then
!PicPath = Replace(!PicPath, strOld, strNew, _
1, 3, vbTextCompare)
End If
rsFnd.MoveNext
Loop
.Close : Set rsFnd = Nothing
End With


Note that using a record set to do this job is the slow,
inefficient way. Better to use an UPDATE query:

UPDATE thetable
SET
PicPath=Replace(PicPath,strOld,strNew,1,3,vbTextCompare)
WHERE PicPath Is Not Null
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top