1

I'm trying to write a piece of code that will run through a table and replace every field that has a certain value with another value.

Private Sub Form_Load()


Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String

Set db = CurrentDb()

strSQL = "SELECT Profile3 FROM Bank WHERE 'AB'"

Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)

Do Until rst.EOF

With rst

If .RecordCount > 0 Then

  .MoveFirst
  .Edit
  !Profile3 = "AA"
  .Update     
  .MoveNext


End If

End With

Loop

End Sub

That's what I'm currently using, however, when it runs it crashes horribly. I know the base code works because when I pull out the loop it works, but only on the first entry.

Like most of the issues I seem to have with VBA, it's probably an absurdly simple fix that I am overlooking.

Thank you for your help.

1 Answer 1

1

You are constantly moving first. You need to get on at some stage. Just get rid of MoveFirst.

Do Until rst.EOF
    With rst
      .Edit
      !Profile3 = "AA"
      .Update     
      .MoveNext
    End With
Loop

In addition, I guess you mean WHERE somefield:

strSQL = "SELECT Profile3 FROM Bank WHERE somefield='AB'"

However, in this case, I suspect what you need is:

strSQL = "UPDATE Bank SET Profile3 ='AA' WHERE Profile3 ='AB'"
CurrentDB.Execute strSQL, dbFailOnError
Sign up to request clarification or add additional context in comments.

2 Comments

Aha! See, I knew it was something simple. Now I just need to figure out how to make it not overwrite EVERY entry, but just the ones that say AB instead of AC or AD, etc. Thanks!
That worked absolutely perfectly. Thanks, I really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.