Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Text and Replace

Status
Not open for further replies.

leadpro

Technical User
Jan 7, 2005
34
0
0
US
I have a State field that sometimes contains the entire State name. I need to replace the entire state name with the abbreviation for each state

THanks,

leadpro
 
How are ya leadpro . . . . .

[purple]BackUp the database so you can come back to square one if you have to.[/purple]

Now . . . in a module in the [purple]Modules Window[/purple], copy/paste the following:
Code:
[blue]Public Function AbbrevState(State)
   
   If State = "New York" Then
      AbbrevState = "NY"
   ElseIf State = "Georgia" Then
      AbbrevState = "GA"
   ElseIf State = "West Virginia" Then
      AbbrevState = "WV"
   [green]'
   'Other States for as many as you have.
   '[/green]
   Else
      [green]'Return the same if state fails to compare.[/green]
      AbbrevState = State
   End If
      
End Function[/blue]
You can run an [blue]Update Query[/blue] with [purple]Update To:[/purple] set to
Code:
[blue]AbbrevState([State])[/blue]
[purple]Thats it! . . . Give it a whirl & let me know . . .[/purple]


Calvin.gif
See Ya! . . . . . .
 
Works great! Any way to not make this case sensitive?

Thanks,

leadpro
 
You can just convert to either upper or lower case to do the test
Code:
If [COLOR=red]UCase$([/color]State[COLOR=red])[/color] = [COLOR=red]UCase$([/color]"New York"[COLOR=red])[/color] Then
   etc.
 
or . . . . .

At the very top of the module (1st line) where you put the code, you should have:
Code:
[blue]Option Compare Database
[purple]or[/purple]
Option Compare Text[/blue]

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top