通过 JET 使用 Access 2003 VBA 函数

发布于 2024-09-27 04:59:16 字数 678 浏览 0 评论 0原文

大家好。

我需要在 n Access 2003 DB 上执行的查询中运行“replace([column], [new], [old])”。我知道我可以在 SQL 中使用的所有等效内容,并且相信我,我很乐意这样做,但我现在没有这个选项。我正在尝试执行一个查询,其中所有字母字符都从列中删除,即。 “(111) 111-1111”简单地变为“1111111111”。我还可以编写 awsum 自定义 VBA 函数并使用它执行查询,但同样无法通过 JET 使用这些函数。有什么想法吗?


感谢各位的回复。好吧,让我澄清一下情况。我正在运行一个 .NET Web 应用程序。此应用程序使用 Access 2003 数据库。我正在尝试进行升级,其中包含一种搜索页面。此页面执行如下查询:SELECT * FROM [table] WHERE Replace([telnumber], '-', '') LIKE '1234567890'。问题是 [telnumber] 列中有许多记录包含字母字符,例如“(123) 123-1234”。在进行比较之前我需要过滤掉这一点。因此,当我在测试环境 IN ACCESS 中运行查询时,使用内置 VBA 函数的查询执行得很好,但是当我从 Web 应用程序运行查询时,它会抛出一个异常,指出“找不到替换函数”之类的内容。有什么想法吗?

Hallo all.

I need to run the 'replace([column], [new], [old])' in a query executing on n Access 2003 DB. I know of all the equivalent stuff i could use in SQL, and believe me I would love to, but i don't have this option now. I'm trying to do a query where all the alpha chars are stripped out of a column ie. '(111) 111-1111' simply becomes '1111111111'. I can also write an awsum custom VBA function and execute the query using this, but once again, can't use these functions through JET. Any ideas?


Thanx for the replies guys. Ok let me clarify the situation. I'm running an .NET web application. This app uses an Access 2003 db. Im trying to do an upgrade where I incorporate a type of search page. This page executes a query like: SELECT * FROM [table] WHERE replace([telnumber], '-', '') LIKE '1234567890'. The problem is that there are many records in the [telnumber] column that has alpha chars in, for instance '(123) 123-1234'. This i need to filter out before i do the comparison. So the query using a built in VBA function executes fine when i run the query in a testing environment IN ACCESS, but when i run the query from my web app, it throws an exception stating something like "Replace function not found". Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一身仙ぐ女味 2024-10-04 04:59:16

根据您评论中的示例查询,我想知道使用通配符重写您的匹配模式以考虑可能的非数字字符是否“足够好”?

SELECT * FROM [table] WHERE telnumber LIKE '*123*456*7890'

Based on the sample query from your comment, I wonder if it could be "good enough" to rewrite your match pattern using wildcards to account for the possible non-digit characters?

SELECT * FROM [table] WHERE telnumber LIKE '*123*456*7890'
掩饰不了的爱 2024-10-04 04:59:16

您的问题有点不清楚,但 Access 确实允许您在查询中使用 VBA 函数。在 Access 中执行此操作是完全合法的:

SELECT replace(mycolumn,'x','y') FROM myTable

如果没有嵌入此类函数,它的执行效果可能不如查询,但它会起作用。

另外,如果它是一次性查询,并且您不担心锁定系统中其他用户的一堆行,那么您也可以只打开表并使用 Control 进行查找和替换-H。

Your question is a little unclear, but Access does allow you to use VBA functions in Queries. It is perfectly legal in Access to do this:

SELECT replace(mycolumn,'x','y') FROM myTable

It may not perform as well as a query without such functions embedded, but it will work.

Also, if it is a one off query and you don't have concerns about locking a bunch of rows from other users who are working in the system, you can also get away with just opening the table and doing a find and replace with Control-H.

放赐 2024-10-04 04:59:16

正如 JohnFx 已经说过的,使用 VBA 函数(无论是内置的还是自己编写的)应该可以。

如果您无法让它与查询中的 VBA 函数一起使用(无论出于何种原因),也许可以选择按代码完成所有操作?
如果它是一次性操作和/或性能不关键,您可以将整个表加载到 Recordset 中,循环遍历它并分别对每一行进行替换。


编辑:

好吧,当您从 .net 应用程序查询 Access 数据库时,这是完全不同的事情。
在这种情况下,无法使用任何内置或自行编写的 VBA 函数,因为 .net 不识别它们。决不。

那么,我们还有什么其他选择?
如果我理解正确的话,这不是一次性操作...您需要每次有人使用您的搜索页面时执行此替换操作,对吗?

在这种情况下,我会做一些完全不同的事情。
即使在查询中进行替换可行,从性能角度来看,这也不是最佳选择,因为它可能会减慢数据库速度。

如果您不经常写入数据库,而是进行大量读取(根据您的描述似乎是这种情况),我会执行以下操作:

  • 在表中添加一列“TelNumberSearch”
  • 每次当您保存一条记录,您将电话号码保存在“电话号码”列中,您对电话号码进行替换,并将剥离后的号码保存在“电话号码搜索”列中

-->当您进行搜索时,您已经拥有包含所有已剥离号码的 TelNumberSearch 列...无需为每次搜索再次剥离它们。并且您仍然保留带有原始数字(带有字母字符)的列用于显示目的。

当然,您需要填充新列一次,但这是一次性操作,因此在这种情况下循环遍历记录并对每个记录进行单独替换就可以了。

As JohnFx already said, using VBA functions (no matter if built in or written by yourself) should work.

If you can't get it to work with the VBA function in the query (for whatever reason), maybe doing it all per code would be an option?
If it's a one-time action and/or not performance critical, you could just load the whole table in a Recordset, loop through it and do your replacing separately for each row.


EDIT:

Okay, it's a completely different thing when you query an Access database from a .net application.
In this case it's not possible to use any built-in or self-written VBA functions, because .net doesn't know them. No way.

So, what other options do we have?
If I understood you correctly, this is not a one-time action...you need to do this replacing stuff every time someone uses your search page, correct?

In this case I would do something completely different.
Even if doing the replace in the query would work, performance wise it's not the best option because it will likely slow down your database.

If you don't write that often to your database, but do a lot of reads (which seems to be the case according to your description), I would do the following:

  • Add a column "TelNumberSearch" to your table
  • Every time when you save a record, you save the phone number in the "TelNumber" column, and you do the replacing on the phone number and save the stripped number in the "TelNumberSearch" column

--> When you do a search, you already have the TelNumberSearch column with all the stripped numbers...no need to strip them again for every single search. And you still have the column with the original number (with alpha chars) for displaying purposes.

Of course you need to fill the new column once, but this is a one-time action, so looping through the records and doing a separate replace for each one would be okay in this case.

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