Results 1 to 8 of 8
  1. #1
    2 Star Lounger
    Join Date
    Oct 2001
    Location
    Suffolk, England
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find and Replace in VBA (access 2000)

    How do you in VBA - speak ask the database to find and replace all instances of (say) 2 with Y in the currently open table?
    Thanks
    Allison

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find and Replace in VBA (access 2000)

    Do you want to replace 2 with Y in one specific field in the table, or throughout all fields?

  3. #3
    2 Star Lounger
    Join Date
    Oct 2001
    Location
    Suffolk, England
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find and Replace in VBA (access 2000)

    Just in 2 specific fields
    thanks

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find and Replace in VBA (access 2000)

    Next question: do you want to replace if the whole field value is 2, or do you want to replace any 2 in a field, e.g. Lotus123 becomes Lotus1Y3?

  5. #5
    2 Star Lounger
    Join Date
    Oct 2001
    Location
    Suffolk, England
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find and Replace in VBA (access 2000)

    I would want the final answer to be Lotus1Y3
    Thanks

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find and Replace in VBA (access 2000)

    You can use code like this - it requires a reference (in Tools | References...) to the Microsoft ActiveX Objects 2.1 Library (it should be selected by default).

    Sub ReplaceInTable()
    Dim cnn As ADODB.Connection
    Dim rst As New ADODB.Recordset

    On Error GoTo ErrHandler

    Set cnn = CurrentProject.Connection
    rst.Open "tblSomething", cnn, adOpenKeyset
    Do While Not rst.EOF
    rst![Field1] = Replace(rst![Field1],"2","Y")
    rst![Field2] = Replace(rst![Field2],"2","Y")
    rst.Update
    rst.MoveNext
    Loop

    ExitHandler:
    On Error Resume Next
    rst.Close
    Set rst = Nothing
    Set cnn = Nothing
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

  7. #7
    2 Star Lounger
    Join Date
    Oct 2001
    Location
    Suffolk, England
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find and Replace in VBA (access 2000)

    thanks
    Allison

  8. #8
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,624
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: Find and Replace in VBA (access 2000)

    This is really just curiosity, but why do you want to write code to do that when the Menu Command Edit/Replace will do that pretty well, and probably a fair bit faster than the VBA routine can do it? In addition, it isn't often that you want to repeat a process like that more than once on the same data - are you by chance importing a new set of data each time you need to run this?
    Wendell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •