“未定义的函数”在 VBA 中使用 DAO QueryDef 时
我将 Access 2007 查询分配给 Excel VBA 中的 QueryDef。我的查询调用用户定义的函数,因为它对使用正则表达式评估字段的结果执行计算。我使用 QueryDef 是因为我在 UserForm 中收集值并希望将它们作为参数传递给查询。
当我运行 VBA 代码时,出现错误:“运行时错误‘3085’:表达式中未定义函数‘regexFunc’。”
这个问题 表明问题是 DAO 无法从 Excel 调用 Access UDF,因此我将 UDF 复制到 Excel VBA 模块中,但仍然收到错误。
访问查询:
select field1 from dataTable where regexFunc(field1)=[regexVal]
这是Excel VBA代码:
'QueryDef function
Sub makeQueryDef (str As String)
Dim qdf As QueryDef
Dim db As Database
Set db = OpenDatabase(DBpath)
Set qdf = db.QueryDefs("paramQuery")
qdf.Parameters("regexVal") = (str="test")
doSomething qdf
End Sub
'Regex function copied from Access VBA module to Excel VBA module
Function regexFunc(str As String) As Boolean
Dim re As RegExp
Dim matches As MatchCollection
regexFunc = False
Set re = New RegExp
re.Pattern = "\reg[ex](pattern)?"
Set matches = re.Execute(str)
If matches.Count <> 0 Then
regexFunc = True
End If
End Function
I'm assigning an Access 2007 query to a QueryDef in Excel VBA. My query calls a user-defined function, because it performs a calculation on the results of evaluating a field with a regular expression. I'm using a QueryDef because I'm collecting values in a UserForm and want to pass them to the query as parameters.
When I run my VBA code, I get an error: "Run-time error '3085': Undefined function 'regexFunc' in expression."
This question suggests that the problem is that DAO is unable to call Access UDFs from Excel, so I copied my UDF into the Excel VBA module, but I still get the error.
Access query:
select field1 from dataTable where regexFunc(field1)=[regexVal]
Here's the Excel VBA code:
'QueryDef function
Sub makeQueryDef (str As String)
Dim qdf As QueryDef
Dim db As Database
Set db = OpenDatabase(DBpath)
Set qdf = db.QueryDefs("paramQuery")
qdf.Parameters("regexVal") = (str="test")
doSomething qdf
End Sub
'Regex function copied from Access VBA module to Excel VBA module
Function regexFunc(str As String) As Boolean
Dim re As RegExp
Dim matches As MatchCollection
regexFunc = False
Set re = New RegExp
re.Pattern = "\reg[ex](pattern)?"
Set matches = re.Execute(str)
If matches.Count <> 0 Then
regexFunc = True
End If
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我要做的...刚刚测试了它,它与我的 UDF 配合得很好:
一件事 - 您是否需要不使用新的 Access.Application?
注意,我将基础查询设置为 SELECT ... INTO 查询,该查询创建一个名为“dataTableResults”的表
这是我在 Access 中的查询 (QueryDef):
我的 MS-Access DB 有一个名为“mysqr”的函数",它在上面的 SQL 中使用。
我正在查询的表“dataTable”只是一个数字列表,因此如果我的参数“avalue”是“16”,那么我会返回行“4”。如果我输入“4”(如我的代码中所示),我会得到“2”。
This is how I would do it... just tested it and it works fine with my UDF:
One thing - are you required to not use New Access.Application?
Note that I made my underlying query a SELECT ... INTO query that creates a table called 'dataTableResults'
This is my query (QueryDef) in Access:
My MS-Access DB has a function called "mysqr", which gets used in the SQL above.
The table "dataTable" I'm querying against is just a list of numbers, so if my parameter "avalue" is "16", then I get the row "4" back. If I enter "4" (as in my code), I get "2" back.
我已经解决了这个问题。我是这样做的。
首先,我将查询更改为记录集并将其传递给我的过滤函数:
这是过滤函数,它创建一个新数组,用通过 UDF 布尔检查的行填充它,然后返回它:
I've solved this. Here's how I did it.
First I change the query into a recordset and pass it to my filtering function:
And here's the filtering function, which creates a new array, populates it with rows that pass the UDF's boolean check, and returns it: