473,378 Members | 1,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

VBA to replace text at end of string in Access 2007

Within a table (tbl_Companies) I have a field (Company_Name) used to store a list of Company Names that I have pulled from multipe sources. I would like to clean the data within the table to remove duplicates.

I have identified a few substrings (including any derivation of case) I would like to remove from the end of each Company Name where present:

1. " Corporation"
2. " Incorporated"
3. " Company"
4. " Limited"
5. " Corp"
6. " Co Inc"
7. " Co LLC"
8. " Co"
9. " Plc"
10. " Ltd"
11. " LLC"
12. " Inc"

An example would be "River College Inc" = "River College" (and not "Riverllege Inc" or "Riverllege").

Can someone please provide some guidance to assist?

Thank you,

BDS
Mar 26 '13 #1
5 3170
Rabbit
12,516 Expert Mod 8TB
You can use a series of regular expression replacements to get rid of those phrases at the end.
Mar 26 '13 #2
Thank you for the response Rabbit. I was thinking of something along these lines instead of using a replace:

Expand|Select|Wrap|Line Numbers
  1. 'Remove legal entity types from end of string
  2. Public Function strRemType(strText As String) As String
  3.  
  4. Dim strChar As String
  5.     strChar = " Corporation"
  6.  
  7.     If Len(strChar) + (InStr(strText, strChar) - 1) = Len(strText) Then
  8.         strText = Left(strText, Len(strText) - Len(strChar))
  9.  
  10.     Else
  11.         strText = strText
  12.  
  13.     End If
  14.  
  15. strRemType = strText
  16.  
  17. End Function
I'm trying to determine what the best way is to do this across all of the substrings I'm trying to remove (as opposed to just copying each of the statements down and changing the strChar = " " part of the function).

Thanks again.
Mar 27 '13 #3
zmbd
5,501 Expert Mod 4TB
Before you can use RegEX() in Ms Access, go to the VBA, References, and find the library:
"Microsoft VBScript Regular Expressions #.#"
Otherwise RegEx Will throw a tempertantrum during the debug/compile
:)
Mar 27 '13 #4
Thank you Rabbit and zmbd. I've made some changes to implement this using RegEx via a function and sub but still had two items I'm trying to figure out:

1. For some reason neither the function or sub appear to be working properly based on my tests. I wanted to see if you could see anything wrong with the code.

2. Is it possible to declare more than one .pattern in the function so I can list out all of the combinations of characters I want removed? Also - since the order of which characters are being removed through the loop is important, will any suggestions take into account that consideration?

See updated code:

Expand|Select|Wrap|Line Numbers
  1. Private mlv As Object
  2.  
  3. Function RemoveType(strText As String) As String
  4.  
  5. If mlv Is Nothing Then
  6.     Set mlv = CreateObject("vbscript.regexp")
  7.  
  8. End If
  9.  
  10. With mlv
  11.     .Pattern = " Corporation$"
  12.     .IgnoreCase = False
  13.     .Global = False
  14.  
  15.     If .Test(strText) Then
  16.         RemoveType = strText
  17.  
  18.     End If
  19.  
  20. End With
  21.  
  22. End Function
  23.  
  24. Sub main()
  25.  
  26. Dim db As DAO.Database
  27. Dim rs As DAO.Recordset
  28. Dim mySQL As String
  29. Dim strColumn2, strColumn3  As String
  30.  
  31. Set db = CurrentDb()
  32.  
  33. mySQL = "SELECT Company_Name, strCompany_Name FROM tbl_Distressed_Companies_Master"
  34.  
  35. Set rs = db.OpenRecordset(mySQL)
  36.  
  37. rs.MoveFirst
  38.  
  39. Do Until rs.EOF
  40.     strColumn2 = rs.Fields("Company_Name")
  41.     strColumn3 = Trim(RemoveText(Trim(strColumn2)))
  42.  
  43. If Len(strColumn3) <> Null Then
  44.     rs.Edit
  45.     rs.Fields("strCompany_Name").Value = strColumn3
  46.     rs.Update
  47.  
  48. End If
  49.  
  50. rs.MoveNext
  51.  
  52. Loop
  53.  
  54. Set mlv = Nothing
  55.  
  56. End Sub
Mar 28 '13 #5
Rabbit
12,516 Expert Mod 8TB
You didn't actually remove the text you were searching for, you only tested for it.

As for your list of values, I would just put them in an array or table and loop through each running the regexp replacement mutliple times.

Though if you want, you can do it in one regexp by using the or functionality.
Mar 28 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Dennis | last post by:
Is there anything built in to vb.net that will take a plain text string and reformat it as HTML? What I mean is: o replace newlines with <BR> o replace " with &quot; o etc. I am using vb.net...
2
by: Brad Williams | last post by:
Greetings, I have a Access 2007 App and I want to have a Config File. I have several unbound Text Boxes that are in small Date format that will be used as a Filter for several forms. I would like...
5
by: ARC | last post by:
Just found out something interesting with Access 2007... In table design, if you set a memo field to the new rich text option, and put that control on a form, set the control to rich text, you can...
4
by: Neil | last post by:
Just found out that the Microsoft Rich Textbox does not support full text justification, since it's based on Version 1.0 of the RichEdit Window Class, and full text justification is only available...
16
by: Neil | last post by:
I posted a few days ago that it seems to me that the Access 2007 rich text feature does not support: a) full text justification; b) programmatic manipulation. I was hoping that someone might...
4
by: lupo666 | last post by:
Hi everybody, this time I have three problems driving me nuts :-((( (1) I have a report with 20 or so Yes/No "squares". Is there a way to either hide/show the "square" or change the yes/no...
8
by: elias.farah | last post by:
Hello Everyone, I'm having some very weird behavior on a couple of Access forms. (Not all forms, just some of them). The forms have been working for years, under Access XP/2003 etc, and last...
10
by: Arno R | last post by:
Hi all, So I bought a new laptop 10 days ago to test my apps with Vista. (home premium) Apparently Office 2007 is pre-installed. (a time limited but complete test version, no SP1) So I take the...
9
by: prakashwadhwani | last post by:
Hi !! I'm about to develop a new project for a client. Should I go about it in Access 2003 or 2007 ? Purchasing it either for me or for my client is not a major consideration here ... what I'd...
6
TheSmileyCoder
by: TheSmileyCoder | last post by:
I need to create a table, where one of the memo fields shall be a Rich Text field. Can anyone help me with that? This is the code I currently have. Public Sub createConTable() 'Create the table...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.