访问 VBA |如何用另一个字符串替换字符串的一部分

发布于 2024-12-22 03:30:33 字数 191 浏览 0 评论 0原文

我正在尝试创建一段代码来用另一个单词替换一个单词。 示例:将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世界如花海般美丽 2024-12-29 03:30:33

使用 Access 的 VBA 函数 Replace(text,查找、替换)

Dim result As String

result = Replace("Some sentence containing Avenue in it.", "Avenue", "Ave")

Use Access's VBA function Replace(text, find, replacement):

Dim result As String

result = Replace("Some sentence containing Avenue in it.", "Avenue", "Ave")
挽清梦 2024-12-29 03:30:33

我正在阅读这篇文章,并想添加信息,尽管它对于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

迷你仙 2024-12-29 03:30:33

您也可以使用与此类似的函数,它允许您在想要更改值的不同情况下添加:

Public Function strReplace(varValue As Variant) as Variant

    Select Case varValue

        Case "Avenue"
            strReplace = "Ave"

        Case "North"
            strReplace = "N"

        Case Else
            strReplace = varValue

    End Select

End Function

然后您的 SQL 将读取如下内容:

SELECT strReplace(Address) As Add FROM Tablename

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:

Public Function strReplace(varValue As Variant) as Variant

    Select Case varValue

        Case "Avenue"
            strReplace = "Ave"

        Case "North"
            strReplace = "N"

        Case Else
            strReplace = varValue

    End Select

End Function

Then your SQL would read something like:

SELECT strReplace(Address) As Add FROM Tablename
一杯敬自由 2024-12-29 03:30:33

由于字符串“North”可能是街道名称的开头,例如“Northern Boulevard”,因此街道方向始终位于街道编号和街道名称之间,并且与街道编号和街道名称分开。

Public Function strReplace(varValue As Variant) as Variant

Select Case varValue

    Case "Avenue"
        strReplace = "Ave"

    Case " North "
        strReplace = " N "

    Case Else
        strReplace = varValue

End Select

End Function

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.

Public Function strReplace(varValue As Variant) as Variant

Select Case varValue

    Case "Avenue"
        strReplace = "Ave"

    Case " North "
        strReplace = " N "

    Case Else
        strReplace = varValue

End Select

End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文