|
Question : Loop through each table, then Find & Replace
|
|
I have an MS Access Database and I am looking for a way to do a global Find & Replace ( or UPDATE statement). I have several tables, each with various amounts of rows, columns. I want to do a Find & Replace for every table, and every column
so that if I find a value of "CAT" anywhere in the database I can change it to "DOG" for an example. I want it to match exactly so that something like "CATS" would NOT be changed
Note: I don't want to change Table names, just data within the tables. I'm guessing I need some sort of VB Script to do this
|
|
Answer : Loop through each table, then Find & Replace
|
|
In which case, try the following:
Public Sub ReplaceValues(pstrSearch As String, pstrReplace As String)
Dim dbsDatabase As Database Dim tdfTableDef As TableDef Dim rstRecordset As Recordset Dim fldField As Field
Set dbsDatabase = CurrentDb For Each tdfTableDef In dbsDatabase.TableDefs If (tdfTableDef.Attributes And dbSystemObject) = 0 Then Set rstRecordset = dbsDatabase.OpenRecordset(tdfTableDef.Name, dbOpenTable) Do Until rstRecordset.EOF rstRecordset.Edit For Each fldField In rstRecordset.Fields If (fldField.Attributes And dbSystemField) = 0 Then If StrComp(rstRecordset(fldField.Name), pstrSearch, vbBinaryCompare) = 0 Then rstRecordset(fldField.Name) = pstrReplace End If End If Next rstRecordset.Update rstRecordset.MoveNext Loop rstRecordset.Close Set rstRecordset = Nothing End If Next
End Sub
|
|
|
|