通过 JET 使用 Access 2003 VBA 函数
大家好。
我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您评论中的示例查询,我想知道使用通配符重写您的匹配模式以考虑可能的非数字字符是否“足够好”?
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?
您的问题有点不清楚,但 Access 确实允许您在查询中使用 VBA 函数。在 Access 中执行此操作是完全合法的:
如果没有嵌入此类函数,它的执行效果可能不如查询,但它会起作用。
另外,如果它是一次性查询,并且您不担心锁定系统中其他用户的一堆行,那么您也可以只打开表并使用 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:
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.
正如 JohnFx 已经说过的,使用 VBA 函数(无论是内置的还是自己编写的)应该可以。
如果您无法让它与查询中的 VBA 函数一起使用(无论出于何种原因),也许可以选择按代码完成所有操作?
如果它是一次性操作和/或性能不关键,您可以将整个表加载到 Recordset 中,循环遍历它并分别对每一行进行替换。
编辑:
好吧,当您从 .net 应用程序查询 Access 数据库时,这是完全不同的事情。
在这种情况下,无法使用任何内置或自行编写的 VBA 函数,因为 .net 不识别它们。决不。
那么,我们还有什么其他选择?
如果我理解正确的话,这不是一次性操作...您需要每次有人使用您的搜索页面时执行此替换操作,对吗?
在这种情况下,我会做一些完全不同的事情。
即使在查询中进行替换可行,从性能角度来看,这也不是最佳选择,因为它可能会减慢数据库速度。
如果您不经常写入数据库,而是进行大量读取(根据您的描述似乎是这种情况),我会执行以下操作:
-->当您进行搜索时,您已经拥有包含所有已剥离号码的 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:
--> 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.