访问 VBA |如何用另一个字符串替换字符串的一部分
我正在尝试创建一段代码来用另一个单词替换一个单词。 示例:将 Avenue 替换为 Ave,将 North 替换为 N。 我正在使用 MS Access,我可以使用 SQL REPLACE 函数,但我想使用 Access 模块在 VBA 中执行此操作,以便我可以将该函数附加到其他列。
我不知道从哪里开始,所以任何意见将不胜感激。
盖伊
I am trying to create a piece of code that replaces one word with another.
Example: Replace Avenue with Ave and North with N.
I am using MS Access, I could use SQL REPLACE Function but I want to do this in VBA using Access module so that I can attached the function to other column.
I am not sure where to start with this, so any input will be greatly appreciated.
Guy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Access 的 VBA 函数
Replace(text,查找、替换)
:Use Access's VBA function
Replace(text, find, replacement)
:我正在阅读这篇文章,并想添加信息,尽管它对于OP来说肯定不再及时。
上面的 BiggerDon 指出了死记硬背将“North”替换为“N”的困难。 “Avenue”到“Ave”也存在类似的问题(例如“Avenue of the Americas”变成“Ave of the Americas”:仍然可以理解,但可能不是OP想要的。replace
()函数完全与上下文无关,但地址不是。一个完整的解决方案需要有额外的逻辑来正确解释上下文,然后根据需要应用replace()。
数据库通常包含地址,所以我想指出OP问题的通用版本。到内的地址美国已通过编码准确性支持系统 (CASS) 进行寻址(幽默!),它是一种数据库工具,它接受美国地址并完成或更正它以满足美国邮政服务设定的标准https://en.wikipedia.org/wiki/Postal_address_verification 有基础知识,更多信息可在邮局获取:https://ribbs.usps.gov /index.cfm?page=address_info_systems
I was reading this thread and would like to add information even though it is surely no longer timely for the OP.
BiggerDon above points out the difficulty of rote replacing "North" with "N". A similar problem exists with "Avenue" to "Ave" (e.g. "Avenue of the Americas" becomes "Ave of the Americas": still understandable, but probably not what the OP wants.
The replace() function is entirely context-free, but addresses are not. A complete solution needs to have additional logic to interpret the context correctly, and then apply replace() as needed.
Databases commonly contain addresses, and so I wanted to point out that the generalized version of the OP's problem as applied to addresses within the United States has been addressed (humor!) by the Coding Accuracy Support System (CASS). CASS is a database tool that accepts a U.S. address and completes or corrects it to meet a standard set by the U.S. Postal Service. The Wikipedia entry https://en.wikipedia.org/wiki/Postal_address_verification has the basics, and more information is available at the Post Office: https://ribbs.usps.gov/index.cfm?page=address_info_systems
您也可以使用与此类似的函数,它允许您在想要更改值的不同情况下添加:
然后您的 SQL 将读取如下内容:
You could use a function similar to this also, it would allow you to add in different cases where you would like to change values:
Then your SQL would read something like:
由于字符串“North”可能是街道名称的开头,例如“Northern Boulevard”,因此街道方向始终位于街道编号和街道名称之间,并且与街道编号和街道名称分开。
Since the string "North" might be the beginning of a street name, e.g. "Northern Boulevard", street directions are always between the street number and the street name, and separated from street number and street name.